-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix type guard crashes #11061
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix type guard crashes #11061
Changes from all commits
e1bd6cf
ac8c2a3
436e9ad
2494cd4
884307c
9c9c358
75808e0
74f04f4
6d4af01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ class level -- these are handled at attribute level (say, 'mod.Cls.method' | |
FuncBase, OverloadedFuncDef, FuncItem, MypyFile, UNBOUND_IMPORTED | ||
) | ||
from mypy.types import ( | ||
Type, TypeGuardType, TypeVisitor, UnboundType, AnyType, NoneType, UninhabitedType, | ||
Type, TypeVisitor, UnboundType, AnyType, NoneType, UninhabitedType, | ||
ErasedType, DeletedType, Instance, TypeVarType, CallableType, TupleType, TypedDictType, | ||
UnionType, Overloaded, PartialType, TypeType, LiteralType, TypeAliasType | ||
) | ||
|
@@ -335,9 +335,6 @@ def visit_union_type(self, typ: UnionType) -> SnapshotItem: | |
normalized = tuple(sorted(items)) | ||
return ('UnionType', normalized) | ||
|
||
def visit_type_guard_type(self, typ: TypeGuardType) -> SnapshotItem: | ||
return ('TypeGuardType', snapshot_type(typ.type_guard)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this may be wrong. We still need to have some way to propagate the information on updated type guard function. (Btw most likely the old way was wrong as well, do we actually have good test cover for type guards in daemon mode?) |
||
|
||
def visit_overloaded(self, typ: Overloaded) -> SnapshotItem: | ||
return ('Overloaded', snapshot_types(typ.items)) | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,7 +4,7 @@ | |||||
from typing_extensions import Final | ||||||
|
||||||
from mypy.types import ( | ||||||
Type, AnyType, TypeGuardType, UnboundType, TypeVisitor, FormalArgument, NoneType, | ||||||
Type, AnyType, UnboundType, TypeVisitor, FormalArgument, NoneType, | ||||||
Instance, TypeVarType, CallableType, TupleType, TypedDictType, UnionType, Overloaded, | ||||||
ErasedType, PartialType, DeletedType, UninhabitedType, TypeType, is_named_instance, | ||||||
FunctionLike, TypeOfAny, LiteralType, get_proper_type, TypeAliasType | ||||||
|
@@ -475,9 +475,6 @@ def visit_overloaded(self, left: Overloaded) -> bool: | |||||
def visit_union_type(self, left: UnionType) -> bool: | ||||||
return all(self._is_subtype(item, self.orig_right) for item in left.items) | ||||||
|
||||||
def visit_type_guard_type(self, left: TypeGuardType) -> bool: | ||||||
raise RuntimeError("TypeGuard should not appear here") | ||||||
|
||||||
def visit_partial_type(self, left: PartialType) -> bool: | ||||||
# This is indeterminate as we don't really know the complete type yet. | ||||||
raise RuntimeError | ||||||
|
@@ -1377,14 +1374,6 @@ def visit_overloaded(self, left: Overloaded) -> bool: | |||||
def visit_union_type(self, left: UnionType) -> bool: | ||||||
return all([self._is_proper_subtype(item, self.orig_right) for item in left.items]) | ||||||
|
||||||
def visit_type_guard_type(self, left: TypeGuardType) -> bool: | ||||||
if isinstance(self.right, TypeGuardType): | ||||||
# TypeGuard[bool] is a subtype of TypeGuard[int] | ||||||
return self._is_proper_subtype(left.type_guard, self.right.type_guard) | ||||||
else: | ||||||
# TypeGuards aren't a subtype of anything else for now (but see #10489) | ||||||
return False | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So just to clarify, what is the view on subtyping between type guard functions? Can a type guard method be overridden in a subclass? Can a type guard function be expected as a callback? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe yes to both. But note that Line 356 in e1bd6cf
mypy.types.TypeGuardedType is only created in find_isinstance_check: Line 4268 in 9c9c358
|
||||||
|
||||||
def visit_partial_type(self, left: PartialType) -> bool: | ||||||
# TODO: What's the right thing to do here? | ||||||
return False | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if it's not a proper type, we still need to fixup the deserialized target type. Do we have an incremental test with an import cycle?