Skip to content

isinstance can coerce a known type to become None #4952

@ikelos

Description

@ikelos

Given the following code (mypy 0.590 and python 3.6.5):

from typing import *

class A(object):
    def method(self) -> str:
        return ''

R = TypeVar('R')

class B(object):
    def func(self, instance: A, instance_type: Type[R]) -> None:
        reveal_type(instance)
        if isinstance(instance, instance_type):
            reveal_type(instance)
            instance.method()

I get the following response:

test.py:11: error: Revealed type is 'test.A'
test.py:13: error: Revealed type is 'builtins.None'
test.py:14: error: "None" has no attribute "method"

I would have expected the type of instance to have at the very least been a subclass of A, given that it was before it went into the call. So something like:

test.py:11: error: Revealed type is 'test.A'
test.py:13: error: Revealed type is 'R'

Or perhaps more complex to indicate it subclasses from both A and R, (but I don't know the syntax for that). It makes no difference whether R is bound to A or not.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions