Skip to content

Types should be promoted by issubclass #2909

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
chadrik opened this issue Feb 26, 2017 · 2 comments · Fixed by #3005
Closed

Types should be promoted by issubclass #2909

chadrik opened this issue Feb 26, 2017 · 2 comments · Fixed by #3005

Comments

@chadrik
Copy link
Contributor

chadrik commented Feb 26, 2017

I expected types to be promoted to sub-types within an issubclass block, the same as instances within an isinstance block, but type-checking the following code results in the error Type[Vehicle] has no attribute "wheels":

from typing import Type

class Vehicle(object):
    name = ''

class Car(Vehicle):
    wheels = 4

def test_isinstance(x):
    # type: (Vehicle) -> None
    print(x.name)
    if isinstance(x, Car):
        print(x.wheels)

def test_issubclass(x):
    # type: (Type[Vehicle]) -> None
    print(x.name)
    if issubclass(x, Car):
        print(x.wheels)

Is this be possible or am I missing something? I'm testing against the master branch.

@ilevkivskyi
Copy link
Member

This would be an interesting feature. Also I think the implementation would be straightforward, since isinstance is already understood by mypy.

@JukkaL
Copy link
Collaborator

JukkaL commented Feb 27, 2017

Yeah, this would be a reasonable thing to support, though you can currently work around the limitation pretty easily by using cast(Type[Subclass], x) after the issubclass check. Example:

...

def test_issubclass(x):
    # type: (Type[Vehicle]) -> None
    print(x.name)
    if issubclass(x, Car):
        print(cast(Type[car], x).wheels)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants