Skip to content

Commit

Permalink
Merge pull request #458 from yukinarit/field-order
Browse files Browse the repository at this point in the history
Fix type error from field with attributes
  • Loading branch information
yukinarit authored Jan 6, 2024
2 parents 100b0dd + ed5c98e commit f3e0cdd
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/en/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,6 @@ print(from_json(Foo, s))
```
That's it! pyserde offers many more features. If you're interested, please read the rest of the documentation.
> **NOTE:** which type checker should be used?
> pyserde depends on [PEP681 dataclass_transform](https://peps.python.org/pep-0681/). [mypy](https://github.com/python/mypy) does not fully support dataclass_transform as of Jan. 2024. [pyright](https://github.com/microsoft/pyright) is recommended for codebase using pyserde.
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
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ strict = true
ignore_missing_imports = true
exclude = [
"serde/numpy.py",
"examples/alias.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 f3e0cdd

Please sign in to comment.