You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
redefinition-new does not apply type narrowing on cast-like function calls #21532
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.)
mypy 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
For other function calls that convert the input, this works perfectly fine.
(A clear and concise description of what the bug is.)
(The playground does not support --allow-redefinition-old so you cannot use the old variant with 2.0+)
Expected Behavior
After x = convert(set[int], x) the type of x should be narrowed to set[int].
Actual Behavior
It works with the old, but not the new --allow-redefinition.
Your Environment
I 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.
I found another similar thing with comprehensions:
def foo(x: Collection[int | None]) -> None:
reveal_type(x) # Collection[int | None]
reveal_type({y for y in x if y}) # set[int]
x = {y for y in x if y}
reveal_type(x) # old: set[int], new: set[int | None]
The narrowing from set to collection works, but the inner type isn't narrowed with new redefinitions.
Activity
DerBiasto commentedon May 22, 2026
I found another similar thing with comprehensions:
The narrowing from set to collection works, but the inner type isn't narrowed with new redefinitions.
DerBiasto commentedon May 22, 2026
Another variant:
https://mypy-play.net/?mypy=1.20.2&python=3.14&flags=allow-redefinition-new%2Clocal-partial-types&gist=bd413d054a355435a99dd2759ccaedcb
https://mypy-play.net/?mypy=1.20.2&python=3.14&flags=allow-redefinition%2Clocal-partial-types&gist=bd413d054a355435a99dd2759ccaedcb