Title: Type declarations of base class attributes not carried over to subclasses if `None` is present · Issue #2022 · python/mypy · GitHub
Open Graph Title: Type declarations of base class attributes not carried over to subclasses if `None` is present · Issue #2022 · python/mypy
X Title: Type declarations of base class attributes not carried over to subclasses if `None` is present · Issue #2022 · python/mypy
Description: Simplest repro: class BaseClass: my_attr = () # type: tuple class Subclass(BaseClass): my_attr = None Output: testing.py: note: In class "Subclass": testing.py:5: error: Need type annotation for variable However, if None isn't the sole v...
Open Graph Description: Simplest repro: class BaseClass: my_attr = () # type: tuple class Subclass(BaseClass): my_attr = None Output: testing.py: note: In class "Subclass": testing.py:5: error: Need type annotation for va...
X Description: Simplest repro: class BaseClass: my_attr = () # type: tuple class Subclass(BaseClass): my_attr = None Output: testing.py: note: In class "Subclass": testing.py:5: error: Need type annotat...
Opengraph URL: https://github.com/python/mypy/issues/2022
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Type declarations of base class attributes not carried over to subclasses if `None` is present","articleBody":"Simplest repro:\r\n\r\n```\r\nclass BaseClass:\r\n my_attr = () # type: tuple\r\n\r\nclass Subclass(BaseClass):\r\n my_attr = None\r\n```\r\n\r\nOutput:\r\n\r\n```\r\ntesting.py: note: In class \"Subclass\":\r\ntesting.py:5: error: Need type annotation for variable\r\n```\r\n\r\nHowever, if `None` isn't the sole value but part of something else, this is likely not the desired behaviour. Consider this:\r\n\r\n```\r\nfrom typing import Tuple, Any\r\n\r\nclass BaseClass:\r\n my_attr = ( (\"\", None), ) # type: Tuple[Tuple[str, Any]]\r\n\r\nclass Subclass(BaseClass):\r\n my_attr = (\r\n (\"one\", set ),\r\n (\"two\", str ),\r\n (\"three\",None),\r\n )\r\n```\r\n\r\nOutput:\r\n\r\n```\r\ntesting.py: note: In class \"Subclass\":\r\ntesting.py:8: error: Need type annotation for variable\r\n```\r\n\r\nChanging `None` for anything else, adding a `# type: ignore` comment on the `(\"one\", set),` line (but not on any other line of the assignment) silences the error. This is somewhat related to #1338 (and, to a lesser extent, #1032).\r\n","author":{"url":"https://github.com/Vgr255","@type":"Person","name":"Vgr255"},"datePublished":"2016-08-13T15:04:25Z","url":"https://github.com/python/mypy/issues/2022"}
| 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:f76a69d8-cbf8-dafb-b87f-15fc615dc81d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C966:2B21DE:5A9152:7AC083:6B00745A |
| html-safe-nonce | de451b0c09ce2c93a34e42784021008bf33763235012f049b5d44f3f6b76ca74 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDOTY2OjJCMjFERTo1QTkxNTI6N0FDMDgzOjZCMDA3NDVBIiwidmlzaXRvcl9pZCI6IjUyMTU3NjQwMzA3MTQ1NzM5MTQiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 5ebc24096cda3684f1ba1aa5f23b1ff8ccf7b09dfebc0e98d774c7662bfa81dd |
| hovercard-subject-tag | issue:171012663 |
| 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/2022/issue_layout |
| twitter:image | https://opengraph.githubassets.com/2f90f6511016de75d443fe892aa193652091ebae61b60166bf4805d8be539215/python/mypy/issues/2022 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/2f90f6511016de75d443fe892aa193652091ebae61b60166bf4805d8be539215/python/mypy/issues/2022 |
| og:image:alt | Simplest repro: class BaseClass: my_attr = () # type: tuple class Subclass(BaseClass): my_attr = None Output: testing.py: note: In class "Subclass": testing.py:5: error: Need type annotation for va... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | Vgr255 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 75c2b65dd205f26961d0c29afed1a8d0bb7cca9f05780571dfd1ef93eb539d16 |
| 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 | 84e563eb4b55f8fa102f325f117533cee9e3df4a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width