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

"Invalid base class" when base class is returned from a function. #4284

Closed
erikwright opened this issue Nov 24, 2017 · 3 comments
Closed

"Invalid base class" when base class is returned from a function. #4284

erikwright opened this issue Nov 24, 2017 · 3 comments

Comments

@erikwright
Copy link

OTT-ErikWright:crucible erikwright$ cat -n ~/mypy_test.py 
     1  import typing
     2
     3  def c() -> type:
     4      class C:
     5          pass
     6      return C
     7
     8  class D(c()):
     9      pass
OTT-ErikWright:crucible erikwright$ mypy ~/mypy_test.py 
/Users/erikwright/mypy_test.py:8: error: Invalid base class

I also tried returning typing.Any, which seemed like it might work based on #2477 (comment) .

@ethanhs
Copy link
Collaborator

ethanhs commented Nov 24, 2017

This is explicitly listed in the wiki as unsupported see https://github.com/python/mypy/wiki/Unsupported-Python-Features. It would be challenging to correctly type your example as of now.

@erikwright
Copy link
Author

erikwright commented Nov 24, 2017

I don't really expect mypy to be able to do any meaningful type checking, but it would be convenient if it would at least understand that c() returns a type, and just didn't try to do any checking (as it would if c() were replaced with a variable of type typing.Any.

@gvanrossum
Copy link
Member

Understood, mypy uses several passes, and currently the return type of c() is not available at the moment it needs the type for the base class of D. Base classes are required to be known in an earlier pass because knowing the bases of a class (actually, its full MRO) is needed to type-check other code.

Your best work-around in this case os to put # type: ignore on the line that causes the error.

Cj-bc added a commit to Cj-bc/backend that referenced this issue Mar 9, 2019
If 'base class is returned from a function', mypy won't treat it
propery.
This is reported and commented by collaborator as *Unsupported feature*
python/mypy#4284
schmir added a commit to trustlines-protocol/tlbc-monitor that referenced this issue Mar 12, 2019
Otherwise mypy complains 'error: Invalid base class' for this code:

,----
| class Block(Base):
|     __tablename__ = "blocks"
| ...
`----

see python/mypy#4284
HiromuHota pushed a commit to HiromuHota/fonduer that referenced this issue Aug 14, 2019
Mypy does not support a base class that is returned from a function.
See https://github.com/python/mypy/wiki/Unsupported-Python-Features for details
Similar reported cases:
python/mypy#4284
python/mypy#6372
lukehsiao pushed a commit to HazyResearch/fonduer that referenced this issue Aug 16, 2019
Mypy does not support a base class that is returned from a function.
See https://github.com/python/mypy/wiki/Unsupported-Python-Features for details
Similar reported cases:
python/mypy#4284
python/mypy#6372
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