Skip to content

Type is wrongly inferenced #151

Open
@kasium

Description

@kasium

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

from typing import Optional, Type, cast, Union
from sqlalchemy.orm import declarative_base, Mapped
from sqlalchemy import Column, Unicode

Base = declarative_base()

class DummyTable(Base):
    __tablename__ = "user"
    col1:Mapped[Optional[str]] = Column(Unicode, primary_key=True)

class DummyTable2(Base):
    __tablename__ = "user2"
    col1:Mapped[Optional[str]] = Column(Unicode, primary_key=True)

def x(arg: Type[Base]) -> None:
    arg = cast(Type[Union[DummyTable, DummyTable2]], arg)
    arg.col1.int_(1,2)

Error

x.py:17:5: error: Item "str" of "Optional[str]" has no attribute "int_"  [union-attr]
        arg.col1.int_(1,2)
        ^
x.py:17:5: error: Item "None" of "Optional[str]" has no attribute "int_"  [union-attr]
        arg.col1.int_(1,2)
        ^

Versions.
OS: Linux
Python: 3.7.1
SQLAlchemy: 1.4.22
mypy: 0.910
SQLAlchemy2-stubs: 0.0.2a6

Have a nice day!

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingmypy pluginsomething that has to do with the sqlalchemy mypy plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions