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

annotating type instance #2799

Closed
tumb1er opened this issue Feb 3, 2017 · 2 comments
Closed

annotating type instance #2799

tumb1er opened this issue Feb 3, 2017 · 2 comments

Comments

@tumb1er
Copy link

tumb1er commented Feb 3, 2017

I'm trying to annotate simple class factory, but constantly getting "Invalid base class" error. Only annotating the result of factory() call with Any return type removes errors, but there is an interesting thing at line 26.
Revealed type for class A is a function that returns A instance, but you can't inherit from real function.
So, two questions:

  • how to annotate factory to return something that is "valid base class"?
  • may be mypy should distinguish type instance and class instance "constructor"?
from typing import Any


class A:
    pass


def factory() -> Any:
    return A


base = factory()  # type: Any
# removing forced Any leads to invalid base class error

reveal_type(base)
# factory.py:15: error: Revealed type is 'Any'


class B(base):
    pass


var = A


reveal_type(var)
# factory.py:26: error: Revealed type is 'def () -> factory.A'


class C(var):
    pass
@ilevkivskyi
Copy link
Member

Normally you would annotate a function that returns a class as

def factory() -> Type[A]:
    return A

a = factory()() # a is correctly inferred to have type A

I think the problem is that mypy currently does not support expressions in base classes or other dynamic features see e.g. #1764

@tumb1er
Copy link
Author

tumb1er commented Feb 6, 2017

Found dynamic base classes in unsupported features, so my questions are now about these features only. Without dynamic base classes, all annotations look reasonable.

@tumb1er tumb1er closed this as completed Feb 6, 2017
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

2 participants