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

confusing behavior with TypeVars and multiple inheritance checks in 0.770 #8604

Open
chadrik opened this issue Mar 30, 2020 · 0 comments
Open
Labels
topic-inheritance Inheritance and incompatible overrides topic-type-variables

Comments

@chadrik
Copy link
Contributor

chadrik commented Mar 30, 2020

Hi, I was just testing out 0.770 and I'm getting a surprising result:

from typing import TypeVar

class Foo(object):
    pass

FooOrStr = TypeVar('FooOrStr', Foo, str)

def doit(reset: bool, arg: FooOrStr) -> FooOrStr:
    if reset:
        if isinstance(arg, Foo):
            reveal_type(arg)
            arg = Foo()
        elif isinstance(arg, str):
            reveal_type(arg)
            arg = ''
        else:
            raise TypeError()
    return arg
test.py:12: note: Revealed type is 'test.Foo*'
test.py:12: note: Revealed type is 'test.<subclass of "str" and "Foo">'
test.py:13: error: Incompatible types in assignment (expression has type "Foo", variable has type "str")  [assignment]
test.py:15: note: Revealed type is 'builtins.str*'

Based on the latest blog post about 0.770 I suspect this is by design, but what can I do to work around it (short of adding some artificial method to make Foo incompatible with str)?

It would be good to add something to the docs about this, because I think it could be a fairly common trap. Also, I think adjusting the error message to clarify the relationship to multiple inheritance would help, e.g. variable has type Foo and 'test.<subclass of "str" and "Foo">'.

Thanks!

@AlexWaygood AlexWaygood added topic-inheritance Inheritance and incompatible overrides topic-type-variables labels Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-inheritance Inheritance and incompatible overrides topic-type-variables
Projects
None yet
Development

No branches or pull requests

2 participants