The problem I am facing is when I am attempting to use SQLAlchelmy ORM declaractions to write my session queries and utilizing VSCode’s recognition of class types to auto complete my coded queries.
Based on my understanding, I have coded an TypeVar declaration in the following format:
import sqlalchemy
from sqlalchemy.orm import sessionmaker
from typing import TypeVar
def generate_schema(prefix: str):
class SchemaA:
tablename: str = f"{prefix}_table"
attr1: int = len(prefix)
return SchemaA
SchemaA = generate_schema("raw")
SchemaAType = TypeVar("SchemaAType", bound=SchemaA)
engine = sqlalchemy.create_engine("xxx")
Session = sessionmaker(engine)
with Session() as sess:
table_a: SchemaAType = sess.query(SchemaA).where(SchemaA.attr1 == 3).first()
prefix_perc: float = table_a.attr1 / len(table_a.tablename)
It used to be able to work properly and VSCode will also be able to map the internal attributes of SchemaA
(as it is a class object) successfully but recently, table_a: SchemaAType
is throwing a linting error: “Variable not allowed in type expression”.
Furthermore, table_a
is also no longer recognized with the internal attributes of SchemaA
variable.
What is it that I am doing wrong now? I cannot seem to find an answer online for this formatting error despite it not throwing any error during runtime.
I have tried researching on using other Var types instead such as NewType but based on documentation, those are not for binding variables to Typing.
Ideally, I would like to avoid defining a generic class for SchemaA
so I can centralize the definition of SchemaA
in one function.
My current python specifications are:
python 3.10.6
typing-extensions==4.9.0
4