Title: redefinition-new does not apply type narrowing on cast-like function calls · Issue #21532 · python/mypy · GitHub
Open Graph Title: redefinition-new does not apply type narrowing on cast-like function calls · Issue #21532 · python/mypy
X Title: redefinition-new does not apply type narrowing on cast-like function calls · Issue #21532 · python/mypy
Description: Bug Report I have a function to which I am passing both a type and a value that the function converts to be of that type. (In the example, the function simply calls cast, in my actual use case it does some actual conversions of course.) ...
Open Graph Description: Bug Report I have a function to which I am passing both a type and a value that the function converts to be of that type. (In the example, the function simply calls cast, in my actual use case it d...
X Description: Bug Report I have a function to which I am passing both a type and a value that the function converts to be of that type. (In the example, the function simply calls cast, in my actual use case it d...
Opengraph URL: https://github.com/python/mypy/issues/21532
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"redefinition-new does not apply type narrowing on cast-like function calls","articleBody":"**Bug Report**\n\nI have a function to which I am passing both a type and a value that the function converts to be of that type.\n\n(In the example, the function simply calls `cast`, in my actual use case it does some actual conversions of course.)\n\nmypy correctly determines the type of the return, but assigning this return to the variable does not narrow the type of the variable, when using `--allow-redefinition-new`. This does not happen with `--allow-redefinition-old`\n\nFor other function calls that convert the input, this works perfectly fine.\n\n(A clear and concise description of what the bug is.)\n\n**To Reproduce**\n\n```python3\nfrom collections.abc import Collection\nfrom typing import Any, cast, reveal_type\n\ndef convert[T](type_: type[T], x: Any) -> T:\n return cast(T, x)\n\ndef to_set[T](x: Collection[T]) -> set[T]:\n return set(x)\n\ndef foo(x: Collection[int]) -> None:\n reveal_type(x) # Collection[int]\n reveal_type(convert(set[int], x)) # set[int]\n x = convert(set[int], x)\n reveal_type(x) # old: set[int], new: Collection[int]\n\ndef bar(x: Collection[int]) -> None:\n reveal_type(x) # Collection[int]\n reveal_type(cast(set[int], x)) # set[int]\n x = cast(set[int], x)\n reveal_type(x) # set[int]\n\ndef baz(x: Collection[int]) -> None:\n reveal_type(x) # Collection[int]\n reveal_type(set(x)) # set[int]\n x = set(x)\n reveal_type(x) # set[int]\n\ndef brr(x: Collection[int]) -> None:\n reveal_type(x) # Collection[int]\n reveal_type(to_set(x)) # set[int]\n x = to_set(x)\n reveal_type(x) # set[int]\n\n```\n\n[Playground 1.20.2 old redefinitions](https://mypy-play.net/?mypy=1.20.2&python=3.13&flags=show-error-codes%2Callow-redefinition%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cno-implicit-reexport%2Clocal-partial-types%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=7d849dccc14463fedb03b380cdedae15)\n\n[Playground 1.20.2 new redefinitions](https://mypy-play.net/?mypy=1.20.2&python=3.13&flags=show-error-codes%2Callow-redefinition-new%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cno-implicit-reexport%2Clocal-partial-types%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=1900c40ff9e82897594ac42e3ef38df3)\n\n[Playground 2.1.0 new redefinitions](https://mypy-play.net/?mypy=2.1.0&python=3.13&flags=show-error-codes%2Callow-redefinition-new%2Cstrict%2Ccheck-untyped-defs%2Cdisallow-any-decorated%2Cdisallow-any-generics%2Cdisallow-any-unimported%2Cdisallow-incomplete-defs%2Cdisallow-subclassing-any%2Cdisallow-untyped-calls%2Cdisallow-untyped-decorators%2Cdisallow-untyped-defs%2Cno-implicit-reexport%2Clocal-partial-types%2Cwarn-redundant-casts%2Cwarn-return-any%2Cwarn-unreachable%2Cwarn-unused-configs%2Cwarn-unused-ignores&gist=6a941274078607960625aad2fba524a0)\n\n(The playground does not support `--allow-redefinition-old` so you cannot use the old variant with 2.0+)\n\n**Expected Behavior**\n\nAfter `x = convert(set[int], x)` the type of x should be narrowed to `set[int]`.\n\n**Actual Behavior**\n\nIt works with the old, but not the new `--allow-redefinition`.\n\n**Your Environment**\n\nI encountered this when upgrading to `mypy==2.0.0`, when the default behavior of `--allow-redefinition` changed from old to new, but this also happens on `mypy==1.20.2` and `mypy==2.1.0`.","author":{"url":"https://github.com/DerBiasto","@type":"Person","name":"DerBiasto"},"datePublished":"2026-05-21T13:07:31Z","url":"https://github.com/python/mypy/issues/21532"}
| 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:3388efba-09a3-171a-13ba-0dcf8c5dd175 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9A08:30CC09:1E7C2F9:2903DC6:6B000C41 |
| html-safe-nonce | 7d29ed9c513b8ea217e76e4346df87242c7847e5052df9ef02343a20949903e9 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5QTA4OjMwQ0MwOToxRTdDMkY5OjI5MDNEQzY6NkIwMDBDNDEiLCJ2aXNpdG9yX2lkIjoiODc5ODcwNzE0NzI1Nzk0MTA1NyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | bc79ec16c8999c2a4c7036f8db70aa5cadfa6775b22f61278a4fde48b9227adb |
| hovercard-subject-tag | issue:4494772785 |
| 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/21532/issue_layout |
| twitter:image | https://opengraph.githubassets.com/90d00529749cc1a115db75fcf509c5a8c73d76a6639afa42c0371a10ccc058d2/python/mypy/issues/21532 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/90d00529749cc1a115db75fcf509c5a8c73d76a6639afa42c0371a10ccc058d2/python/mypy/issues/21532 |
| og:image:alt | Bug Report I have a function to which I am passing both a type and a value that the function converts to be of that type. (In the example, the function simply calls cast, in my actual use case it d... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | DerBiasto |
| hostname | github.com |
| expected-hostname | github.com |
| None | 0a0d8686a27478df474dd7415776a7a080cde8838b3cd92ea7601187572e5729 |
| 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 | 16a11879678aaf97e2a42c33ee70830aa084fa37 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width