Open kasium opened 3 years ago
I've come across the same issue, and it seems to be related to Union
. It is also reproducible by using a Union
with the type itself:
from typing import Type, Union
from sqlalchemy import Column, String
from sqlalchemy.orm import declarative_base, Mapped
Base = declarative_base()
class Foo(Base):
id = Column(String, primary_key=True)
data: Mapped[str] = Column(String, nullable=False)
class Bar(Base):
id = Column(String, primary_key=True)
data: Mapped[str] = Column(String, nullable=False)
def do_something(mapped: Type[Union[Foo, Foo]]) -> None:
mapped.data.in_(["a", "b"])
Result is:
error: "str" has no attribute "in_"
Using any of the mapped classes outside of Union
works fine.
I think I'm noticing similar issue when trying to get column name via .key
....since Column is typed as string, .key
can't be called on it resulting
class UserModel(Base):
__tablename__ = "users"
username = Column(String, unique=True, index=True)
self.get_user_by_username = load_by_foreign_key(
UserModel, UserModel.username.key
)
Pycharm is complaining: Unresolved attribute reference 'key' for class 'str'
Describe the bug Not sure how to exactly explain it, but if I use a union, mapped and cast together, there is something wrong.
Expected behavior No mypy error
To Reproduce
Error
Versions. OS: Linux Python: 3.7.1 SQLAlchemy: 1.4.22 mypy: 0.910 SQLAlchemy2-stubs: 0.0.2a6
Have a nice day!