Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python &引用;继承';基础';,这不是一个类;在VS代码中使用SQLAlchemy声明性_base()_Python_Python 3.x_Visual Studio Code_Sqlalchemy - Fatal编程技术网

Python &引用;继承';基础';,这不是一个类;在VS代码中使用SQLAlchemy声明性_base()

Python &引用;继承';基础';,这不是一个类;在VS代码中使用SQLAlchemy声明性_base(),python,python-3.x,visual-studio-code,sqlalchemy,Python,Python 3.x,Visual Studio Code,Sqlalchemy,VS代码将“继承'Base',这不是类”显示为错误消息,如下所示: from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Integer , String , Column Base = declarative_base() class Socio(Base): __tablename__ = 'socios' id = Column(Integer, autoincre

VS代码将“继承'Base',这不是类”显示为错误消息,如下所示:

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))
为什么会发生这种情况?如何修复它?

继承不是类的“Base”实际上不是错误。
相反,它是一个静态分析结果,来自微软的Python语言服务器(反过来,该服务器非常依赖于)进行此类分析。它并不总是准确的:如果一个类是由一个函数动态生成并返回的(这里就是这种情况),那么静态检查工具可能无法正确理解它的类型

如中所述,通过更改以下设置可以禁用此功能:

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

从VS代码1.47开始,当使用Marshmallow序列化/反序列化SQLAlchemy对象并从继承时,请使用来自的解决方案:

似乎不再工作(即,您仍然会得到一个“ma.SQLAlchemyAutoSchema”,它不是一个类。”警告)。您可以在特定行上使用更通用的注释:

ma = Marshmallow(app)

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

但是请注意,VS代码将#noqa视为该行的禁用所有设置。

不要相信linter(或任何其他Python静态分析工具)告诉您的一切:运行代码,看看它是否/如何实际失败(然后在问题中包含该错误消息),而不是假设VS代码告诉了你真相
declarative_base()
确实返回了一个类……也就是说:您发布的内容实际上不是Python的错误。这是VS代码发出的警告。请告诉我们您从Python中得到的实际错误(如果有)。(警告不会阻止代码真正运行;它只是说VS code的分析认为代码可能无法成功运行,但该分析并非来自Python本身,也并非总是正确的)。谢谢您的回答。如果我在python终端中运行,我会得到以下错误:nombre=Column(String(250))Traceback(最近一次调用最后一次):File“”,第1行,in NameError:name'Column'未定义ID您在终端中运行的内容中包括
来自sqlalchemy import Integer,String,Column
?如果我在终端中运行它,我现在没有得到任何错误,但是当我尝试从其他.py文件导入“social”时,我得到了以下错误:“File”/home/gastonpalav/Workspace/frro-soport-2019-08/practico_05/ejercicio_02.py”,第6行,在from practico_05.ejercicio_01导入库中,social modulenofounderror:没有名为“practico_05”的模块“谢谢”。我的烧瓶sqlalchemy模型文件出现了这个错误,这个文件已经处理好了。我从来没有遇到过它正常工作的问题。
ma = Marshmallow(app)

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