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

mypy: error: "Type[X]" has no attribute "Schema" [attr-defined] #243

Open
darwinyip opened this issue Jun 2, 2023 · 1 comment
Open

Comments

@darwinyip
Copy link

mypy complains about the following code:

from dataclasses import dataclass
from typing import Union

from marshmallow_dataclass import add_schema


@add_schema
@dataclass(frozen=True)
class Person:
    name: str
    age: Union[int, float]


Person.Schema().load({"name": "jane", "age": 50.0})

Error:

$ mypy main.py --ignore-missing-imports
app/main.py:14: error: "Type[Person]" has no attribute "Schema"  [attr-defined]
Found 1 error in 1 file (checked 1 source file)

This is not an issue with:

PersonSchema = marshmallow_dataclass.class_schema(Person)
PersonSchema().load({"name": "jane", "age": 50.0})
@mivade
Copy link

mivade commented Jun 2, 2023

It is actually documented in an example that you can add the following to placate type checkers:

Schema: ClassVar[Type[Schema]] = Schema # For the type checker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants