redefinition-new does not apply type narrowing on cast-like function calls
bug
**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.)
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.)
**To Reproduce**
```python3
from collections.abc import Collection
from typing import Any, cast, reveal_type
def convert[T](type_: type[T], x: Any) -> T:
return cast(T, x)
def to_set[T](x: Collection[T]) -> set[T]:
return set(x)
def foo(x: Collection[int]) -> None:
reveal_type(x) # Collection[int]
reveal_type(convert(set[int], x)) # set[int]
x = convert(set[int], x)
reveal_type(x) # old: set[int], new: Collection[int]
def bar(x: Collection[int]) -> None:
reveal_type(x) # Collection[int]
reveal_type(cast(set[int], x)) # set[int]
x = cast(set[int], x)
reveal_type(x) # set[int]
def baz(x: Collection[int]) -> None:
reveal_type(x) # Collection[int]
reveal_type(set(x)) # set[int]
x = set(x)
reveal_type(x) # set[int]
def brr(x: Collection[int]) -> None:
reveal_type(x) # Collection[int]
reveal_type(to_set(x)) # set[int]
x = to_set(x)
reveal_type(x) # set[int]
```
[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)
[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)
[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)
(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`.
1 条评论