Title: Inferencing type reference of abstract class inferred from concrete type values · Issue #8050 · python/mypy · GitHub
Open Graph Title: Inferencing type reference of abstract class inferred from concrete type values · Issue #8050 · python/mypy
X Title: Inferencing type reference of abstract class inferred from concrete type values · Issue #8050 · python/mypy
Description: import abc class MyAbstractType(metaclass=abc.ABCMeta): @abc.abstractmethod def do(self): pass class MyConcreteA(MyAbstractType): def do(self): print('A') class MyConcreteB(MyAbstractType): def do(self): print('B') my_types = { 'A': MyCo...
Open Graph Description: import abc class MyAbstractType(metaclass=abc.ABCMeta): @abc.abstractmethod def do(self): pass class MyConcreteA(MyAbstractType): def do(self): print('A') class MyConcreteB(MyAbstractType): def do(...
X Description: import abc class MyAbstractType(metaclass=abc.ABCMeta): @abc.abstractmethod def do(self): pass class MyConcreteA(MyAbstractType): def do(self): print('A') class MyConcreteB(MyAbstractType):...
Opengraph URL: https://github.com/python/mypy/issues/8050
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Inferencing type reference of abstract class inferred from concrete type values","articleBody":"```python3\r\nimport abc\r\n\r\nclass MyAbstractType(metaclass=abc.ABCMeta):\r\n @abc.abstractmethod\r\n def do(self): pass\r\n\r\nclass MyConcreteA(MyAbstractType):\r\n def do(self):\r\n print('A')\r\n\r\nclass MyConcreteB(MyAbstractType):\r\n def do(self):\r\n print('B')\r\n\r\nmy_types = {\r\n 'A': MyConcreteA,\r\n 'B': MyConcreteB,\r\n}\r\nreveal_type(my_types) # Revealed type is 'builtins.dict[builtins.str*, def () -> test-abc.MyAbstractType]'\r\n\r\na = my_types['A']() # Cannot instantiate abstract class 'MyAbstractType' with abstract attribute 'do'\r\na.do()\r\nb = my_types['B']() # Cannot instantiate abstract class 'MyAbstractType' with abstract attribute 'do'\r\nb.do()\r\n```\r\nThe above example does not pass the type check as-is.\r\nI need to change it to pass the type check as follows:\r\n\r\n```python3\r\nimport abc\r\nfrom typing import Mapping, Type\r\n\r\nclass MyAbstractType(metaclass=abc.ABCMeta):\r\n @abc.abstractmethod\r\n def do(self): pass\r\n\r\nclass MyConcreteA(MyAbstractType):\r\n def do(self):\r\n print('A')\r\n\r\nclass MyConcreteB(MyAbstractType):\r\n def do(self):\r\n print('B')\r\n\r\nmy_types: Mapping[str, Type[MyAbstractType]] = { # explicit annotation\r\n 'A': MyConcreteA,\r\n 'B': MyConcreteB,\r\n}\r\nreveal_type(my_types) # Revealed type is 'typing.Mapping[builtins.str, Type[test-abc.MyAbstractType]]'\r\n\r\na = my_types['A']()\r\na.do()\r\nb = my_types['B']()\r\nb.do()\r\n```\r\n\r\nSince mypy is able to infer `MyAbstractType` from the dictionary in the first snippet, I believe that it should be also able to infer `Type[MyAbstractType]` since the dictionary values are not instances but class names.\r\n\r\nMy setup: Python 3.8.0 + mypy 0.750\r\n\r\nRelated issues/PRs:\r\n - #5135 looks like a similar issue as the author did not use explicit annotation for the dictionaries.\r\n - #2853 allowed using explicit annotation above.","author":{"url":"https://github.com/achimnol","@type":"Person","name":"achimnol"},"datePublished":"2019-12-02T04:54:42Z","url":"https://github.com/python/mypy/issues/8050"}
| 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:abaf27c2-6e51-a523-f8af-79cf23240bd1 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B288:2261EB:76E54D:99D041:6AFFCDB3 |
| html-safe-nonce | 00256c799130cf89e55f4b35cf881ff695d617256d0c820954c18928502a0539 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCMjg4OjIyNjFFQjo3NkU1NEQ6OTlEMDQxOjZBRkZDREIzIiwidmlzaXRvcl9pZCI6IjM2NTA0NjA5NjYzMDQzMzczMzEiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 689e72b1ae8a6c3e232a311b9949019a65a2aa651336e081b7a69a5a83566a94 |
| hovercard-subject-tag | issue:530889131 |
| 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/8050/issue_layout |
| twitter:image | https://opengraph.githubassets.com/25ae06797b89148f0abe90330b06612f4703bd95558194d839f02a86ccd42f1b/python/mypy/issues/8050 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/25ae06797b89148f0abe90330b06612f4703bd95558194d839f02a86ccd42f1b/python/mypy/issues/8050 |
| og:image:alt | import abc class MyAbstractType(metaclass=abc.ABCMeta): @abc.abstractmethod def do(self): pass class MyConcreteA(MyAbstractType): def do(self): print('A') class MyConcreteB(MyAbstractType): def do(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | achimnol |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width