Skip to content

Commit f3d36c5

Browse files
committed
Modernize the code for Python 3.10+
1 parent 4d9de22 commit f3d36c5

11 files changed

Lines changed: 203 additions & 189 deletions

File tree

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
project = 'PyMagento'
2121
# noinspection PyShadowingBuiltins
22-
copyright = '2020-2023, Bixoto'
22+
copyright = '2020-2026, Bixoto'
2323
author = 'Bixoto'
2424

2525
# The full version, including alpha/beta/rc tags

magento/attributes.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Custom attributes utilities."""
22
from collections import OrderedDict
3-
from typing import Callable, Optional, cast, Union, List, Tuple, Iterable, OrderedDict as OrderedDictType, overload, \
4-
TypeVar, Any
3+
from collections.abc import Callable, Iterable
4+
from typing import cast, overload, TypeVar, Any
55

66
from .types import MagentoEntity, CustomAttributeDict, Product, Customer, Category
77

88
T = TypeVar('T')
99

10-
Item = TypeVar('Item', bound=Union[Category, Customer, Product, MagentoEntity])
10+
Item = TypeVar('Item', bound=Category | Customer | Product | MagentoEntity)
1111

1212
# From Magento
1313
CATEGORY_ENTITY_TYPE_ID = 3
@@ -16,18 +16,18 @@
1616

1717
@overload
1818
def get_custom_attribute(item: Item, attribute_code: str,
19-
coerce_as: Callable[[str], T]) -> Union[None, T, List[T]]: # pragma: nocover
19+
coerce_as: Callable[[str], T]) -> None | T | list[T]: # pragma: nocover
2020
...
2121

2222

2323
@overload
2424
def get_custom_attribute(item: Item, attribute_code: str) \
25-
-> Union[None, str, List[str]]: # pragma: nocover
25+
-> None | str | list[str]: # pragma: nocover
2626
...
2727

2828

2929
def get_custom_attribute(item: Item, attribute_code: str,
30-
coerce_as: Union[Callable[[str], Any], None] = None) -> Any:
30+
coerce_as: Callable[[str], Any] | None = None) -> Any:
3131
"""Get a custom attribute from an item given its code.
3232
3333
For example:
@@ -48,7 +48,7 @@ def get_custom_attribute(item: Item, attribute_code: str,
4848
def coerce_as(s: str) -> bool:
4949
return bool(int(s))
5050

51-
attributes = cast(List[CustomAttributeDict], item.get("custom_attributes", []))
51+
attributes = cast(list[CustomAttributeDict], item.get("custom_attributes", []))
5252
for attribute in attributes:
5353
if attribute["attribute_code"] == attribute_code:
5454
value = attribute["value"]
@@ -62,21 +62,21 @@ def coerce_as(s: str) -> bool:
6262
return None
6363

6464

65-
def get_boolean_custom_attribute(item: Item, attribute_code: str) -> Optional[bool]:
65+
def get_boolean_custom_attribute(item: Item, attribute_code: str) -> bool | None:
6666
"""Equivalent of ``get_custom_attribute(item, attribute_code, coerce_as=bool)`` with proper typing."""
67-
return cast(Optional[bool], get_custom_attribute(item, attribute_code, coerce_as=bool))
67+
return cast(bool | None, get_custom_attribute(item, attribute_code, coerce_as=bool))
6868

6969

70-
def get_custom_attributes_dict(item: Item) -> OrderedDictType[str, Union[List[str], str, None]]:
70+
def get_custom_attributes_dict(item: Item) -> OrderedDict[str, list[str] | str | None]:
7171
"""Get all custom attributes from an item as an ordered dict of code->value."""
7272
d = OrderedDict()
73-
for attribute in cast(List[CustomAttributeDict], item.get("custom_attributes", [])):
73+
for attribute in cast(list[CustomAttributeDict], item.get("custom_attributes", [])):
7474
d[attribute["attribute_code"]] = attribute["value"]
7575

7676
return d
7777

7878

79-
def serialize_attribute_value(value: Union[str, int, float, bool, None], force_none: bool = False) -> Optional[str]:
79+
def serialize_attribute_value(value: str | int | float | bool | None, force_none: bool = False) -> str | None:
8080
"""Serialize a value to be stored in a Magento attribute."""
8181
if isinstance(value, bool):
8282
return "1" if value else "0"
@@ -88,7 +88,7 @@ def serialize_attribute_value(value: Union[str, int, float, bool, None], force_n
8888

8989

9090
def set_custom_attribute(item: Item, attribute_code: str,
91-
attribute_value: Union[str, int, float, bool, None],
91+
attribute_value: str | int | float | bool | None,
9292
*, force_none: bool = False) -> Item:
9393
"""Set a custom attribute in an item dict.
9494
@@ -107,7 +107,7 @@ def set_custom_attribute(item: Item, attribute_code: str,
107107

108108

109109
def set_custom_attributes(item: Item,
110-
attributes: Iterable[Tuple[str, Union[str, int, float, bool, None]]],
110+
attributes: Iterable[tuple[str, str | int | float | bool | None]],
111111
*, force_none: bool = False) -> Item:
112112
"""Set custom attributes in an item dict.
113113
Like ``set_custom_attribute`` but with an iterable of attributes.
@@ -117,7 +117,7 @@ def set_custom_attributes(item: Item,
117117
:param force_none: see ``set_custom_attribute`` for usage.
118118
:return: the modified item dict.
119119
"""
120-
item_custom_attributes = cast(List[CustomAttributeDict], item.get("custom_attributes", []))
120+
item_custom_attributes = cast(list[CustomAttributeDict], item.get("custom_attributes", []))
121121

122122
attributes_index = {attribute["attribute_code"]: index for index, attribute in enumerate(item_custom_attributes)}
123123

magento/batches.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from typing import List, Callable, Iterable, TypeVar, Generic, Optional, Any, Dict, Iterator
2-
from typing_extensions import Self
1+
from collections.abc import Callable, Iterable, Iterator
2+
from typing import TypeVar, Generic, Any
33

44
from api_session import JSONDict
5+
from typing_extensions import Self
56

67
from .client import Magento
78
from .queries import make_field_value_query
@@ -17,7 +18,7 @@ def __init__(self, client: Magento, api_path: str, batch_size: int = BATCH_SIZE)
1718
self.client = client
1819
self.path = api_path
1920
self.batch_size = batch_size
20-
self._batch: List[MagentoEntity] = []
21+
self._batch: list[MagentoEntity] = []
2122
# some stats
2223
self._sent_batches = 0
2324
self._sent_items = 0
@@ -30,7 +31,7 @@ def add_item(self, item_data: MagentoEntity) -> None:
3031
if len(self._batch) >= self.batch_size:
3132
self.send_batch()
3233

33-
def send_batch(self) -> Optional[JSONDict]:
34+
def send_batch(self) -> JSONDict | None:
3435
"""Send the current pending batch (if any) and return the response from the Magento API."""
3536
if not self._batch:
3637
return None
@@ -45,7 +46,7 @@ def _put_batch(self) -> JSONDict: # pragma: nocover
4546
res: JSONDict = self.client.put_json_api(self.path, json=self._batch, async_bulk=True)
4647
return res
4748

48-
def finalize(self) -> Dict[str, int]:
49+
def finalize(self) -> dict[str, int]:
4950
"""Send the last pending batch (if any). This doesn’t need to be called when the object is used as a context
5051
manager.
5152
@@ -93,7 +94,7 @@ def __init__(self, getter: Callable[..., Iterable[T]], key_field: str, keys: Ite
9394
self.getter = getter
9495
self.key_field = key_field
9596
self.keys = keys
96-
self._batch: List[str] = []
97+
self._batch: list[str] = []
9798

9899
def _get_batch(self) -> Iterable[T]:
99100
if not self._batch:

0 commit comments

Comments
 (0)