File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change @@ -3233,6 +3233,8 @@ def get_isinstance_type(expr: Expression,
3233
3233
elif isinstance (typ , Instance ) and typ .type .fullname () == 'builtins.type' :
3234
3234
object_type = Instance (typ .type .mro [- 1 ], [])
3235
3235
types .append (TypeRange (object_type , is_upper_bound = True ))
3236
+ elif isinstance (typ , AnyType ):
3237
+ types .append (TypeRange (typ , is_upper_bound = False ))
3236
3238
else : # we didn't see an actual type, but rather a variable whose value is unknown to us
3237
3239
return None
3238
3240
if not types :
Original file line number Diff line number Diff line change @@ -1975,3 +1975,33 @@ def f() -> None:
1975
1975
[typing fixtures/typing-full.pyi]
1976
1976
[builtins fixtures/dict.pyi]
1977
1977
[out]
1978
+
1979
+ [case testIsinstanceWidensWithAnyArg]
1980
+ from typing import Any
1981
+ class A: ...
1982
+ B: Any
1983
+ x: A
1984
+ x.foo() # E: "A" has no attribute "foo"
1985
+ assert isinstance(x, B)
1986
+ x.foo()
1987
+ reveal_type(x) # E: Revealed type is 'Any'
1988
+ [builtins fixtures/isinstance.pyi]
1989
+
1990
+ [case testIsinstanceWidensUnionWithAnyArg]
1991
+ from typing import Any, Union
1992
+ class A: ...
1993
+ B: Any
1994
+ x: Union[A, B]
1995
+ reveal_type(x) # E: Revealed type is 'Union[__main__.A, Any]'
1996
+ assert isinstance(x, B)
1997
+ reveal_type(x) # E: Revealed type is 'Any'
1998
+ [builtins fixtures/isinstance.pyi]
1999
+
2000
+ [case testIsinstanceIgnoredImport]
2001
+ from typing import Union
2002
+ from foo import A # type: ignore
2003
+ def f(x: Union[A, str]) -> None:
2004
+ x.method_only_in_a() # E: Item "str" of "Union[Any, str]" has no attribute "method_only_in_a"
2005
+ if isinstance(x, A):
2006
+ x.method_only_in_a()
2007
+ [builtins fixtures/isinstance.pyi]
You can’t perform that action at this time.
0 commit comments