question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid base class "Base" when returned by a function

See original GitHub issue

Describe 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:open
  • Created 10 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
zzzeekcommented, Dec 4, 2022

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.

1reaction
zzzeekcommented, Dec 4, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found