3

VS code screenshot

VS Code shows "Inheriting 'Base', which is not a class" as an error message given the below:

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Integer , String , Column

Base = declarative_base()

class Socio(Base):

    __tablename__ = 'socios'
    id = Column(Integer, autoincrement = True , primary_key = True)
    dni = Column(Integer , unique = True)
    nombre = Column(String(250))
    apellido= Column(String(250))

Why does this happen? How can I fix it?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Don't believe everything your linter (or any other static analysis tool for Python) tells you: Run the code and see if/how it **actually** fails (and then include that error message in your question), instead of assuming that VS Code is telling you the truth. `declarative_base()` truly does return a class. – Charles Duffy Jul 26 '19 at 01:12
  • ...which is to say: What you posted **is not actually an error from Python**. It's a warning from VS Code. Please show us the actual error you get from Python, if any. (A warning won't stop the code from really running; it just says that VS Code's analysis thinks it's likely the code won't run successfully, but that analysis isn't coming from Python itself, and it isn't always right). – Charles Duffy Jul 26 '19 at 01:16
  • Thank you for your answer . IF i run in a python terminal i get this error : nombre = Column(String(250)) Traceback (most recent call last): File "", line 1, in NameError: name 'Column' is not defined – Gaston Palavecino Jul 26 '19 at 01:23
  • Did you include `from sqlalchemy import Integer, String, Column` in what you ran in the terminal? – Charles Duffy Jul 26 '19 at 01:42
  • If i run it in terminal i get no errors now, but when i try to import "Socio" from other .py files i get this error : "File "/home/gastonpalav/Workspace/frro-soporte-2019-08/practico_05/ejercicio_02.py", line 6, in from practico_05.ejercicio_01 import Base , Socio ModuleNotFoundError: No module named 'practico_05'" – Gaston Palavecino Jul 26 '19 at 01:55
  • Please show us how you are using "practico_05". – J.K Jul 26 '19 at 02:34
  • That's a completely unrelated error, and should be its own separate question. – Charles Duffy Jul 26 '19 at 02:37

2 Answers2

10

Inheriting 'Base', which is not a class is not actually an error.

Rather, it's a static analysis result coming from Microsoft's Python language server (which in turn leans heavily on pylint) for this kind of analysis. It's not always accurate: If a class is dynamically generated and returned by a function (as is the case here), the static-checking tools may not properly understand its type.

As described in microsoft/python-language-server#1390, this feature can be disabled with the following settings change:

"python.analysis.disabled": [
    "inherit-non-class"
],
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • thank you. I was getting this error with my flask-sqlalchemy models file which this took care of. I have never had a problem with it functioning correctly. – Jeff Bluemel Jul 30 '19 at 15:12
0

As of VS Code 1.47, when using Marshmallow to serialize/deserialize SQLAlchemy objects and inheriting from marshmallow_sqlalchemy.SQLAlchemyAutoSchema, using the solution from the other answer:

"python.analysis.disabled": [
    "inherit-non-class"
],

does not seem to work anymore (i.e. you will still get a "ma.SQLAlchemyAutoSchema', which is not a class." warning). You can instead use the more generic #noqa comment on specific lines:

ma = Marshmallow(app)

class UserSchema(ma.SQLAlchemyAutoSchema):  # noqa
    class Meta:
        model = Person
        sqla_session = db.session

Note though, that VS Code treats #noqa as a disable-all setting for that line.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135