Combining two type checks with and
deduces Any
if the first is Any
#17100
Labels
bug
mypy got something wrong
and
deduces Any
if the first is Any
#17100
Combining two
isinstance
checks withand
where the first one deduces the type toAny
ignores the second one.To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=643066dcf62c1c2b88bb4714032fa8b5
Expected Behavior
Mypy should deduce the type as
A
in both cases:Actual Behavior
Background
This worked before mypy 1.7 and is a regression caused by #16237. The direct reason is that
Any
is now preferred in some circumstances inand_conditional_maps()
(theisinstance
in theif
):mypy/mypy/checker.py
Lines 7609 to 7618 in 8019010
I'm not sure why this is the case or why it is needed to prefer
Any
here. Intuitively, it should be the other way around. The additionalAny
doesn't add any new information but it shouldn't destroy the information already known from other sources.When I remove the
or isinstance(get_proper_type(m1[n1]), AnyType)
in the above code, only one testcase fails (intestNarrowingWithAnyOps
) because it explicitly tests for thisAny
result, like this:With the changed code this deduces the type as
D
instead.The text was updated successfully, but these errors were encountered: