Skip to content

Compatibility of "ordinary" and Optional types #9567

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

Closed
2 tasks done
hubernhard opened this issue Mar 30, 2023 · 1 comment
Closed
2 tasks done

Compatibility of "ordinary" and Optional types #9567

hubernhard opened this issue Mar 30, 2023 · 1 comment
Labels
duplicate This issue or pull request already exists external library/application issues a separate library / application that's not SQLAlchemy has a problem (dependent or dependee) typing pep -484 typing issues. independent of "mypy"

Comments

@hubernhard
Copy link

hubernhard commented Mar 30, 2023

Ensure stubs packages are not installed

  • No sqlalchemy stub packages is installed (both sqlalchemy-stubs and sqlalchemy2-stubs are not compatible with v2)

Verify if the api is typed

  • The api is not in a module listed in #6810 so it should pass type checking

Describe the typing issue

Hi,

in this simple example I try to assign values from a Mapped[int] column into a Mapped[int | None] column. MyPy complains that those types are not compatible. I would assume it should behave similar to ordinary dataclasses where this is possible?

Thank you!

To Reproduce

from sqlalchemy.orm import DeclarativeBase, Mapped, MappedAsDataclass


class Base(MappedAsDataclass, DeclarativeBase):
    pass


class A(Base):
    x: Mapped[int | None]


class B(Base):
    x: Mapped[int]

    def export(self) -> A:
        return A(x=self.x)

Error

error: Argument "x" to "A" has incompatible type "Mapped[int]"; expected "Mapped[Optional[int]]"  [arg-type]

Versions

  • OS: Windows
  • Python: 3.10
  • SQLAlchemy: 2.0.7
  • Type checker: mypy 1.1.1

Additional context

No response

@hubernhard hubernhard added requires triage New issue that requires categorization typing pep -484 typing issues. independent of "mypy" labels Mar 30, 2023
@zzzeek zzzeek added external library/application issues a separate library / application that's not SQLAlchemy has a problem (dependent or dependee) duplicate This issue or pull request already exists and removed requires triage New issue that requires categorization labels Mar 30, 2023
@zzzeek
Copy link
Member

zzzeek commented Mar 30, 2023

hi this is unfortunately a bug in mypy , this issue is a dupe of #9467.

mypy issue is at: python/mypy#13856

SQLAlchemy workarounds documented at : https://docs.sqlalchemy.org/en/20/orm/dataclasses.html#declarative-dataclass-mapping

@zzzeek zzzeek closed this as not planned Won't fix, can't repro, duplicate, stale Mar 30, 2023
sqlalchemy-bot pushed a commit that referenced this issue Mar 30, 2023
Change-Id: Ie25810bfb188794858ba39e246a72b06c484e474
References: #9567
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists external library/application issues a separate library / application that's not SQLAlchemy has a problem (dependent or dependee) typing pep -484 typing issues. independent of "mypy"
Projects
None yet
Development

No branches or pull requests

2 participants