Skip to content

MyPy type inference clashes with type annotation #4293

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
alanhdu opened this issue Nov 29, 2017 · 2 comments
Closed

MyPy type inference clashes with type annotation #4293

alanhdu opened this issue Nov 29, 2017 · 2 comments

Comments

@alanhdu
Copy link
Contributor

alanhdu commented Nov 29, 2017

The following code:

import os
from typing import Union, Type

class B():
    pass
class C():
    pass

CLASS: Union[Type[B], Type[C]] = B if "A" in os.environ else C

fails type-checking using mypy==0.550.0 and Python 3.6 with the following error:

Incompatible types in assignment (expression has type "Type[object]", variable has type "Union[Type[B], Type[C]]")

The "use the common base class" as the type is probably a good heuristic in general, but it seems weird that an explicit type annotation with a union fails.

(The context for this is that we have two classes that both implement an ABC interface. At runtime, we switch between them based on some configuration and environment variables, but because MyPy infers the class type as the ABC, we get a Cannot instantiate abstract class error).

@alanhdu alanhdu changed the title MyPy type inference too general MyPy type inference clashes with type annotation Nov 29, 2017
@elazarg
Copy link
Contributor

elazarg commented Nov 30, 2017

Duplicate of #3487

@ilevkivskyi
Copy link
Member

Yes, looks like a duplicate.

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

No branches or pull requests

3 participants