Skip to content
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 crash on ErasedType and covers_at_runtime #11924

Merged
merged 5 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,14 @@ def restrict_subtype_away(t: Type, s: Type, *, ignore_promotions: bool = False)
def covers_at_runtime(item: Type, supertype: Type, ignore_promotions: bool) -> bool:
"""Will isinstance(item, supertype) always return True at runtime?"""
item = get_proper_type(item)
supertype = get_proper_type(supertype)
if (isinstance(item, (ErasedType, DeletedType))
or isinstance(supertype, (ErasedType, DeletedType))):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I wonder if ErasedType instances can be nested inside unions, for example, and still cause a crash. Maybe erase_type should just propagate erased types unmodified? I don't think that DeletedType can be nested so it seems safe to handle here, though I'm not sure we need special casing for it. Did you find a use case where we encounter deleted types here, or is it here more as a defensive measure?

Copy link
Member Author

@sobolevn sobolevn Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, good idea. I will try it!

Did you find a use case where we encounter deleted types here, or is it here more as a defensive measure?

More like a defensive programming. I will remove it, because it does not make much sense without ErasedType.

# It might happen when working with `lambda` arguments.
# Basically, we cannot tell at this point: whether some type will cover
# `lambda`'s argument or not. But, `ErasedType` or `DeletedType`
# will break our code later in `erase_type` visitor.
return False

# Since runtime type checks will ignore type arguments, erase the types.
supertype = erase_type(supertype)
Expand Down
21 changes: 21 additions & 0 deletions test-data/unit/check-inference.test
Original file line number Diff line number Diff line change
Expand Up @@ -3210,3 +3210,24 @@ def test(seq: List[Union[Iterable, Any]]) -> None:
k = [k]
reveal_type(k) # N: Revealed type is "builtins.list[Any]"
[builtins fixtures/list.pyi]

[case testErasedTypeRuntimeCoverage]
# https://github.com/python/mypy/issues/11913
from typing import TypeVar, Type, Generic, Callable, Iterable

_S = TypeVar('_S')
_T1 = TypeVar('_T1')

class map(Generic[_S]):
def __init__(self, __func: Callable[[_T1], _S], __iter1: Iterable[_T1]) -> None: ...

class DataType: ...
T = TypeVar("T", bound=DataType)

def collection_from_dict_value(model: Type[T]) -> None:
map(
# This line was failing:
lambda i: i if isinstance(i, model) else i,
["asdf"]
)
sobolevn marked this conversation as resolved.
Show resolved Hide resolved
[builtins fixtures/isinstancelist.pyi]