Skip to content
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

dataclass constructor expects Mapped[] values when using _mypy_mapped_attrs #129

Open
iamnoah opened this issue Jun 30, 2021 · 0 comments
Open
Labels
mypy plugin something that has to do with the sqlalchemy mypy plugin

Comments

@iamnoah
Copy link
Contributor

iamnoah commented Jun 30, 2021

Describe the bug

Following the mypy support instructions here and trying variations from here, I cannot find a way to get type checking to work for both the fields as clause elements and the dataclass constructor. Removing _mypy_mapped_attrs results in a usable constructor, but unusable fields for constructing a query.

Expected behavior

That the dataclass constructor would be type checked according to the actual field type annotations. Instead, the presence of _mypy_mapped_attrs causes mypy to expect Mapped values to be passed to the constructor, so creating an instance does not pass type checking.

To Reproduce

from dataclasses import dataclass, field

from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import registry

mapper_registry: registry = registry()

@mapper_registry.mapped
@dataclass
class Foo:
    __tablename__ = "foo"
    __table_args__ = {"schema": "public"}

    __sa_dataclass_metadata_key__ = "sa"

    uuid: str = field(
        metadata={
            "sa": Column(
                UUID,
                primary_key=True,
                nullable=False,
            )
        }
    )

    _mypy_mapped_attrs = [uuid]


foo = Foo(
    uuid="123",
)
[mypy]
plugins = sqlalchemy.ext.mypy.plugin

Error

mypy_test.py:31: error: Argument "uuid" to "Foo" has incompatible type "str"; expected "Mapped[str]"

Versions.

  • OS: Mac OS X
  • Python: 3.9.5
  • SQLAlchemy: 1.4.20
  • mypy: 0.910
  • SQLAlchemy2-stubs: 0.0.2a4

Additional context

Alternative mapping that produces the same error:

from dataclasses import dataclass, field
from typing import TYPE_CHECKING

from sqlalchemy import Column, Table
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import registry

mapper_registry: registry = registry()


@mapper_registry.mapped
@dataclass
class Foo:
    __table__ = Table(
        "foo",
        mapper_registry.metadata,
        Column(
            UUID,
            primary_key=True,
            nullable=False,
        ),
    )
    uuid: str = field()

    if TYPE_CHECKING:
        _mypy_mapped_attrs = [uuid]


foo = Foo(
    uuid="123",
)
@iamnoah iamnoah added the requires triage New issue that requires categorization label Jun 30, 2021
@MaicoTimmerman MaicoTimmerman added mypy plugin something that has to do with the sqlalchemy mypy plugin and removed requires triage New issue that requires categorization labels Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mypy plugin something that has to do with the sqlalchemy mypy plugin
Projects
None yet
Development

No branches or pull requests

2 participants