Skip to content

Commit

Permalink
Merge pull request #503 from yukinarit/drop-python3.8
Browse files Browse the repository at this point in the history
Drop python 3.8 and use PEP585 entirely
  • Loading branch information
yukinarit authored Mar 30, 2024
2 parents 70c6d4c + 9fd6b15 commit 19f7695
Show file tree
Hide file tree
Showing 80 changed files with 655 additions and 1,028 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
matrix:
# Remove pypy-3.8 until it supports numpy
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] # "pypy-3.8"
python-version: ["3.9", "3.10", "3.11", "3.12"] # "pypy-3.8"
steps:
- uses: actions/checkout@v4
- name: Install poetry
Expand Down
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ Declare a class with pyserde's `@serde` decorator.

```python
@serde
@dataclass
class Foo:
i: int
s: str
Expand Down Expand Up @@ -58,8 +57,8 @@ Foo(i=10, s='foo', f=100.0, b=True)
- Supported types
- Primitives (`int`, `float`, `str`, `bool`)
- Containers
- `List`, `Set`, `Tuple`, `Dict`
- [`FrozenSet`](https://docs.python.org/3/library/stdtypes.html#frozenset), [`DefaultDict`](https://docs.python.org/3/library/collections.html#collections.defaultdict)
- `list`, `set`, `tuple`, `dict`
- [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset), [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict)
- [`typing.Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)
- [`typing.Union`](https://docs.python.org/3/library/typing.html#typing.Union)
- User defined class with [`@dataclass`](https://docs.python.org/3/library/dataclasses.html)
Expand Down
4 changes: 2 additions & 2 deletions bench/attrs_class.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
from functools import partial
from typing import List, Union
from typing import Union

import attr
import data
Expand All @@ -17,7 +17,7 @@ class Small:

@attr.s(auto_attribs=True)
class Medium:
inner: List[Small] = attr.Factory(list)
inner: list[Small] = attr.Factory(list)


SMALL = Small(**data.args_sm)
Expand Down
4 changes: 0 additions & 4 deletions docs/en/class-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ print(to_json(f))
>
> ```python
> @serde(rename_all = 'camelcase')
> @dataclass
> class Foo:
> int_field: int
> str_field: str = field(rename='str-field')
Expand Down Expand Up @@ -86,7 +85,6 @@ class MyDeserializer:
return datetime.strptime(value, "%d/%m/%y")
@serde(class_serializer=MySerializer(), class_deserializer=MyDeserializer())
@dataclass
class Foo:
v: datetime
```
Expand Down Expand Up @@ -143,7 +141,6 @@ def deserializer(cls, o):
raise SerdeSkip()
@serde(serializer=serializer, deserializer=deserializer)
@dataclass
class Foo:
a: datetime
```
Expand All @@ -160,7 +157,6 @@ New in v0.9.8. Since `dataclasses.fields` doesn't include a class variable [^1],

```python
@serde(serialize_class_var=True)
@dataclass
class Foo:
a: ClassVar[int] = 10
```
Expand Down
16 changes: 3 additions & 13 deletions docs/en/field-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Field attributes are options to customize (de)serialization behaviour for a fiel
class Foo:
a: int = 10
b: int = field(default=10) # same as field "a"
c: Dict[str, int] = field(default_factory=dict)
c: dict[str, int] = field(default_factory=dict)

print(from_dict(Foo, {})) # prints Foo(a=10, b=10, c={})
```
Expand All @@ -32,7 +32,6 @@ Here is an example specifying `rename` attribute in both `serde.field` and `data

```python
@serde.serde
@dataclass.dataclass
class Foo:
a: str = serde.field(rename="A")
b: str = dataclasses.field(metadata={"serde_rename"="B"})
Expand All @@ -44,7 +43,6 @@ class Foo:

```python
@serde
@dataclass
class Foo:
id: int = field(rename="ID")
```
Expand All @@ -57,11 +55,10 @@ See [examples/rename.py](https://github.com/yukinarit/pyserde/blob/main/examples

```python
@serde
@dataclass
class Resource:
name: str
hash: str
metadata: Dict[str, str] = field(default_factory=dict, skip=True)
metadata: dict[str, str] = field(default_factory=dict, skip=True)
```

See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/skip.py) for the complete example.
Expand All @@ -72,7 +69,6 @@ See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/s

```python
@serde
@dataclass
class World:
buddy: str = field(default='', skip_if=lambda v: v == 'Pikachu')
```
Expand All @@ -85,9 +81,8 @@ See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/s

```python
@serde
@dataclass
class World:
enemies: List[str] = field(default_factory=list, skip_if_false=True)
enemies: list[str] = field(default_factory=list, skip_if_false=True)
```

See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/skip.py) for the complete example.
Expand All @@ -98,7 +93,6 @@ See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/s

```python
@serde
@dataclass
class World:
town: str = field(default='Masara Town', skip_if_default=True)
```
Expand All @@ -111,7 +105,6 @@ You can set aliases for field names. Alias only works for deserialization.

```python
@serde
@dataclass
class Foo:
a: str = field(alias=["b", "c"])
```
Expand All @@ -130,7 +123,6 @@ In the following example, field `a` is serialized into `"2021-01-01T00:00:00"` b

```python
@serde
@dataclass
class Foo:
a: datetime
b: datetime = field(serializer=lambda x: x.strftime('%d/%m/%y'), deserializer=lambda x: datetime.strptime(x, '%d/%m/%y'))
Expand All @@ -144,13 +136,11 @@ You can flatten the fields of the nested structure.

```python
@serde
@dataclass
class Bar:
c: float
d: bool

@serde
@dataclass
class Foo:
a: int
b: str
Expand Down
7 changes: 1 addition & 6 deletions docs/en/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Installation

Install pyserde from PyPI. pyserde requires Python>=3.8.
Install pyserde from PyPI. pyserde requires Python>=3.9.

```
pip install pyserde
Expand Down Expand Up @@ -48,11 +48,9 @@ Here are the available extras
Define your class with pyserde's `@serde` decorators. Be careful that module name is `serde`, not `pyserde`. `pyserde` heavily depends on the standard library's `dataclasses` module. If you are new to dataclass, I would recommend to read [dataclasses documentation](https://docs.python.org/3/library/dataclasses.html) first.

```python
from dataclasses import dataclass
from serde import serde

@serde
@dataclass
class Foo:
i: int
s: str
Expand All @@ -66,11 +64,9 @@ pyserde generates methods necessary for (de)serialization by `@serde` when a cla
>
> e.g. If you do only serialization, you can use `@serialize` decorator. But calling deserialize API e.g. `from_json` for `Foo` will raise an error.
> ```python
> from dataclasses import dataclass
> from serde import serialize
>
> @serialize
> @dataclass
> class Foo:
> i: int
> s: str
Expand All @@ -83,7 +79,6 @@ pyserde generates methods necessary for (de)serialization by `@serde` when a cla
[PEP585](https://www.python.org/dev/peps/pep-0585/) style annotation is supported for python>=3.9. [PEP604](https://www.python.org/dev/peps/pep-0604/) Union operator is also supported for python>=3.10. With PEP585 and PEP604, you can write a pyserde class pretty neatly.
```python
@serde
@dataclass
class Foo:
a: int
b: list[str]
Expand Down
6 changes: 2 additions & 4 deletions docs/en/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Here is the list of the supported types. See the simple example for each type in

* Primitives (int, float, str, bool) [^1]
* Containers
* `List`, `Set`, `Tuple`, `Dict` [^2]
* [`FrozenSet`](https://docs.python.org/3/library/stdtypes.html#frozenset), [^3] [`DefaultDict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) [^4]
* `list`, `set`, `tuple`, `dict` [^2]
* [`frozenset`](https://docs.python.org/3/library/stdtypes.html#frozenset), [^3] [`defaultdict`](https://docs.python.org/3/library/collections.html#collections.defaultdict) [^4]
* [`typing.Optional`](https://docs.python.org/3/library/typing.html#typing.Optional) [^5]
* [`typing.Union`](https://docs.python.org/3/library/typing.html#typing.Union) [^6] [^7] [^8]
* User defined class with [`@dataclass`](https://docs.python.org/3/library/dataclasses.html) [^9] [^10]
Expand All @@ -28,12 +28,10 @@ Here is the list of the supported types. See the simple example for each type in
You can write pretty complex class like this:
```python
@serde
@dataclass
class bar:
i: int

@serde
@dataclass
class Foo:
a: int
b: list[str]
Expand Down
12 changes: 1 addition & 11 deletions docs/en/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ This is the default Union representation for pyserde<0.7. Given these dataclasse

```python
@serde
@dataclass
class Bar:
b: int

@serde
@dataclass
class Baz:
b: int

@serde(tagging=Untagged)
@dataclass
class Foo:
a: Union[Bar, Baz]
```
Expand All @@ -31,7 +28,6 @@ This is the default Union representation since 0.7. A class declaration with `Ex

```python
@serde(tagging=ExternalTagging)
@dataclass
class Foo:
a: Union[Bar, Baz]
```
Expand All @@ -42,7 +38,7 @@ class Foo:
> @serde(tagging=ExternalTagging)
> @dataclass
> class Foo:
> a: Union[List[int], Set[int]]
> a: Union[list[int], set[int]]
> ```
## `InternalTagging`
Expand All @@ -51,7 +47,6 @@ A class declaration with `InternalTagging` looks like below. If you serialize `F
```python
@serde(tagging=InternalTagging("type"))
@dataclass
class Foo:
a: Union[Bar, Baz]
```
Expand All @@ -62,7 +57,6 @@ A class declaration with `AdjacentTagging` looks like below. If you serialize `F

```python
@serde(tagging=AdjacentTagging("type", "content"))
@dataclass
class Foo:
a: Union[Bar, Baz]
```
Expand All @@ -75,12 +69,10 @@ Passing Union types directly in (de)serialize APIs (e.g. to_json, from_json) was

```python
@serde
@dataclass
class Foo:
a: int

@serde
@dataclass
class Bar:
a: int

Expand All @@ -96,12 +88,10 @@ Since v0.12.0, pyserde can handle union that's passed in (de)serialize APIs a bi

```python
@serde
@dataclass
class Foo:
a: int

@serde
@dataclass
class Bar:
a: int

Expand Down
3 changes: 0 additions & 3 deletions examples/alias.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
from dataclasses import dataclass

from serde import field, serde
from serde.json import from_json


@serde
@dataclass
class Foo:
a: int = field(alias=["b", "c", "d"])

Expand Down
3 changes: 0 additions & 3 deletions examples/any.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from dataclasses import dataclass
from typing import Any

from serde import serde
from serde.json import from_json, to_json


@serde
@dataclass
class Bar:
v: float


@serde
@dataclass
class Foo:
a: Any
b: Any
Expand Down
7 changes: 2 additions & 5 deletions examples/class_var.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
from dataclasses import dataclass
from typing import ClassVar, List
from typing import ClassVar

from serde import serde
from serde.json import from_json, to_json


@serde
@dataclass
class Bar:
v: int


@serde(serialize_class_var=True)
@dataclass
class Foo:
a: ClassVar[int] = 10
b: ClassVar[Bar] = Bar(100)
c: ClassVar[List[Bar]] = [Bar(1), Bar(2)]
c: ClassVar[list[Bar]] = [Bar(1), Bar(2)]


def main() -> None:
Expand Down
Loading

0 comments on commit 19f7695

Please sign in to comment.