René's URL Explorer Experiment


Title: get_attribute_hook does not trigger for class attributes, get_base_class_hook cannot resolve class attr types · Issue #9645 · python/mypy · GitHub

Open Graph Title: get_attribute_hook does not trigger for class attributes, get_base_class_hook cannot resolve class attr types · Issue #9645 · python/mypy

X Title: get_attribute_hook does not trigger for class attributes, get_base_class_hook cannot resolve class attr types · Issue #9645 · python/mypy

Description: Bug Report I wasn't sure how to best phrase title and/or description for this, so edits/suggestions/renames are welcomed. The get_attribute_hook plugin hook is not triggered for attributes/variables on classes. It seems to only trigger f...

Open Graph Description: Bug Report I wasn't sure how to best phrase title and/or description for this, so edits/suggestions/renames are welcomed. The get_attribute_hook plugin hook is not triggered for attributes/variable...

X Description: Bug Report I wasn't sure how to best phrase title and/or description for this, so edits/suggestions/renames are welcomed. The get_attribute_hook plugin hook is not triggered for attributes/vari...

Opengraph URL: https://github.com/python/mypy/issues/9645

X: @github

direct link

Domain: github.com


Hey, it has json ld scripts:
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"get_attribute_hook does not trigger for class attributes, get_base_class_hook cannot resolve class attr types","articleBody":"**Bug Report**\r\n\r\nI wasn't sure how to best phrase title and/or description for this, so edits/suggestions/renames are welcomed.\r\n\r\nThe `get_attribute_hook` plugin hook is not triggered for attributes/variables on classes. It seems to only trigger for attributes/variables on instances. Additionally, I seem to be unable to resolve types of class variables in the hook return by `get_base_class_hook`.\r\n\r\n**To Reproduce**\r\n\r\nGiven the source file\r\n\r\n```python\r\nfrom typing import Dict, TYPE_CHECKING, Tuple, cast\r\n\r\n\r\nclass Field:\r\n    pass\r\n\r\n\r\nclass FieldBuilder:\r\n    def with_name(self) -> 'FieldBuilder':\r\n        return self\r\n\r\n    # Imagine arbitrarily more builder methods\r\n\r\n    def build(self) -> Field:\r\n        return Field()\r\n\r\n\r\nclass BaseMeta(type):\r\n    def __new__(mcs, name: str, bases: Tuple[type, ...], attrs: Dict[str, object]) -> 'BaseMeta':\r\n        for key, val in attrs.items():\r\n            if isinstance(val, FieldBuilder):\r\n                attrs[key] = val.build()\r\n        return cast(BaseMeta, type.__new__(mcs, name, bases, attrs))\r\n\r\n\r\nclass Base(metaclass=BaseMeta):\r\n    pass\r\n\r\n\r\nclass Concrete(Base):\r\n    foo = FieldBuilder().with_name()  # Can have arbitrary chain of builder methods\r\n    bar = 1\r\n\r\n\r\nif TYPE_CHECKING:\r\n    reveal_type(Concrete.foo)  # Would like this to show up as type \"Field\"\r\n    reveal_type(Concrete.bar)  # Would like this to show up as type \"int\"\r\nprint(type(Concrete.foo))\r\nprint(type(Concrete.bar))\r\n```\r\n\r\nand the plugin\r\n\r\n```python\r\nfrom typing import Callable, Optional\r\nfrom typing import Type as TypingType\r\n\r\nfrom mypy.plugin import AttributeContext, ClassDefContext, Plugin\r\nfrom mypy.types import Type as MypyType\r\n\r\n\r\nclass ExamplePlugin(Plugin):\r\n    def get_attribute_hook(self, fullname: str) -> Optional[Callable[[AttributeContext], MypyType]]:\r\n        print('ATTRIBUTE HOOK FOR', fullname)\r\n        return None\r\n\r\n    def get_base_class_hook(self, fullname: str) -> Optional[Callable[[ClassDefContext], None]]:\r\n        if not fullname.endswith('.Base'):\r\n            return None\r\n\r\n        def hook(ctx: ClassDefContext) -> None:\r\n            print('BASE CLASS HOOK FOR', fullname, ctx.cls.fullname)\r\n            if ctx.cls.info.names['foo'].type is None:\r\n                if ctx.api.final_iteration:\r\n                    print('CANNOT DEFER ANY MORE', fullname)\r\n                else:\r\n                    print('DEFERRING', fullname)\r\n                    ctx.api.defer()\r\n\r\n        return hook\r\n\r\ndef plugin(_mypy_info: str) -> TypingType[Plugin]:\r\n    return ExamplePlugin\r\n```\r\n\r\n**Expected Behavior**\r\n\r\nI would expect that an `ATTRIBUTE HOOK FOR` line would be printed for the `Concrete.foo` and `Concrete.bar` accesses. Alternatively, I'd expect to be able to eventually get a non-`None` type for the `foo` attribute in the base class hook.\r\n\r\n**Actual Behavior**\r\n\r\nThe `ATTRIBUTE HOOK FOR` is never printed for the `Concrete.*` accesses and we never get a non-`None` type for `foo` (we reach the final iteration).\r\n\r\nIf there's a way to accomplish the `Would like this to show up as type` comments with the existing plugin hook behaviors, that also works for me! Note, I've seen [other plugins](https://github.com/dropbox/sqlalchemy-stubs/blob/55470ceab8149db983411d5c094c9fe16343c58b/sqlmypy.py#L79-L91) use the `get_function_hook` to modify the return type and therefore the type of the class var, but I didn't see a way to know that the result of the function would be stored in a class var, since I wouldn't want *every* builder method to be automatically converted.\r\n\r\n**Your Environment**\r\n\r\n- Mypy version used: `mypy 0.800+dev.f220ce50725ed7f117832b9f03aed4d1a9508e00`\r\n- Mypy command-line flags: `--show-traceback --warn-unused-configs --config-file \u003cfile>`\r\n- Mypy configuration options from `mypy.ini` (and other config files):\r\n```ini\r\n[mypy]\r\nplugins = \u003cmy plugin>\r\nstrict = True\r\n```\r\n- Python version used: `3.7.6`\r\n- Operating system and version: macOS 10.15.7\r\n","author":{"url":"https://github.com/FuegoFro","@type":"Person","name":"FuegoFro"},"datePublished":"2020-10-26T20:18:40Z","url":"https://github.com/python/mypy/issues/9645"}

