-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Mapped property is interpreted to mapped type instead of InstrumentedAttibute on union #10751
Comments
that's going to be a Mypy issue. If you replace InstrumentedAttribute with a plain Python descriptor you will likely get the same result. I'll show you a confirming case in a few (will close this issue). |
adding reveal_type() to your example, here's mypy:
however here's pyright
pyright is correct, mypy is wrong. mypy has a lot of problems dealing with Here's a non-SQLAlchemy example, feel free to report this to mypy: from __future__ import annotations
from typing import Any, overload, Generic, TypeVar, Optional, Union
_T_co = TypeVar("_T_co", bound=Any, covariant=True)
class InstrumentedAttribute(Generic[_T_co]):
@overload
def __get__(
self, instance: None, owner: Any
) -> InstrumentedAttribute[_T_co]:
...
@overload
def __get__(self, instance: object, owner: Any) -> _T_co:
...
def __get__(
self, instance: Optional[object], owner: Any
) -> Union[InstrumentedAttribute[_T_co], _T_co]:
...
def __set__(self, instance: Any, value: Any) -> None:
...
def __delete__(self, instance: Any) -> None:
...
class Foo:
foobar: InstrumentedAttribute[str]
class Bar:
foobar: InstrumentedAttribute[str]
def foo(attribute: InstrumentedAttribute[Any]) -> None:
pass
union: type[Foo | Bar] = Foo
reveal_type(union.foobar)
foo(union.foobar) reproduces in the same way as the previous example for both tools (correct for pyright, wrong for mypy) |
This is issue python/mypy#5570 of mypy Considering it's open since 2018 I'm not sure how soon it will be fixed |
Thanks for yours replies. My apologies for the mistake. |
No problem! |
This will hopefully be fixed by python/mypy#16604 |
thanks for reporting back. No change is required in sqlalchemy, correct? |
Correct - it appears to be a bug only in mypy. |
Ensure stubs packages are not installed
sqlalchemy-stubs
andsqlalchemy2-stubs
are not compatible with v2)Verify if the api is typed
Describe the typing issue
When I have a variable that contain an union of model type (
foo: type[ModelA | ModelB]
) property type is interpreted by mypy as mapped_type instead of InstrumentedAttribute[mapped_type].To Reproduce
Error
Versions
Additional context
sqlalchemy and mypy are installed in venv
requirements.txt
minimal example can be found here https://github.com/Fred-si/sqlalchemy_union_type_issue
The text was updated successfully, but these errors were encountered: