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

isinstance(instance, SubClass) loses type instead of narrowing #24

Closed
jeremyBanks opened this issue Mar 26, 2019 · 1 comment
Closed
Labels
addressed in next version Issue is fixed and will appear in next published version

Comments

@jeremyBanks
Copy link

Given an instance of a SuperClass, I would expect isinstance(instance, SubClass) to narrow the type of instance to SubClass in the following branch. Instead, the type appears to have been lost and replaced with NoneType.

class SuperClass:
    def __init__(self) -> None:
        self.property: bool = True

class SubClass(SuperClass):
    pass

def function(instance: SuperClass) -> None:
    if isinstance(instance, SubClass):
        print(instance.property)
$ mypy --strict example.py && echo "no errors"
no errors
$ pyright example.py
Finding source files
Found 1 source files
Analyzed 1 file in 1.229sec
/Users/jbanks/Desktop/example.py
  'property' is not a known member of 'None' (10, 24)
1 error, 0 warnings

For a slightly smaller example, we get the same error narrowing from the base object type:

class MyClass:
    def __init__(self) -> None:
        self.property: bool = True


def function(instance: object) -> None:
    if isinstance(instance, MyClass):
        print(instance.property)
@erictraut
Copy link
Collaborator

This is fixed in 1.0.4, which I just published. Thanks for the bug report.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version
Projects
None yet
Development

No branches or pull requests

2 participants