Title: `__slots__` requires explicit type definition and must be `Any` (and nothing else) when used with `Protocol`s · Issue #11013 · python/mypy · GitHub
Open Graph Title: `__slots__` requires explicit type definition and must be `Any` (and nothing else) when used with `Protocol`s · Issue #11013 · python/mypy
X Title: `__slots__` requires explicit type definition and must be `Any` (and nothing else) when used with `Protocol`s · Issue #11013 · python/mypy
Description: # test_case1.py from __future__ import annotations from typing import Protocol, runtime_checkable @runtime_checkable class Foo(Protocol): __slots__ = () def foo(self): ... class Bar: __slots__ = ("_t",) def __init__(self): self._t = True...
Open Graph Description: # test_case1.py from __future__ import annotations from typing import Protocol, runtime_checkable @runtime_checkable class Foo(Protocol): __slots__ = () def foo(self): ... class Bar: __slots__ = ("...
X Description: # test_case1.py from __future__ import annotations from typing import Protocol, runtime_checkable @runtime_checkable class Foo(Protocol): __slots__ = () def foo(self): ... class Bar: __slots__ = (&...
Opengraph URL: https://github.com/python/mypy/issues/11013
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`__slots__` requires explicit type definition and must be `Any` (and nothing else) when used with `Protocol`s","articleBody":"``` python\r\n# test_case1.py\r\nfrom __future__ import annotations\r\nfrom typing import Protocol, runtime_checkable\r\n\r\n@runtime_checkable\r\nclass Foo(Protocol):\r\n __slots__ = ()\r\n def foo(self):\r\n ...\r\n\r\nclass Bar:\r\n __slots__ = (\"_t\",)\r\n def __init__(self):\r\n self._t = True\r\n def foo(self):\r\n pass\r\n\r\nprint(f\"isinstance(Bar(), Foo) == {isinstance(Bar(), Foo)}\") # \u003c- prints … == True\r\nfoo: Foo = Bar() # \u003c- errors here\r\n```\r\n\r\n```\r\n% python test_case1.py\r\nisinstance(Bar(), Foo) == True\r\n% mypy --version\r\nmypy 0.910\r\n% mypy --config-file=/dev/null test_case1.py\r\n/dev/null: No [mypy] section in config file\r\ntest_case1.py:19: error: Incompatible types in assignment (expression has type \"Bar\", variable has type \"Foo\")\r\ntest_case1.py:19: note: Following member(s) of \"Bar\" have conflicts:\r\ntest_case1.py:19: note: __slots__: expected \"Tuple[]\", got \"Tuple[str]\"\r\n```\r\n\r\nThe \"fix\" is to provide `Tuple[str, ...]` as the type for `__slots__`:\r\n\r\n``` python\r\n# test_case2.py\r\nfrom __future__ import annotations\r\nfrom typing import Protocol, Tuple, runtime_checkable\r\n\r\n@runtime_checkable\r\nclass Foo(Protocol):\r\n __slots__: Tuple[str, ...] = ()\r\n def foo(self):\r\n ...\r\n\r\nclass Bar:\r\n __slots__ : Tuple[str, ...] = (\"_t\",)\r\n def __init__(self):\r\n self._t = True\r\n def foo(self):\r\n pass\r\n\r\nprint(f\"isinstance(Bar(), Foo) == {isinstance(Bar(), Foo)}\") # still True\r\nfoo: Foo = Bar() # \u003c- works now?\r\n```\r\n\r\n```\r\n% mypy --config-file=/dev/null test_case2.py\r\n/dev/null: No [mypy] section in config file\r\nSuccess: no issues found in 1 source file\r\n```\r\n\r\nShouldn't mypy already know the type of `__slots__`?","author":{"url":"https://github.com/posita","@type":"Person","name":"posita"},"datePublished":"2021-08-24T04:07:47Z","url":"https://github.com/python/mypy/issues/11013"}
| 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:a644803b-c705-0d90-3102-ec4bd098074b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D76A:C5FC2:21F2F7F:2CE27B1:6B004E3E |
| html-safe-nonce | e41e46b1d4f0d9a350ac1026434b1e7c588596ae3e905dbcc97fa33feff99f4e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzZBOkM1RkMyOjIxRjJGN0Y6MkNFMjdCMTo2QjAwNEUzRSIsInZpc2l0b3JfaWQiOiIxOTU1NTI5MTAxNjM2ODE2NDQ2IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 313cbfdc798c22c707b7748038d11e1d0c5c36353ac9051357bcee3978f89ae7 |
| hovercard-subject-tag | issue:977681471 |
| 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/11013/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0e1824b94f4c68a0d03dab157430318dbb14ab0bd8396855a1c111440247b8f6/python/mypy/issues/11013 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0e1824b94f4c68a0d03dab157430318dbb14ab0bd8396855a1c111440247b8f6/python/mypy/issues/11013 |
| og:image:alt | # test_case1.py from __future__ import annotations from typing import Protocol, runtime_checkable @runtime_checkable class Foo(Protocol): __slots__ = () def foo(self): ... class Bar: __slots__ = ("... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | posita |
| hostname | github.com |
| expected-hostname | github.com |
| None | f9517bf360d46638a1e5e7daee7158842fe5ef43c9000afa0e127564a7c2f39b |
| 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 | b27c5dde30676028abd5c5fc53ce5c9b625f8281 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width