Invalid base class "Base" when returned by a function
See original GitHub issueDescribe the bug mypy returns [Invald ](error: Invalid base class “Base” [misc]) when Base class is returned by a function.
To Reproduce
pip install sqlalchemy[mypy]==1.4.44
mypy --config-file=mypy.ini test.py
mypy.ini
[mypy]
plugins = sqlalchemy.ext.mypy.plugin
test.py
from sqlalchemy import String, select
from sqlalchemy.orm import declarative_base
from typing import Optional, Dict, Type
import sqlalchemy
from sqlalchemy.orm.ext import DeclarativeMeta
def get_base() -> Type[DeclarativeMeta]:
return declarative_base()
Base: Type[DeclarativeMeta] = get_base()
class User(Base):
__tablename__ = "user"
id = sqlalchemy.Column(sqlalchemy.Integer, primary_key=True)
name = sqlalchemy.Column(sqlalchemy.String)
Error
error: Invalid base class "Base"
Versions.
- OS: Ubuntu 22.04
- Python: 3.8
- SQLAlchemy: 1.4.44
- mypy: 0.991
- SQLAlchemy2-stubs: 0.0.2a29
Have a nice day!
Issue Analytics
- State:
- Created 10 months ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Unhelpful error "Invalid base class mypy(error)" #6372 - GitHub
In this case it is because the Base class is dynamic (computed from a function return), therefore mypy cannot use it for static...
Read more >Callable is invalid base class? - python - Stack Overflow
Basically, if a class has a __call__ method, it's implicitly assumed that class is also a callable. Share.
Read more >[class.virtual]
In a derived class, if a virtual member function of a base class subobject has more than one final overrider the program is...
Read more >about Classes - PowerShell | Microsoft Learn
Data returned by a method can be any defined data type. ... The derived class inherits the properties of the base class.
Read more >More types - mypy 0.991 documentation
The callable returned by NewType accepts only one argument; this is equivalent to supporting only one constructor accepting an instance of the base...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
short answer no, there’s nothing different. as long as your Base class is making mappings, it’s doing the thing it’s supposed to do.
the declarative_base() function invokes Python code that is the equivalent operation as doing your “class Base” declaration. It uses the Python type() function to create a new type. But also it could just as well have that same “class Base” declaration inside of it, then return the class; Python typing has no way to represent that returned class as something you can subclass.