Title: Support recursive NamedTuple · Issue #9397 · python/mypy · GitHub
Open Graph Title: Support recursive NamedTuple · Issue #9397 · python/mypy
X Title: Support recursive NamedTuple · Issue #9397 · python/mypy
Description: 🐛 Bug Report NamedTuple's appear to behave in unexpected ways with recursion, especially when compared to frozen dataclasses (which are rather similar, as far as the user is concerned). To Reproduce from __future__ import annotations fro...
Open Graph Description: 🐛 Bug Report NamedTuple's appear to behave in unexpected ways with recursion, especially when compared to frozen dataclasses (which are rather similar, as far as the user is concerned). To Reproduc...
X Description: 🐛 Bug Report NamedTuple's appear to behave in unexpected ways with recursion, especially when compared to frozen dataclasses (which are rather similar, as far as the user is concerned). To Repr...
Opengraph URL: https://github.com/python/mypy/issues/9397
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Support recursive NamedTuple","articleBody":"\u003c!--\r\nNote: If the problem you are reporting is about a specific library function, then the typeshed tracker is better suited\r\nfor this report: https://github.com/python/typeshed/issues\r\n-->\r\n\r\n## 🐛 Bug Report\r\n\r\nNamedTuple's appear to behave in unexpected ways with recursion, especially when compared to frozen dataclasses (which are rather similar, as far as the user is concerned).\r\n\r\n## To Reproduce\r\n\r\n```python\r\nfrom __future__ import annotations\r\nfrom typing import NamedTuple, Optional\r\n\r\nclass A(NamedTuple):\r\n a: Optional[A]\r\n```\r\n\r\nProduces a \"mypy-sample1.py:5: error: Cannot resolve name \"A\" (possible cyclic definition)\". However,\r\n\r\n```python\r\nfrom __future__ import annotations\r\nfrom typing import Optional\r\nfrom dataclasses import dataclass\r\n\r\n@dataclass(frozen=True)\r\nclass A:\r\n a: Optional[A]\r\n```\r\n\r\nPasses MyPy just fine, and as far as a user is concerned, presents a very similar surface for MyPy to type check (i.e. both have a read-only attribute a which is either None or of class A).\r\n\r\n## Expected Behavior\r\n\r\nThe expected behaviour would be that both NamedTuple and frozen dataclass behave the same, as NamedTuple and frozen dataclasses present logically similar behaviour with regards to the attribute 'a'.\r\n\r\n## Actual Behavior\r\n\r\nIn addition to the odd behaviour with cycle checker triggering on NamedTuple but not on dataclasses, it's possible to fool the cycle checker completely and have MyPy crash by recursing too far (e.g. issue #8695 and although that issue has been reported with a recursive type that could never be instantiated, I've encountered the same behaviour with type definitions that can be instantiated, but not in a minimal example friendly format). I think this specific behaviour is the root cause, as the real problem is that for some reason NamedTuple's are behaving differently to what would be expected.\r\n\r\n## Your Environment\r\n\r\n\u003c!-- Include as many relevant details about the environment you experienced the bug in -->\r\n\r\n- Mypy version used: mypy 0.782\r\n- Mypy command-line flags: none\r\n- Mypy configuration options from `mypy.ini` (and other config files): none\r\n- Python version used: 3.8.2\r\n- Operating system and version: Linux\r\n\r\n","author":{"url":"https://github.com/habilain","@type":"Person","name":"habilain"},"datePublished":"2020-09-02T15:44:26Z","url":"https://github.com/python/mypy/issues/9397"}
| 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:47b2d315-b124-2813-f275-98099d8bf242 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 996C:34D7A7:1F850D:2A9769:6B00228F |
| html-safe-nonce | 56999844bece69169b3d9e1e9ec9ebbb7919879462abbe713419fba0bf06c3b5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTZDOjM0RDdBNzoxRjg1MEQ6MkE5NzY5OjZCMDAyMjhGIiwidmlzaXRvcl9pZCI6IjczMTY3ODc0MTg3MjI1NTYzMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 94e90a9c803dcabe8d3e2b939e8066e3cdef276b289bdd9f70a6c21d67e5d3d3 |
| hovercard-subject-tag | issue:691132310 |
| 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/9397/issue_layout |
| twitter:image | https://opengraph.githubassets.com/e53145ad7687c6f4a96b01d6440fecb477502a6d81965190e73c40678acf5736/python/mypy/issues/9397 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/e53145ad7687c6f4a96b01d6440fecb477502a6d81965190e73c40678acf5736/python/mypy/issues/9397 |
| og:image:alt | 🐛 Bug Report NamedTuple's appear to behave in unexpected ways with recursion, especially when compared to frozen dataclasses (which are rather similar, as far as the user is concerned). To Reproduc... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | habilain |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4eb4cba0d69f045374ac15de9666d22b0961e1d555cc3824f36b7734c00c78e0 |
| 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 | 44c658b24abf00491fb7ae5f56d42c9f38cbeab2 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width