Open muggenhor opened 3 years ago
Mypy doesn't support this sort of conditional narrowing, where it has to understand that if a is of type A, b is also of type B. This is unfortunately unlikely to change.
Any way that I could annotate this in some way? To tell it that whatever type a
has b
has as well?
The only way I can think of is to split off part of the body of the for loop into a function that takes a TypeVar("T", int, str)
as an argument.
Unfortunately that doesn't work either, I'm just getting this instead:
test.py:31: error: Value of type variable "_TIS" of "_int_or_str_compare" cannot be "Union[str, int]"
test.py:31: error: Argument 2 to "_int_or_str_compare" has incompatible type "Union[str, int]"; expected "str"
That's with this added function that gets called after the last type comparison (type(a) != type(b)
):
_TIS = TypeVar("_TIS", int, str)
def _int_or_str_compare(lhs: _TIS, rhs: str) -> bool: ...
Bug Report
Given the following script I'm receiving several unexpected errors:
Even though the
if
-block and assert statement just before the comparison operator usage ona
andb
clearly filters out the case of mismatching types. I've only added theassert
statement in the hope of telling mypy that these variables have the same type, but no luck there.