Title: mypy rejects subclassing `Any` typed `*args` entries within a function · Issue #21998 · python/mypy · GitHub
Open Graph Title: mypy rejects subclassing `Any` typed `*args` entries within a function · Issue #21998 · python/mypy
X Title: mypy rejects subclassing `Any` typed `*args` entries within a function · Issue #21998 · python/mypy
Description: (No AI or LLMs were used in finding or writing up this issue.) In Python and passing other type checkers (e.g. pyright 1.1.414 ) it is ok to subclass a type passed within the *args variadic argument which is typed as Any, but mypy 2.3.1 ...
Open Graph Description: (No AI or LLMs were used in finding or writing up this issue.) In Python and passing other type checkers (e.g. pyright 1.1.414 ) it is ok to subclass a type passed within the *args variadic argumen...
X Description: (No AI or LLMs were used in finding or writing up this issue.) In Python and passing other type checkers (e.g. pyright 1.1.414 ) it is ok to subclass a type passed within the *args variadic argumen...
Opengraph URL: https://github.com/python/mypy/issues/21998
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"mypy rejects subclassing `Any` typed `*args` entries within a function","articleBody":"**(No AI or LLMs were used in finding or writing up this issue.)**\n\nIn Python and passing other type checkers (e.g. pyright 1.1.414 ) it is ok to subclass a type passed within the `*args` variadic argument which is typed as Any, but mypy 2.3.1 and recent master seems to reject this.\n\nE.g. the following Python program runs fine and typechecks via pyright 1.1.414 \n(btw if you have an objection to this on the basis of the intermediate `x` variable, please also see the example pair at the end of the issue showing mypy will allow a keyword arg for a type to subclass but not an element of `*args` without an intermediate variable)\n \nhttps://mypy-play.net/?gist=caa19ef606afd2b352e2e5e3fa608bca \n```\nfrom typing import Any\n\ndef fun_varargs_element_to_type_used_for_inherit(\n *args: *tuple[int, Any],\n) -> Any:\n # not allowed by mypy but allowed by pyright and others\n x = args[1]\n class Sub(x): pass\n return Sub()\n\ns = fun_varargs_element_to_type_used_for_inherit(1, str)\nprint('isinstance(s, str): ', isinstance(s, str))\n```\n\nPer pyright 1.1.414 it typechecks and in python 3.13.2 (and 3.12 see gist above) runs and produces valid output:\n```\n(venv) [liveuser@localhost-live mypy]$ pyright ~/examplecomplex2.py\n0 errors, 0 warnings, 0 informations\n(venv) [liveuser@localhost-live mypy]$ python ~/examplecomplex2.py\nisinstance(s, str): True\n```\n\nBut mypy 2.3.1 rejects it (see mypy playground link and associated gist https://mypy-play.net/?gist=caa19ef606afd2b352e2e5e3fa608bca ) .\n\nHere are some unit test cases showing this behavior as well:\n```\n[case testVarargsArgumentTypeCheckingUsingArgumentTypeFromVarArgsForInheritanceExplicitCastOk]\n# flags: --python-version 3.12\nfrom typing import Any\n\ndef fun_explicit_cast_varargs_element_to_type_used_for_inherit(\n *args: *tuple[int, Any],\n):\n # allowed\n x : type = args[1]\n class Sub(x): pass\n\nfun_explicit_cast_varargs_element_to_type_used_for_inherit(1, str)\n\n[builtins fixtures/tuple.pyi]\n\n[case testVarargsArgumentTypeCheckingUsingArgumentTypeFromVarArgsForInheritance-xfail]\n# flags: --python-version 3.12\nfrom typing import Any\n\ndef fun_varargs_element_to_type_used_for_inherit(\n *args: *tuple[int, Any],\n):\n # not allowed by mypy but allowed by pyright and others\n x = args[1]\n class Sub(x): pass\n\nfun_varargs_element_to_type_used_for_inherit(1, str)\n\n[builtins fixtures/tuple.pyi]\n```\n\n---\n\nThe first case passes, the second xfails (I marked it expected fail for now per current behavior) when it should pass it seems.\n\nNote that passing a type via an Any typed keyword parameter is OK-ed by mypy:\nhttps://mypy-play.net/?gist=9ff16e8bc2f873f3fe5d0dcb4ff8baeb\n```\n[liveuser@localhost-live mypy]$ cat ~/examplecomplex3.py\nfrom typing import Any\n\ndef fun_keyword_parameter_to_type_used_for_inherit(\n *args: *tuple[int, Any],\n keywordarg: Any\n) -> Any:\n class Sub(keywordarg): pass\n return Sub()\n\ns = fun_keyword_parameter_to_type_used_for_inherit(1, 1, keywordarg=str)\nprint('isinstance(s, str): ', isinstance(s, str))\n\n[liveuser@localhost-live mypy]$ python ~/examplecomplex3.py\nisinstance(s, str): True\n\n[liveuser@localhost-live mypy]$ mypy ~/examplecomplex3.py\nSuccess: no issues found in 1 source file\n```\n\nBut using `args[1]` (an element of the `*args` tuple directly) when Any typed to pass a type for subclassing is not allowed by mypy, while it is allowed by pyright 1.1.414 and python:\nhttps://mypy-play.net/?gist=d12a1ef7f71677f150d09200fadf1958\n```\n[liveuser@localhost-live mypy]$ cat ~/examplecomplex4.py\nfrom typing import Any\n\ndef fun_varargs_element_to_type_used_for_inherit(\n *args: *tuple[int, Any],\n) -> Any:\n # not allowed by mypy but allowed by pyright and others\n class Sub(args[1]): pass\n return Sub()\n\ns = fun_varargs_element_to_type_used_for_inherit(1, str)\nprint('isinstance(s, str): ', isinstance(s, str))\n\n[liveuser@localhost-live mypy]$ python ~/examplecomplex4.py\nisinstance(s, str): True\n\n[liveuser@localhost-live mypy]$ pyright ~/examplecomplex4.py\n0 errors, 0 warnings, 0 informations\n\n[liveuser@localhost-live mypy]$ mypy ~/examplecomplex4.py\n/home/liveuser/examplecomplex4.py:7: error: Variable \"args\" is not valid as a type [valid-type]\n/home/liveuser/examplecomplex4.py:7: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases\n/home/liveuser/examplecomplex4.py:7: error: Invalid base class \"args\" [misc]\n/home/liveuser/examplecomplex4.py:7: error: Invalid type: try using Literal[1] instead? [valid-type]\nFound 3 errors in 1 file (checked 1 source file)\n```\n\n(Note the above typechecks fine in pyright 1.1.414 ) \n\n- Mypy version used: 2.3.1 (and recent master)\n- Mypy command-line flags: None\n- Mypy configuration options from `mypy.ini` (and other config files): defaults\n- Python version used: 3.12 and 3.13.2\n\nThanks so much for building this tool!","author":{"url":"https://github.com/willy-b","@type":"Person","name":"willy-b"},"datePublished":"2026-09-16T20:29:09Z","url":"https://github.com/python/mypy/issues/21998"}
| 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:9025d6ec-096f-54f8-d644-c10ac0081e4e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 87B4:1AEC58:14B704:1BB510:6B001F08 |
| html-safe-nonce | 6bb5895739cba8d2f2df1743c283d5af11a09ab1f0fc8889dc837a5313a26f6b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4N0I0OjFBRUM1ODoxNEI3MDQ6MUJCNTEwOjZCMDAxRjA4IiwidmlzaXRvcl9pZCI6IjQ1NzgzMzI4NTg5Nzk5ODMxMTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 3370874a2727d3cb842a6e236c625f4f3ed72120c21c6def5b9da97fecb4656c |
| hovercard-subject-tag | issue:5479582277 |
| 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/21998/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6f716c563794609b193c5f59f82ecc08dfaf5823e3d9c66c7088bc0a2c69dd32/python/mypy/issues/21998 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6f716c563794609b193c5f59f82ecc08dfaf5823e3d9c66c7088bc0a2c69dd32/python/mypy/issues/21998 |
| og:image:alt | (No AI or LLMs were used in finding or writing up this issue.) In Python and passing other type checkers (e.g. pyright 1.1.414 ) it is ok to subclass a type passed within the *args variadic argumen... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | willy-b |
| 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