Skip to content

Commit

Permalink
Fix type error from field with attributes
Browse files Browse the repository at this point in the history
This commit fixes pyright type error from field with attributes.
You still get type error with mypy v1.8.0 as mypy still does not
fully support dataclass_transform
```
from dataclasses import dataclass
from serde import serde, field

@serde
@DataClass
class Foo:
    id: int = field(rename="ID")
    comments: str
```
  • Loading branch information
yukinarit committed Jan 6, 2024
1 parent 100b0dd commit 084cb2e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions examples/field_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from dataclasses import dataclass
from serde import serde, field


@serde
@dataclass
class Foo:
id: int = field(rename="ID") # Field with attributes can be defined before other fields
# thanks to dataclass_transform field_specifiers
# https://peps.python.org/pep-0681/#the-dataclass-transform-decorator
# NOTE: you still get error with mypy as of mypy v1.8.0
comments: str


if __name__ == "__main__":
pass
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ strict = true
ignore_missing_imports = true
exclude = [
"serde/numpy.py",
"examples/field_order.py",
"bench",
"tests"
]
Expand Down
2 changes: 1 addition & 1 deletion serde/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def serde(
...


@dataclass_transform() # type: ignore
@dataclass_transform(field_specifiers=(field,)) # type: ignore
def serde(
_cls: Any = None,
rename_all: Optional[str] = None,
Expand Down

0 comments on commit 084cb2e

Please sign in to comment.