11"""Custom attributes utilities."""
22from 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
66from .types import MagentoEntity , CustomAttributeDict , Product , Customer , Category
77
88T = 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
1313CATEGORY_ENTITY_TYPE_ID = 3
1616
1717@overload
1818def 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
2424def 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
2929def 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
9090def 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
109109def 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
0 commit comments