react-profiling0
react-import-mapreact,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-controllervoltron_issues_fragments
route-actionissue_layout
fetch-noncev2:d3e5a0ed-c330-c596-ec48-6c5bd1f3901f
current-catalog-service-hash81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114
request-id9D3A:1C3DC0:447F13:5C0F35:6B002C4C
html-safe-nonce4f1705d511bca6a9a3cd246f196882356364ce08160053296828e4f8afc082de
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDNBOjFDM0RDMDo0NDdGMTM6NUMwRjM1OjZCMDAyQzRDIiwidmlzaXRvcl9pZCI6IjY0NzU5Mzc5NzI1Nzg0MzAwMjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ==
visitor-hmacbd4002369bbdc9714454bb67340373d2a181f8c27e5a09632751e55284ac2738
hovercard-subject-tagissue:729875615
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/mypy/9645/issue_layout
twitter:imagehttps://opengraph.githubassets.com/19b3cbf3f958f255768d8911f2805bf789605ac8c6def9fe18489dbf43fcdde5/python/mypy/issues/9645
twitter:cardsummary_large_image
og:imagehttps://opengraph.githubassets.com/19b3cbf3f958f255768d8911f2805bf789605ac8c6def9fe18489dbf43fcdde5/python/mypy/issues/9645
og:image:altBug Report I wasn't sure how to best phrase title and/or description for this, so edits/suggestions/renames are welcomed. The get_attribute_hook plugin hook is not triggered for attributes/variable...
og:image:width1200
og:image:height600
og:site_nameGitHub
og:typeobject
og:author:usernameFuegoFro
hostnamegithub.com
expected-hostnamegithub.com
None7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0
turbo-cache-controlno-preview
go-importgithub.com/python/mypy git https://github.com/python/mypy.git
octolytics-dimension-user_id1525981
octolytics-dimension-user_loginpython
octolytics-dimension-repository_id7053637
octolytics-dimension-repository_nwopython/mypy
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id7053637
octolytics-dimension-repository_network_root_nwopython/mypy
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
releasef3f3a43ca095cdea3abbb394112e971f9fdb94fa
ui-targetfull
theme-color#1e2327
color-schemelight dark
react-app-nameissues

