Skip to content

[logging] Allow LogRecord.exc_info to be False - #16466

Merged
srittau merged 1 commit into
python:mainfrom
tjkuson:exc-info-fix
Oct 5, 2026
Merged

srittau merged 1 commit into
python:mainfrom
tjkuson:exc-info-fix

Conversation

@tjkuson

@tjkuson tjkuson commented Oct 3, 2026 •

Copy link
Copy Markdown
Contributor

Logger._log only normalises exc_info when it is truthy, so logger.info("msg", exc_info=False) creates a record whose exc_info is False. The standard library itself passes exc_info=False in asyncio, for example.

Given that the this value can technically be any falsy, the type could arguably be typing.Any (or just object). But I think this would make the annotation unhelpful for the vast majority of uses, and just adding Literal[False] to the union captures the most common way to pass a falsy value.

@github-actions

github-actions Bot commented Oct 3, 2026

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

trio (https://github.com/python-trio/trio)
+ src/trio/_tests/test_highlevel_serve_listeners.py:148: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ src/trio/_tests/test_highlevel_serve_listeners.py:149: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ src/trio/_core/_tests/test_instrumentation.py:209: error: "Literal[False]" object is not iterable  [misc]

zulip (https://github.com/zulip/zulip)
+ zerver/tests/test_email_mirror.py:2232: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]

sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/util/logging.py: note: In member "filter" of class "_RaiseOnWarningFilter":
+ sphinx/util/logging.py:411:47: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]

scrapy (https://github.com/scrapy/scrapy)
+ tests/test_pipeline_files.py:390: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ tests/test_request_cb_kwargs.py:182: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ tests/test_request_cb_kwargs.py:183: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ tests/test_request_cb_kwargs.py:188: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ tests/test_request_cb_kwargs.py:189: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ tests/test_crawl.py:209: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]
+ tests/test_crawl.py:222: error: Value of type "tuple[type[BaseException], BaseException, TracebackType | None] | tuple[None, None, None] | Literal[False]" is not indexable  [index]

@tjkuson
tjkuson marked this pull request as ready for review October 3, 2026 12:15
@srittau srittau added stubs: false positive Type checkers report false errors stubs: false negative Type checkers do not report an error, but should labels Oct 5, 2026

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't allow arbitrary falsy values to be passed to log() etc., I think Literal[False] is the correct annotation. This will also be a hint to users that falsely use is not None (and there are a few according to primer) that something is amiss.

@srittau
srittau merged commit b9808cc into python:main Oct 5, 2026
88 checks passed
@tjkuson
tjkuson deleted the exc-info-fix branch October 5, 2026 12:48
@srittau

srittau commented Oct 5, 2026

Copy link
Copy Markdown
Collaborator

I've also opened python/cpython#158839 and python/cpython#158840 to normalize falsy values before passing them to LogRecord to match the documentation (which says that LogRecord.exc_info can be an exception (info) or None).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stubs: false negative Type checkers do not report an error, but should stubs: false positive Type checkers report false errors

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants