Skip to content

Regression with Narrowing (Test Cases) #20600

Description

@George-Ogden

I had been working on narrowing in #20277.
After #20275 was fixed in #20492, I merged and ran some test cases.

Here are some test cases that I wrote from that one that the new version fails on:

[casetestMultipleTypeEqualsCheck]
fromtypingimportAnyx: Anyy: Anyiftype(x) ==type(y) ==int:
reveal_type(y) # N: Revealed type is "builtins.int"reveal_type(x) # N: Revealed type is "builtins.int"z: Anyifint==type(z) ==int:
reveal_type(z) # N: Revealed type is "builtins.int"
[casetestTypeEqualsCheckUsingImplicitTypes]
fromtypingimportAnyx: stry: Anyz: objectiftype(y) istype(x):
reveal_type(x) # N: Revealed type is "builtins.str"reveal_type(y) # N: Revealed type is "builtins.str"iftype(x) istype(z):
reveal_type(x) # N: Revealed type is "builtins.str"reveal_type(z) # N: Revealed type is "builtins.str"
[casetestTypeEqualsCheckUsingDifferentSpecializedTypes]
fromcollectionsimportdefaultdictx: defaultdicty: dictz: objectiftype(x) istype(y) istype(z):
reveal_type(x) # N: Revealed type is "collections.defaultdict[Any, Any]"reveal_type(y) # N: Revealed type is "collections.defaultdict[Any, Any]"reveal_type(z) # N: Revealed type is "collections.defaultdict[Any, Any]"
[casetestUnionTypeEquality]
fromtypingimportAny, reveal_type# flags: --warn-unreachablex: Any= ()
iftype(x) == (int, str):
reveal_type(x) # E: Statement is unreachableclassX: x=1classY: y=1classZ(X, Y): ...
z=Z()
x: X=zy: Y=ziftype(x) istype(y):
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.Y" and "__main__.X">"x.y+y.xifisinstance(x, type(y)) andisinstance(y, type(x)):
reveal_type(x) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"reveal_type(y) # N: Revealed type is "__main__.<subclass of "__main__.X" and "__main__.Y">"x.y+y.x
[casetestTypeEqualsCheckUsingIsNonOverlapping]
# flags: --warn-unreachablefromtypingimportUniony: striftype(y) isint: # E: Subclass of "str" and "int" cannot exist: would have incompatible method signaturesy# E: Statement is unreachableelse:
reveal_type(y) # N: Revealed type is "builtins.str"
[casetestTypeEqualsMultipleTypesShouldntNarrow]
# make sure we don't do any narrowing if there are multiple types being compared# flags: --warn-unreachablefromtypingimportUnionx: Union[int, str]
iftype(x) ==int==str:
reveal_type(x) # E: Statement is unreachableelse:
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str"

Please let me know if I can contribute to #20598 (or any other parts of this feature).

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-narrowingConditional type narrowing / binder

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions