Title: Documented pattern for adding contextual information to LogRecord objects fails type checking · Issue #7833 · python/typeshed · GitHub
Open Graph Title: Documented pattern for adding contextual information to LogRecord objects fails type checking · Issue #7833 · python/typeshed
X Title: Documented pattern for adding contextual information to LogRecord objects fails type checking · Issue #7833 · python/typeshed
Description: https://docs.python.org/3/library/logging.html#logrecord-objects gives the following pattern for adding contextual information to LogRecord objects which can then be used in log formats: old_factory = logging.getLogRecordFactory() def re...
Open Graph Description: https://docs.python.org/3/library/logging.html#logrecord-objects gives the following pattern for adding contextual information to LogRecord objects which can then be used in log formats: old_factor...
X Description: https://docs.python.org/3/library/logging.html#logrecord-objects gives the following pattern for adding contextual information to LogRecord objects which can then be used in log formats: old_factor...
Opengraph URL: https://github.com/python/typeshed/issues/7833
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Documented pattern for adding contextual information to LogRecord objects fails type checking","articleBody":"https://docs.python.org/3/library/logging.html#logrecord-objects gives the following pattern for adding contextual information to LogRecord objects which can then be used in log formats:\r\n\r\n```python\r\nold_factory = logging.getLogRecordFactory()\r\n\r\ndef record_factory(*args, **kwargs):\r\n record = old_factory(*args, **kwargs)\r\n record.custom_attribute = 0xdecafbad\r\n return record\r\n\r\nlogging.setLogRecordFactory(record_factory)\r\n```\r\n\r\nHowever, if you use this pattern and run mypy, you'll get an error like `error: \"LogRecord\" has no attribute \"custom_attribute\"`. You can get around this error by using `setattr(record, \"custom_attribute\", 0xdecafbad)` instead of `record.custom_attribute = 0xdecafbad`.\r\n\r\nShould the stub for the `LogRecord` class define a `__setattr__` method because this sort of usage of assigning to arbitrary attributes is expected for the `LogRecord` class? Would that have undesirable side effects?\r\n\r\nHere is a complete example:\r\n\r\n```python\r\nimport logging\r\n\r\ncustom_contextual_data = \"XYZ\"\r\n\r\n\r\ndef main() -\u003e None:\r\n logging.basicConfig(\r\n format=\"[%(asctime)s] [%(custom_contextual_data)s] [%(name)s] %(levelname)s: %(message)s\", level=logging.DEBUG\r\n )\r\n\r\n old_factory = logging.getLogRecordFactory()\r\n\r\n def record_factory(*args: object, **kwargs: object) -\u003e logging.LogRecord:\r\n global custom_contextual_data\r\n record = old_factory(*args, **kwargs)\r\n\r\n # mypy: error: \"LogRecord\" has no attribute \"custom_contextual_data\"\r\n record.custom_contextual_data = custom_contextual_data\r\n\r\n # mypy: no error\r\n # setattr(record, \"custom_contextual_data\", custom_contextual_data)\r\n\r\n return record\r\n\r\n logging.setLogRecordFactory(record_factory)\r\n\r\n logger = logging.getLogger(__name__)\r\n logger.info(\"Hello XYZ world!\")\r\n global custom_contextual_data\r\n custom_contextual_data = \"ABC\"\r\n logger.info(\"Hello ABC world!\")\r\n\r\n\r\nif __name__ == \"__main__\":\r\n main()\r\n```\r\n\r\nmypy.ini:\r\n\r\n```\r\n[mypy]\r\nfiles = mypy_log_record_repro/\r\nstrict_equality = True\r\ndisallow_untyped_defs = True\r\ndisallow_untyped_calls = True\r\ndisallow_untyped_decorators = True\r\nno_implicit_optional = True\r\nstrict_optional = True\r\nwarn_unused_ignores = True\r\nwarn_redundant_casts = True\r\nwarn_no_return = True\r\nwarn_return_any = True\r\nwarn_unreachable = True\r\n```","author":{"url":"https://github.com/LHCGreg","@type":"Person","name":"LHCGreg"},"datePublished":"2022-05-12T15:31:29.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/7833/typeshed/issues/7833"}
| 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:228e34fc-d36d-b08a-883a-00e8385afd7e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A0AC:2867:3A9E26A:50EB946:6A60EC25 |
| html-safe-nonce | 29bfe2d92ee51e2be3d688f0229ca95bb417a090b35375998b41b2b443c09559 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMEFDOjI4Njc6M0E5RTI2QTo1MEVCOTQ2OjZBNjBFQzI1IiwidmlzaXRvcl9pZCI6Ijg0NzAxMTYyMjAzODU5NDY2NjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8fa861de980eead6de6c3a5b99df681eacf570cb3b8fc22bb77e3f3da6e0e4b6 |
| hovercard-subject-tag | issue:1234161996 |
| 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/typeshed/7833/issue_layout |
| twitter:image | https://opengraph.githubassets.com/a6d3e2dd3c8a7a6e6956d06371700ea2e573f94d37cb5c201150d72d7a9b8bb4/python/typeshed/issues/7833 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/a6d3e2dd3c8a7a6e6956d06371700ea2e573f94d37cb5c201150d72d7a9b8bb4/python/typeshed/issues/7833 |
| og:image:alt | https://docs.python.org/3/library/logging.html#logrecord-objects gives the following pattern for adding contextual information to LogRecord objects which can then be used in log formats: old_factor... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | LHCGreg |
| hostname | github.com |
| expected-hostname | github.com |
| None | 3ba36a464b9464992131f4003292a219fc87f92404b0dbe3fd1ef07f11102d0f |
| turbo-cache-control | no-preview |
| go-import | github.com/python/typeshed git https://github.com/python/typeshed.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 31696383 |
| octolytics-dimension-repository_nwo | python/typeshed |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31696383 |
| octolytics-dimension-repository_network_root_nwo | python/typeshed |
| 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 | 2cc5e4f897a27632dd600edfec4c737bea2f8338 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width