Title: wrong/ambiguous type descriptions for `LogRecord.exc_info` · Issue #161918 · python/cpython · GitHub
Open Graph Title: wrong/ambiguous type descriptions for `LogRecord.exc_info` · Issue #161918 · python/cpython
X Title: wrong/ambiguous type descriptions for `LogRecord.exc_info` · Issue #161918 · python/cpython
Description: Documentation Hey. The documentation of LogRecord describes the exc_info attribute as: exc_info (tuple[type[BaseException], BaseException, types.TracebackType] | None) – An exception tuple with the current exception information, as retur...
Open Graph Description: Documentation Hey. The documentation of LogRecord describes the exc_info attribute as: exc_info (tuple[type[BaseException], BaseException, types.TracebackType] | None) – An exception tuple with the...
X Description: Documentation Hey. The documentation of LogRecord describes the exc_info attribute as: exc_info (tuple[type[BaseException], BaseException, types.TracebackType] | None) – An exception tuple with the...
Opengraph URL: https://github.com/python/cpython/issues/161918
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"wrong/ambiguous type descriptions for `LogRecord.exc_info`","articleBody":"### Documentation\n\nHey.\n\nThe [documentation of `LogRecord`](https://docs.python.org/3/library/logging.html#logrecord-objects) describes the `exc_info` attribute as:\n\n> exc_info (tuple[type[BaseException], BaseException, types.TracebackType] | None) – An exception tuple with the current exception information, as returned by sys.exc_info(), or None if no exception information is available.\n\n\nHowever, records created through the normal logger methods can contain values that don’t match this type:\n```python\nimport logging\n\nclass H(logging.Handler):\n def emit(self, record):\n print(repr(record.exc_info))\n\nlogger = logging.getLogger()\nlogger.addHandler(H())\nlogger.propagate = False\n\nlogger.error(\"b\", exc_info=True) # outside of an except-block\nlogger.error(\"c\", exc_info=ValueError()) # exception that was never raised\n```\nwhich gives as of CPython 3.14.10:\n```\n(None, None, None)\n(\u003cclass 'ValueError'>, ValueError(), None)\n```\nA very recent commit (938144efad4659f7aa66bf9ef362fd3c602c4bbc) already fixed the case of a false truth value, which as per [“If exc_info does not evaluate as false, …”](https://docs.python.org/3/library/logging.html#logging.Logger.debug) is IMO allowed.\n\nNow:\n1. [Outside an `except`-block it would be `(None, None, None)`.](https://docs.python.org/3/library/sys.html#sys.exc_info)\n2. If an exception instance is passed, the tuple is built from its [__traceback__](url), which is [None](url) for exceptions that were never raised, while the documented type requires [types.TracebackType](url).\n\nI think typeshed already documents it properly:\nhttps://github.com/python/typeshed/blob/80f7beabfbb71a634cc3dbca8e418a5da790142c/stdlib/logging/__init__.pyi#L63\n\nhttps://github.com/python/typeshed/blob/80f7beabfbb71a634cc3dbca8e418a5da790142c/stdlib/logging/__init__.pyi#L318\n\nSo far I'd say this is a documentation issue.\n\nNow I can create valid `LogRecord`-objects that don’t go through `_log()` and its normalisation:\n```\n>>> import logging\n>>> l = logging.getLogger()\n>>> r = logging.makeLogRecord({\"exc_info\": True})\n>>> r.exc_info\nTrue\n>>> r = l.makeRecord(\"z\", 40, \"f\", 1, \"m\", None, exc_info=False)\n>>> r.exc_info\nFalse\n>>>\n```\nFor `makeLogRecord` and `makeRecord`… it’s IMO not really clear what values these allow (de jure) for `exc_info`.\nIf they’d follow the spirit of “If exc_info does not evaluate as false, …” then the the above type description would be even more wrong, as then `exc_info` could be any object.\n\nThis would then IMO also make the types on typeshed wrong, cause while `logging.Formatter.format()` itself would call `formatException()` only if `exc_info` has a true truth value:\nhttps://github.com/python/cpython/blob/ecefd40671f340d543c214b80744d4301d249f37/Lib/logging/__init__.py#L716-L720\n\nthat may be still any object, there's nothing for the false case and both `format()` and `formatException()` are public, so any subclass may call `formatException()` without the `if record.exc_info:`.\n\nLast but not least:\n[`formatException()` is more or less documented](https://docs.python.org/3/library/logging.html#logging.Formatter.formatException) to an even more restrictive type:\n\n> Formats the specified exception information (a standard exception tuple as returned by sys.exc_info()) as a string.\n\nSo not sure,... at least some documentation seems to be not exact... but maybe there also needs to be code changes if the normalisation of `exc_info` should happen not only via `LogRecord` objects created through `_log()`... and if not, typeshed might need some updates to account for values that may be given.\n\nCheers,\nChris\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-162179\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/calestyo","@type":"Person","name":"calestyo"},"datePublished":"2026-11-13T00:08:42Z","url":"https://github.com/python/cpython/issues/161918"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:c5037f29-d95d-dca4-31ca-590028fe75b5 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 86AA:3CBF1A:1A70C82:23F8F6D:6B0006D6 |
| html-safe-nonce | 14f152c792fec9828b6911fc1945f2c74b3c9ee8998cd05d9063e91cd7383a5b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4NkFBOjNDQkYxQToxQTcwQzgyOjIzRjhGNkQ6NkIwMDA2RDYiLCJ2aXNpdG9yX2lkIjoiODI1NDQ3MzU5MTg2NTczODk2NiIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 2377b75c582829e5cf0dc8f1bf9f8cb1a912ca69edc41ad27bfedd2ee035a65c |
| hovercard-subject-tag | issue:6361725786 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/161918/issue_layout |
| twitter:image | https://opengraph.githubassets.com/20bd8e7814969b61bce203c90fcdc2ad6b77543ccd5cbeb3e68300eb3e03a48c/python/cpython/issues/161918 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/20bd8e7814969b61bce203c90fcdc2ad6b77543ccd5cbeb3e68300eb3e03a48c/python/cpython/issues/161918 |
| og:image:alt | Documentation Hey. The documentation of LogRecord describes the exc_info attribute as: exc_info (tuple[type[BaseException], BaseException, types.TracebackType] | None) – An exception tuple with the... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | calestyo |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width