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
Narrowing doesn't work on a typevar with a union bound #15631
But that's not the case! The main problem here is typevar reveal_types cannot really express the narrowing as shown here. While mypy can do it under the hood (and you presumably have a case that shows this isn't the case?) that's not exactly what you're asking here!
typevar_narrow.py:7: note: Revealed type is "builtins.int"
typevar_narrow.py:8: error: Incompatible return value type (got "int", expected "T") [return-value]
The type of x was narrowed to int and became incompatible with T even though the code is obviously correct. Here we did too much narrowing.
Actually, this issue is different from #15235. #15235 is only about the negative-narrowing behavior, whereas this issue is also about the positive-narrowing behavior. My bad, sorry for the noise!
narrow.py:15: note: Revealed type is "narrow.A | narrow.B"
narrow.py:16: error: Incompatible return value type (got "A | B", expected "T") [return-value]
This however doesn't work either, but it is caused by an unrelated bug. This is similar to what @sterliakov was working on at some point (but with def (Type[T]) -> T kind of things, see #14790), but looks like yet another problem with type variables with union bounds. I suggest opening a separate issue.
Activity
A5rocks commentedon Jul 18, 2023
By the way, the expected behavior is not correct:
Imagine
Tis bound as some string subclass! IfTwere accepted asstrthen the following should probably be valid, correct?:But that's not the case! The main problem here is typevar
reveal_types cannot really express the narrowing as shown here. While mypy can do it under the hood (and you presumably have a case that shows this isn't the case?) that's not exactly what you're asking here!Mind clarifying a bit?
eltoder commentedon Jul 18, 2023
Good point, my example is not 100% correct. Let me clarify and also add a second problem case.
isinstancecheck:This produces:
The type of
xwas narrowed tointand became incompatible withTeven though the code is obviously correct. Here we did too much narrowing.This produces
but we know the type should be (a subclass of)
str, because of the bound onT.To make it work in both cases, I think, rather than narrowing the type of
xwe should narrow the bound onT.brianschubert commentedon Nov 14, 2024
Duplicate of #15235
isinstanceofTypeVar#15235isinstanceofTypeVar#15235brianschubert commentedon Nov 14, 2024
Actually, this issue is different from #15235. #15235 is only about the negative-narrowing behavior, whereas this issue is also about the positive-narrowing behavior. My bad, sorry for the noise!
ilevkivskyi commentedon May 31, 2025
The original example is not a bug in itself, the rest should be fixed by #19183
jorgefandinno commentedon Aug 3, 2025
This seems not completely solved in mypy 1.17.1 when calling a method that return the type of the object.
produces
ilevkivskyi commentedon Aug 3, 2025
@jorgefandinno This example is genuinely unsafe. I guess what you may want is something like this:
This however doesn't work either, but it is caused by an unrelated bug. This is similar to what @sterliakov was working on at some point (but with
def (Type[T]) -> Tkind of things, see #14790), but looks like yet another problem with type variables with union bounds. I suggest opening a separate issue.