Links:

Skip to contenthttps://github.com/python/mypy/issues/9645#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F9645
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
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
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://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
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fpython%2Fmypy%2Fissues%2F9645
Sign uphttps://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%2Fmypy
Reloadhttps://github.com/python/mypy/issues/9645
Reloadhttps://github.com/python/mypy/issues/9645
Reloadhttps://github.com/python/mypy/issues/9645
pythonhttps://github.com/python
mypyhttps://github.com/python/mypy
Codehttps://github.com/python/mypy
Issues2.7k (2.7k)https://github.com/python/mypy/issues
Pull requests547 (547)https://github.com/python/mypy/pulls
Actionshttps://github.com/python/mypy/actions
Projectshttps://github.com/python/mypy/projects
Wikihttps://github.com/python/mypy/wiki
Security and qualityhttps://github.com/python/mypy/security
Insightshttps://github.com/python/mypy/insights
https://github.com/python/mypy
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/9645
New issuehttps://github.com/login?return_to=https://github.com/python/mypy/issues/9645
#9881https://github.com/python/mypy/pull/9881
get_attribute_hook does not trigger for class attributes, get_base_class_hook cannot resolve class attr typeshttps://github.com/python/mypy/issues/9645#top
#9881https://github.com/python/mypy/pull/9881
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
https://github.com/FuegoFro
FuegoFrohttps://github.com/FuegoFro
on Oct 26, 2020https://github.com/python/mypy/issues/9645#issue-729875615
other pluginshttps://github.com/dropbox/sqlalchemy-stubs/blob/55470ceab8149db983411d5c094c9fe16343c58b/sqlmypy.py#L79-L91
FuegoFrohttps://github.com/FuegoFro
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
on Oct 26, 2020https://github.com/python/mypy/issues/9645#event-3922645976
FuegoFrohttps://github.com/FuegoFro
on Jan 5, 2021https://github.com/python/mypy/issues/9645#event-4172136684
Add a class attribute hook to the plugin systemhttps://github.com/FuegoFro/mypy/commit/cd7fb7258d73116834bfa0ffeab68b5856c044ec
cd7fb72https://github.com/FuegoFro/mypy/commit/cd7fb7258d73116834bfa0ffeab68b5856c044ec
FuegoFrohttps://github.com/FuegoFro
on Mar 9, 2021https://github.com/python/mypy/issues/9645#event-4430605019
Add a class attribute hook to the plugin systemhttps://github.com/FuegoFro/mypy/commit/3b344c954a9ae30fc346c526f7162d65c974f031
3b344c9https://github.com/FuegoFro/mypy/commit/3b344c954a9ae30fc346c526f7162d65c974f031
FuegoFrohttps://github.com/FuegoFro
on Apr 5, 2021https://github.com/python/mypy/issues/9645#event-4553816374
Add a class attribute hook to the plugin systemhttps://github.com/FuegoFro/mypy/commit/8520dc56ec6b51161b3886c5274f8a0569893c28
8520dc5https://github.com/FuegoFro/mypy/commit/8520dc56ec6b51161b3886c5274f8a0569893c28
FuegoFrohttps://github.com/FuegoFro
on May 19, 2021https://github.com/python/mypy/issues/9645#event-4768065366
Add a class attribute hook to the plugin systemhttps://github.com/FuegoFro/mypy/commit/d9ee4b1d087e71da1fafc442e842e1e20ec7b7d4
d9ee4b1https://github.com/FuegoFro/mypy/commit/d9ee4b1d087e71da1fafc442e842e1e20ec7b7d4
denbeigh2000https://github.com/denbeigh2000
on Jul 28, 2021https://github.com/python/mypy/issues/9645#event-5081259638
Add a class attribute hook to the plugin systemhttps://github.com/denbeigh2000/mypy/commit/a74285660f828ff98b8e033d4ae91331b6281d13
a742856https://github.com/denbeigh2000/mypy/commit/a74285660f828ff98b8e033d4ae91331b6281d13
Add a class attribute hook to the plugin systemhttps://github.com/denbeigh2000/mypy/commit/6a9b8821aa2fca5ab86621d4c03cb8b9733ceb45
6a9b882https://github.com/denbeigh2000/mypy/commit/6a9b8821aa2fca5ab86621d4c03cb8b9733ceb45
Add a class attribute hook to the plugin systemhttps://github.com/denbeigh2000/mypy/commit/791106a034612d10dbab8bd9fce86b79d6e27e6b
791106ahttps://github.com/denbeigh2000/mypy/commit/791106a034612d10dbab8bd9fce86b79d6e27e6b
FuegoFrohttps://github.com/FuegoFro
on Aug 4, 2021https://github.com/python/mypy/issues/9645#event-5112458031
Add a class attribute hook to the plugin systemhttps://github.com/FuegoFro/mypy/commit/23aeeb04b5456cb6389a2e96fffd45716ef3190f
23aeeb0https://github.com/FuegoFro/mypy/commit/23aeeb04b5456cb6389a2e96fffd45716ef3190f
denbeigh2000https://github.com/denbeigh2000
on Aug 11, 2021https://github.com/python/mypy/issues/9645#event-5138519663
Add a class attribute hook to the plugin systemhttps://github.com/denbeigh2000/mypy/commit/e35d7de2a981e8a50d834ea7a7e65e3a49ceb10f
e35d7dehttps://github.com/denbeigh2000/mypy/commit/e35d7de2a981e8a50d834ea7a7e65e3a49ceb10f
ddfisherhttps://github.com/ddfisher
on Nov 19, 2021https://github.com/python/mypy/issues/9645#event-5647447827
Add a class attribute hook to the plugin systemhttps://github.com/discord/mypy/commit/ff6efba3f02890dcde9d9fe5fdeff9a974fe00ba
ff6efbahttps://github.com/discord/mypy/commit/ff6efba3f02890dcde9d9fe5fdeff9a974fe00ba
hauntsaninjahttps://github.com/hauntsaninja
completedhttps://github.com/python/mypy/issues?q=is%3Aissue%20state%3Aclosed%20archived%3Afalse%20reason%3Acompleted
#9881https://github.com/python/mypy/pull/9881
on May 7, 2022https://github.com/python/mypy/issues/9645#event-6567978919
hauntsaninjahttps://github.com/hauntsaninja
on May 7, 2022https://github.com/python/mypy/issues/9645#event-6567978949
Add a class attribute hook to the plugin system (#9881)https://github.com/python/mypy/commit/23e2a51e05ce38aa765fe67ab1507836227b301b
#9881https://github.com/python/mypy/pull/9881
https://docs.github.com/articles/closing-issues-via-commit-messages
23e2a51https://github.com/python/mypy/commit/23e2a51e05ce38aa765fe67ab1507836227b301b
Sign up for freehttps://github.com/signup?return_to=https://github.com/python/mypy/issues/9645
Sign in to commenthttps://github.com/login?return_to=https://github.com/python/mypy/issues/9645
bugmypy got something wronghttps://github.com/python/mypy/issues?q=state%3Aopen%20label%3A%22bug%22
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.