|
| 1 | +""" |
| 2 | +Regression test for Issue #856: Missing NullHandler in the edgar package logger. |
| 3 | +
|
| 4 | +A well-behaved library should attach a ``logging.NullHandler`` to its |
| 5 | +package-root logger so that it never emits log output unless the consuming |
| 6 | +application configures logging itself (see the Python logging HOWTO, |
| 7 | +"Configuring Logging for a Library"). Without it, edgartools warnings have no |
| 8 | +handler in their ancestry and fall back to the ``logging.lastResort`` handler, |
| 9 | +which writes to stderr -- especially harmful in MCP / stdio environments where |
| 10 | +stray stderr output corrupts the protocol stream. |
| 11 | +""" |
| 12 | + |
| 13 | +import logging |
| 14 | + |
| 15 | +import edgar # noqa: F401 (importing configures the package-root logger) |
| 16 | + |
| 17 | + |
| 18 | +class TestPackageRootNullHandler: |
| 19 | + def test_edgar_logger_has_null_handler(self): |
| 20 | + """The 'edgar' package-root logger must carry a NullHandler.""" |
| 21 | + handlers = logging.getLogger("edgar").handlers |
| 22 | + assert any(isinstance(h, logging.NullHandler) for h in handlers), ( |
| 23 | + "edgartools must attach a logging.NullHandler to the 'edgar' logger " |
| 24 | + "so library logging stays silent until the application configures it" |
| 25 | + ) |
| 26 | + |
| 27 | + def test_edgar_logger_adds_only_null_handler(self): |
| 28 | + """The library must not attach handlers other than NullHandler (HOWTO).""" |
| 29 | + handlers = logging.getLogger("edgar").handlers |
| 30 | + assert handlers, "the 'edgar' logger should have a NullHandler attached" |
| 31 | + assert all(isinstance(h, logging.NullHandler) for h in handlers), ( |
| 32 | + "a library must not attach handlers other than NullHandler to its " |
| 33 | + "package-root logger; that is the application's responsibility" |
| 34 | + ) |
0 commit comments