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
Domain: github.com
{"@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-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:d3e5a0ed-c330-c596-ec48-6c5bd1f3901f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9D3A:1C3DC0:447F13:5C0F35:6B002C4C |
| html-safe-nonce | 4f1705d511bca6a9a3cd246f196882356364ce08160053296828e4f8afc082de |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5RDNBOjFDM0RDMDo0NDdGMTM6NUMwRjM1OjZCMDAyQzRDIiwidmlzaXRvcl9pZCI6IjY0NzU5Mzc5NzI1Nzg0MzAwMjgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | bd4002369bbdc9714454bb67340373d2a181f8c27e5a09632751e55284ac2738 |
| hovercard-subject-tag | issue:729875615 |
| 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/mypy/9645/issue_layout |
| twitter:image | https://opengraph.githubassets.com/19b3cbf3f958f255768d8911f2805bf789605ac8c6def9fe18489dbf43fcdde5/python/mypy/issues/9645 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/19b3cbf3f958f255768d8911f2805bf789605ac8c6def9fe18489dbf43fcdde5/python/mypy/issues/9645 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | FuegoFro |
| hostname | github.com |
| expected-hostname | github.com |
| None | 7d0829bbde9e48b2486cb5dd6d147896eb01d32134627091f818958a935ef3f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/mypy git https://github.com/python/mypy.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 7053637 |
| octolytics-dimension-repository_nwo | python/mypy |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 7053637 |
| octolytics-dimension-repository_network_root_nwo | python/mypy |
| 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 | f3f3a43ca095cdea3abbb394112e971f9fdb94fa |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width