René's URL Explorer Experiment


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

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:228e34fc-d36d-b08a-883a-00e8385afd7e
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-idA0AC:2867:3A9E26A:50EB946:6A60EC25
html-safe-nonce29bfe2d92ee51e2be3d688f0229ca95bb417a090b35375998b41b2b443c09559
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMEFDOjI4Njc6M0E5RTI2QTo1MEVCOTQ2OjZBNjBFQzI1IiwidmlzaXRvcl9pZCI6Ijg0NzAxMTYyMjAzODU5NDY2NjEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmac8fa861de980eead6de6c3a5b99df681eacf570cb3b8fc22bb77e3f3da6e0e4b6
hovercard-subject-tagissue:1234161996
github-keyboard-shortcutsrepository,issues,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///voltron/issues_fragments/issue_layout
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/typeshed/7833/issue_layout
twitter:imagehttps://opengraph.githubassets.com/a6d3e2dd3c8a7a6e6956d06371700ea2e573f94d37cb5c201150d72d7a9b8bb4/python/typeshed/issues/7833
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/a6d3e2dd3c8a7a6e6956d06371700ea2e573f94d37cb5c201150d72d7a9b8bb4/python/typeshed/issues/7833
og:image:althttps://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:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameLHCGreg
hostnamegithub.com
expected-hostnamegithub.com
None3ba36a464b9464992131f4003292a219fc87f92404b0dbe3fd1ef07f11102d0f
turbo-cache-controlno-preview
go-importgithub.com/python/typeshed git https://github.com/python/typeshed.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id31696383
octolytics-dimension-repository_nwopython/typeshed
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id31696383
octolytics-dimension-repository_network_root_nwopython/typeshed
turbo-body-classeslogged-out env-production page-responsive
disable-turbofalse
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
release2cc5e4f897a27632dd600edfec4c737bea2f8338
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/python/typeshed/issues/7833#start-of-content
https://github.com/
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F7833
GitHub CopilotWrite better code with AIhttps://github.com/features/copilot
GitHub Copilot appDirect agents from issue to mergehttps://github.com/features/ai/github-app
MCP RegistryNewIntegrate external toolshttps://github.com/mcp
ActionsAutomate any workflowhttps://github.com/features/actions
CodespacesInstant dev environmentshttps://github.com/features/codespaces
IssuesPlan and track workhttps://github.com/features/issues
Code ReviewManage code changeshttps://github.com/features/code-review
Code QualityEnforce quality at mergehttps://github.com/features/code-quality
GitHub Advanced SecurityFind and fix vulnerabilitieshttps://github.com/security/advanced-security
Code securitySecure your code as you buildhttps://github.com/security/advanced-security/code-security
Secret protectionStop leaks before they starthttps://github.com/security/advanced-security/secret-protection
Why GitHubhttps://github.com/why-github
Documentationhttps://docs.github.com
Bloghttps://github.blog
Changeloghttps://github.blog/changelog
Marketplacehttps://github.com/marketplace
View all featureshttps://github.com/features
Enterpriseshttps://github.com/enterprise
Small and medium teamshttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
App Modernizationhttps://github.com/solutions/use-case/app-modernization
DevSecOpshttps://github.com/solutions/use-case/devsecops
DevOpshttps://github.com/solutions/use-case/devops
CI/CDhttps://github.com/solutions/use-case/ci-cd
View all use caseshttps://github.com/solutions/use-case
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
View all industrieshttps://github.com/solutions/industry
View all solutionshttps://github.com/solutions
AIhttps://github.com/resources/articles?topic=ai
Software Developmenthttps://github.com/resources/articles?topic=software-development
DevOpshttps://github.com/resources/articles?topic=devops
Securityhttps://github.com/resources/articles?topic=security
View all topicshttps://github.com/resources/articles
Customer storieshttps://github.com/customer-stories
Events & webinarshttps://github.com/resources/events
Ebooks & reportshttps://github.com/resources/whitepapers
Business insightshttps://github.com/solutions/executive-insights
GitHub Skillshttps://skills.github.com
Documentationhttps://docs.github.com
Customer supporthttps://support.github.com
Community forumhttps://github.com/orgs/community/discussions
Trust centerhttps://github.com/trust-center
Partnershttps://github.com/partners
View all resourceshttps://github.com/resources
GitHub SponsorsFund open source developershttps://github.com/open-source/sponsors
Security Labhttps://securitylab.github.com
Maintainer Communityhttps://maintainers.github.com
Acceleratorhttps://github.com/open-source/accelerator
GitHub Starshttps://stars.github.com
Archive Programhttps://archiveprogram.github.com
Topicshttps://github.com/topics
Trendinghttps://github.com/trending
Collectionshttps://github.com/collections
Enterprise platformAI-powered developer platformhttps://github.com/enterprise
GitHub Advanced SecurityEnterprise-grade security featureshttps://github.com/security/advanced-security
Copilot for BusinessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Pricinghttps://github.com/pricing
Search syntax tipshttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
documentationhttps://docs.github.com/search-github/github-code-search/understanding-github-code-search-syntax
Sign in https://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Ftypeshed%2Fissues%2F7833
Sign up https://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fvoltron%2Fissues_fragments%2Fissue_layout&source=header-repo&source_repo=python%2Ftypeshed
Reloadhttps://github.com/python/typeshed/issues/7833
Reloadhttps://github.com/python/typeshed/issues/7833
Reloadhttps://github.com/python/typeshed/issues/7833
Please reload this pagehttps://github.com/python/typeshed/issues/7833
python https://github.com/python
typeshedhttps://github.com/python/typeshed
Please reload this pagehttps://github.com/python/typeshed/issues/7833
Notifications https://github.com/login?return_to=%2Fpython%2Ftypeshed
Fork 2.1k https://github.com/login?return_to=%2Fpython%2Ftypeshed
Star 5.1k https://github.com/login?return_to=%2Fpython%2Ftypeshed
Code https://github.com/python/typeshed
Issues 220 https://github.com/python/typeshed/issues
Pull requests 146 https://github.com/python/typeshed/pulls
Actions https://github.com/python/typeshed/actions
Security and quality 0 https://github.com/python/typeshed/security
Insights https://github.com/python/typeshed/pulse
Code https://github.com/python/typeshed
Issues https://github.com/python/typeshed/issues
Pull requests https://github.com/python/typeshed/pulls
Actions https://github.com/python/typeshed/actions
Security and quality https://github.com/python/typeshed/security
Insights https://github.com/python/typeshed/pulse
#8064https://github.com/python/typeshed/pull/8064
Documented pattern for adding contextual information to LogRecord objects fails type checkinghttps://github.com/python/typeshed/issues/7833#top
#8064https://github.com/python/typeshed/pull/8064
https://github.com/LHCGreg
LHCGreghttps://github.com/LHCGreg
on May 12, 2022https://github.com/python/typeshed/issues/7833#issue-1234161996
https://docs.python.org/3/library/logging.html#logrecord-objectshttps://docs.python.org/3/library/logging.html#logrecord-objects
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.