Title: MyPy reports errors when performing nested element access using a ``TypeVar``-parameterized generic type · Issue #16953 · python/mypy · GitHub
Open Graph Title: MyPy reports errors when performing nested element access using a ``TypeVar``-parameterized generic type · Issue #16953 · python/mypy
X Title: MyPy reports errors when performing nested element access using a ``TypeVar``-parameterized generic type · Issue #16953 · python/mypy
Description: Bug Report Type variables and generics enable propagating finer-grained type information through objects like containers. However, it seems that MyPy has issues when this design pattern is used in a nested manner: To Reproduce import typ...
Open Graph Description: Bug Report Type variables and generics enable propagating finer-grained type information through objects like containers. However, it seems that MyPy has issues when this design pattern is used in ...
X Description: Bug Report Type variables and generics enable propagating finer-grained type information through objects like containers. However, it seems that MyPy has issues when this design pattern is used in ...
Opengraph URL: https://github.com/python/mypy/issues/16953
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"MyPy reports errors when performing nested element access using a ``TypeVar``-parameterized generic type","articleBody":"**Bug Report**\r\n\r\nType variables and generics enable propagating finer-grained type information through objects like containers. However, it seems that MyPy has issues when this design pattern is used in a nested manner:\r\n\r\n**To Reproduce**\r\n\r\n```python\r\nimport typing\r\n\r\nT = typing.TypeVar(\"T\")\r\n\r\nclass Container(typing.Generic[T]):\r\n def __init__(self, value : T) -> None:\r\n self.value = value\r\n\r\nclass A(Container[int]):\r\n pass\r\n\r\nclass B(Container[A]):\r\n pass\r\n\r\ndef get(x: Container[T]) -> T:\r\n return x.value\r\n\r\n\r\na = B(A(5))\r\nreveal_type(a)\r\n\r\nb = get(a)\r\nreveal_type(b)\r\n\r\nc1 = get(b)\r\nreveal_type(c1)\r\n\r\n# This is expected to match 'c1'\r\nc2 = get(get(a))\r\nreveal_type(c2)\r\n```\r\nMyPy-Play link: https://gist.github.com/mypy-play/40d62fe7a0fc28bf68bd9d156ba77c5b\r\n\r\n```\r\na.py:20: note: Revealed type is \"a.B\"\r\na.py:23: note: Revealed type is \"a.A\"\r\na.py:26: note: Revealed type is \"builtins.int\"\r\na.py:30: note: Revealed type is \"builtins.int\"\r\n```\r\n\r\nReference (PyRight):\r\n```\r\n/Users/wjakob/drjit-nanobind/a.py\r\n /Users/wjakob/drjit-nanobind/a.py:20:13 - information: Type of \"a\" is \"B\"\r\n /Users/wjakob/drjit-nanobind/a.py:23:13 - information: Type of \"b\" is \"A\"\r\n /Users/wjakob/drjit-nanobind/a.py:26:13 - information: Type of \"c1\" is \"int\"\r\n /Users/wjakob/drjit-nanobind/a.py:30:13 - information: Type of \"c2\" is \"int\"\r\n```\r\n\r\n**Actual Behavior**\r\n\r\n```\r\na.py:20: note: Revealed type is \"a.B\"\r\na.py:23: note: Revealed type is \"a.A\"\r\na.py:26: note: Revealed type is \"builtins.int\"\r\na.py:29: error: Argument 1 to \"get\" has incompatible type \"B\"; expected \"Container[Container[int]]\" [arg-type]\r\na.py:30: note: Revealed type is \"builtins.int\"\r\nFound 1 error in 1 file (checked 1 source file)\r\n```\r\n\r\n**Your Environment**\r\nPython 3.12.2, MyPy 1.8.0\r\n","author":{"url":"https://github.com/wjakob","@type":"Person","name":"wjakob"},"datePublished":"2024-02-27T08:31:00Z","url":"https://github.com/python/mypy/issues/16953"}
| 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:bef74493-c79e-b93e-598a-c7407dc81b4b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A8F8:B7CF9:3634A8:48FA72:6B0027E0 |
| html-safe-nonce | 38c1941c35e5b3db41c397051098a068b7b9d84dd887642d5dafa1309245251f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBOEY4OkI3Q0Y5OjM2MzRBODo0OEZBNzI6NkIwMDI3RTAiLCJ2aXNpdG9yX2lkIjoiMjQ3MTYwNDE5MDAxMTQ2Nzc0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d7dbbdcb5a703b38839321e4847687b083c2752a6263e45b25beda76c72a45b9 |
| hovercard-subject-tag | issue:2155978532 |
| 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/16953/issue_layout |
| twitter:image | https://opengraph.githubassets.com/256d4884b487d8d67430f224ff5cd1c9275b79fe187fc68f4dcd636d19e6b31c/python/mypy/issues/16953 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/256d4884b487d8d67430f224ff5cd1c9275b79fe187fc68f4dcd636d19e6b31c/python/mypy/issues/16953 |
| og:image:alt | Bug Report Type variables and generics enable propagating finer-grained type information through objects like containers. However, it seems that MyPy has issues when this design pattern is used in ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wjakob |
| 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