Skip to content

Types should be promoted by issubclass #2909

Closed

Description

@chadrik

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.

Activity

  1. ilevkivskyi commented on Feb 26, 2017

    @ilevkivskyi
    Member

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

  2. JukkaL commented on Feb 27, 2017

    @JukkaL
    Collaborator

    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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions