Skip to content

issubclass() doesn't narrow down types of type variables #7920

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

Closed
ambv opened this issue Nov 11, 2019 · 3 comments · Fixed by #7930
Closed

issubclass() doesn't narrow down types of type variables #7920

ambv opened this issue Nov 11, 2019 · 3 comments · Fixed by #7930
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal

Comments

@ambv
Copy link
Contributor

ambv commented Nov 11, 2019

Example:

from typing import *

TypeT = TypeVar("TypeT", bound=type)

class Base:
    field: str = "Hey"

class C1:
    def method(self, other: type) -> str:
        if issubclass(other, Base):
            # reveal_type(other) == Type[Base]
            return other.field
        return "Hi"

class C2(Generic[TypeT]):
    def method(self, other: TypeT) -> str:
        if issubclass(other, Base):
            # reveal_type(other) == TypeT`1
            return other.field  # mypy error: "TypeT" has no attribute "field"
        return "Hi"

Actual behavior as of 0.740 included in the comments above. Expected behavior would be to not raise an error on the penultimate line.

@ilevkivskyi
Copy link
Member

Looks like an issue in checker.py (the logic around issubclass() is pretty ad-hoc there). One needs to add an elif for type variables in find_isinstance_check() there.

This might potentially need also an adjustment in meet.py either in narrow_declared_type() or in is_overlapping_types() or in meet_types().

@ilevkivskyi ilevkivskyi added bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal labels Nov 11, 2019
@TH3CHARLie
Copy link
Collaborator

@ilevkivskyi I'd like to take this

@ilevkivskyi
Copy link
Member

I'd like to take this

OK, go ahead.

ilevkivskyi pushed a commit that referenced this issue Nov 14, 2019
Resolves #7920 

The fix is just to add one more special-case to the existing logic. Using this opportunity,
the special-casing logic around `issubclass()` is moved to a helper method.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong false-positive mypy gave an error on correct code priority-1-normal
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants