From c1676776112f06f6dd10b09d373eddbdb2dc8b99 Mon Sep 17 00:00:00 2001 From: Lorenzo Sciuto Date: Thu, 4 Aug 2022 16:12:03 +0200 Subject: [PATCH 1/3] added missing dataclasses arguments kw_only, slots, match_args --- marshmallow_dataclass/__init__.py | 139 ++++++++++++++++-------------- setup.py | 12 +-- 2 files changed, 80 insertions(+), 71 deletions(-) diff --git a/marshmallow_dataclass/__init__.py b/marshmallow_dataclass/__init__.py index f2014af..cd5e362 100644 --- a/marshmallow_dataclass/__init__.py +++ b/marshmallow_dataclass/__init__.py @@ -66,7 +66,6 @@ class User: from marshmallow_dataclass.lazy_class_attribute import lazy_class_attribute - __all__ = ["dataclass", "add_schema", "class_schema", "field_for_schema", "NewType"] NoneType = type(None) @@ -84,29 +83,35 @@ class User: @overload def dataclass( - _cls: Type[_U], - *, - repr: bool = True, - eq: bool = True, - order: bool = False, - unsafe_hash: bool = False, - frozen: bool = False, - base_schema: Optional[Type[marshmallow.Schema]] = None, - cls_frame: Optional[types.FrameType] = None, + _cls: Type[_U], + *, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, + match_args=True, + kw_only=False, + slots=False, + base_schema: Optional[Type[marshmallow.Schema]] = None, + cls_frame: Optional[types.FrameType] = None, ) -> Type[_U]: ... @overload def dataclass( - *, - repr: bool = True, - eq: bool = True, - order: bool = False, - unsafe_hash: bool = False, - frozen: bool = False, - base_schema: Optional[Type[marshmallow.Schema]] = None, - cls_frame: Optional[types.FrameType] = None, + *, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, + match_args=True, + kw_only=False, + slots=False, + base_schema: Optional[Type[marshmallow.Schema]] = None, + cls_frame: Optional[types.FrameType] = None, ) -> Callable[[Type[_U]], Type[_U]]: ... @@ -115,15 +120,18 @@ def dataclass( # underscore. The presence of _cls is used to detect if this # decorator is being called with parameters or not. def dataclass( - _cls: Type[_U] = None, - *, - repr: bool = True, - eq: bool = True, - order: bool = False, - unsafe_hash: bool = False, - frozen: bool = False, - base_schema: Optional[Type[marshmallow.Schema]] = None, - cls_frame: Optional[types.FrameType] = None, + _cls: Type[_U] = None, + *, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, + match_args=True, + kw_only=False, + slots=False, + base_schema: Optional[Type[marshmallow.Schema]] = None, + cls_frame: Optional[types.FrameType] = None, ) -> Union[Type[_U], Callable[[Type[_U]], Type[_U]]]: """ This decorator does the same as dataclasses.dataclass, but also applies :func:`add_schema`. @@ -152,7 +160,8 @@ def dataclass( """ # dataclass's typing doesn't expect it to be called as a function, so ignore type check dc = dataclasses.dataclass( # type: ignore - _cls, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen + _cls, repr=repr, eq=eq, order=order, unsafe_hash=unsafe_hash, frozen=frozen, match_args=match_args, + kw_only=kw_only, slots=slots ) if not cls_frame: current_frame = inspect.currentframe() @@ -172,16 +181,16 @@ def add_schema(_cls: Type[_U]) -> Type[_U]: @overload def add_schema( - base_schema: Type[marshmallow.Schema] = None, + base_schema: Type[marshmallow.Schema] = None, ) -> Callable[[Type[_U]], Type[_U]]: ... @overload def add_schema( - _cls: Type[_U], - base_schema: Type[marshmallow.Schema] = None, - cls_frame: types.FrameType = None, + _cls: Type[_U], + base_schema: Type[marshmallow.Schema] = None, + cls_frame: types.FrameType = None, ) -> Type[_U]: ... @@ -221,9 +230,9 @@ def decorator(clazz: Type[_U]) -> Type[_U]: def class_schema( - clazz: type, - base_schema: Optional[Type[marshmallow.Schema]] = None, - clazz_frame: types.FrameType = None, + clazz: type, + base_schema: Optional[Type[marshmallow.Schema]] = None, + clazz_frame: types.FrameType = None, ) -> Type[marshmallow.Schema]: """ Convert a class to a marshmallow schema @@ -360,9 +369,9 @@ def class_schema( @lru_cache(maxsize=MAX_CLASS_SCHEMA_CACHE_SIZE) def _internal_class_schema( - clazz: type, - base_schema: Optional[Type[marshmallow.Schema]] = None, - clazz_frame: types.FrameType = None, + clazz: type, + base_schema: Optional[Type[marshmallow.Schema]] = None, + clazz_frame: types.FrameType = None, ) -> Type[marshmallow.Schema]: _RECURSION_GUARD.seen_classes[clazz] = clazz.__name__ try: @@ -419,20 +428,20 @@ def _internal_class_schema( def _field_by_type( - typ: Union[type, Any], base_schema: Optional[Type[marshmallow.Schema]] + typ: Union[type, Any], base_schema: Optional[Type[marshmallow.Schema]] ) -> Optional[Type[marshmallow.fields.Field]]: return ( - base_schema and base_schema.TYPE_MAPPING.get(typ) - ) or marshmallow.Schema.TYPE_MAPPING.get(typ) + base_schema and base_schema.TYPE_MAPPING.get(typ) + ) or marshmallow.Schema.TYPE_MAPPING.get(typ) def _field_by_supertype( - typ: Type, - default: Any, - newtype_supertype: Type, - metadata: dict, - base_schema: Optional[Type[marshmallow.Schema]], - typ_frame: Optional[types.FrameType], + typ: Type, + default: Any, + newtype_supertype: Type, + metadata: dict, + base_schema: Optional[Type[marshmallow.Schema]], + typ_frame: Optional[types.FrameType], ) -> marshmallow.fields.Field: """ Return a new field for fields based on a super field. (Usually spawned from NewType) @@ -485,10 +494,10 @@ def _generic_type_add_any(typ: type) -> type: def _field_for_generic_type( - typ: type, - base_schema: Optional[Type[marshmallow.Schema]], - typ_frame: Optional[types.FrameType], - **metadata: Any, + typ: type, + base_schema: Optional[Type[marshmallow.Schema]], + typ_frame: Optional[types.FrameType], + **metadata: Any, ) -> Optional[marshmallow.fields.Field]: """ If the type is a generic interface, resolve the arguments and construct the appropriate Field. @@ -592,11 +601,11 @@ def _field_for_generic_type( def field_for_schema( - typ: type, - default=marshmallow.missing, - metadata: Mapping[str, Any] = None, - base_schema: Optional[Type[marshmallow.Schema]] = None, - typ_frame: Optional[types.FrameType] = None, + typ: type, + default=marshmallow.missing, + metadata: Mapping[str, Any] = None, + base_schema: Optional[Type[marshmallow.Schema]] = None, + typ_frame: Optional[types.FrameType] = None, ) -> marshmallow.fields.Field: """ Get a marshmallow Field corresponding to the given python type. @@ -723,17 +732,17 @@ def field_for_schema( forward_reference = getattr(typ, "__forward_arg__", None) nested = ( - nested_schema - or forward_reference - or _RECURSION_GUARD.seen_classes.get(typ) - or _internal_class_schema(typ, base_schema, typ_frame) + nested_schema + or forward_reference + or _RECURSION_GUARD.seen_classes.get(typ) + or _internal_class_schema(typ, base_schema, typ_frame) ) return marshmallow.fields.Nested(nested, **metadata) def _base_schema( - clazz: type, base_schema: Optional[Type[marshmallow.Schema]] = None + clazz: type, base_schema: Optional[Type[marshmallow.Schema]] = None ) -> Type[marshmallow.Schema]: """ Base schema factory that creates a schema for `clazz` derived either from `base_schema` @@ -771,10 +780,10 @@ def _get_field_default(field: dataclasses.Field): def NewType( - name: str, - typ: Type[_U], - field: Optional[Type[marshmallow.fields.Field]] = None, - **kwargs, + name: str, + typ: Type[_U], + field: Optional[Type[marshmallow.fields.Field]] = None, + **kwargs, ) -> Callable[[_U], _U]: """NewType creates simple unique types to which you can attach custom marshmallow attributes. diff --git a/setup.py b/setup.py index 8b80afa..9b3d911 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = "8.5.8" +VERSION = "8.5.9" CLASSIFIERS = [ "Development Status :: 4 - Beta", @@ -33,11 +33,11 @@ ], } EXTRAS_REQUIRE["dev"] = ( - EXTRAS_REQUIRE["enum"] - + EXTRAS_REQUIRE["union"] - + EXTRAS_REQUIRE["lint"] - + EXTRAS_REQUIRE["docs"] - + EXTRAS_REQUIRE["tests"] + EXTRAS_REQUIRE["enum"] + + EXTRAS_REQUIRE["union"] + + EXTRAS_REQUIRE["lint"] + + EXTRAS_REQUIRE["docs"] + + EXTRAS_REQUIRE["tests"] ) setup( From aebb9b72ac142a6e11de3befce54a1b22c871439 Mon Sep 17 00:00:00 2001 From: Lorenzo Sciuto Date: Tue, 30 Aug 2022 10:34:30 +0200 Subject: [PATCH 2/3] fixed indentation issues --- marshmallow_dataclass/__init__.py | 145 +++++++++++++++--------------- 1 file changed, 73 insertions(+), 72 deletions(-) diff --git a/marshmallow_dataclass/__init__.py b/marshmallow_dataclass/__init__.py index cd5e362..ecb8c9a 100644 --- a/marshmallow_dataclass/__init__.py +++ b/marshmallow_dataclass/__init__.py @@ -66,6 +66,7 @@ class User: from marshmallow_dataclass.lazy_class_attribute import lazy_class_attribute + __all__ = ["dataclass", "add_schema", "class_schema", "field_for_schema", "NewType"] NoneType = type(None) @@ -83,35 +84,35 @@ class User: @overload def dataclass( - _cls: Type[_U], - *, - repr: bool = True, - eq: bool = True, - order: bool = False, - unsafe_hash: bool = False, - frozen: bool = False, - match_args=True, - kw_only=False, - slots=False, - base_schema: Optional[Type[marshmallow.Schema]] = None, - cls_frame: Optional[types.FrameType] = None, + _cls: Type[_U], + *, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, + match_args=True, + kw_only=False, + slots=False, + base_schema: Optional[Type[marshmallow.Schema]] = None, + cls_frame: Optional[types.FrameType] = None, ) -> Type[_U]: ... @overload def dataclass( - *, - repr: bool = True, - eq: bool = True, - order: bool = False, - unsafe_hash: bool = False, - frozen: bool = False, - match_args=True, - kw_only=False, - slots=False, - base_schema: Optional[Type[marshmallow.Schema]] = None, - cls_frame: Optional[types.FrameType] = None, + *, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, + match_args=True, + kw_only=False, + slots=False, + base_schema: Optional[Type[marshmallow.Schema]] = None, + cls_frame: Optional[types.FrameType] = None, ) -> Callable[[Type[_U]], Type[_U]]: ... @@ -120,18 +121,18 @@ def dataclass( # underscore. The presence of _cls is used to detect if this # decorator is being called with parameters or not. def dataclass( - _cls: Type[_U] = None, - *, - repr: bool = True, - eq: bool = True, - order: bool = False, - unsafe_hash: bool = False, - frozen: bool = False, - match_args=True, - kw_only=False, - slots=False, - base_schema: Optional[Type[marshmallow.Schema]] = None, - cls_frame: Optional[types.FrameType] = None, + _cls: Type[_U] = None, + *, + repr: bool = True, + eq: bool = True, + order: bool = False, + unsafe_hash: bool = False, + frozen: bool = False, + match_args=True, + kw_only=False, + slots=False, + base_schema: Optional[Type[marshmallow.Schema]] = None, + cls_frame: Optional[types.FrameType] = None, ) -> Union[Type[_U], Callable[[Type[_U]], Type[_U]]]: """ This decorator does the same as dataclasses.dataclass, but also applies :func:`add_schema`. @@ -181,16 +182,16 @@ def add_schema(_cls: Type[_U]) -> Type[_U]: @overload def add_schema( - base_schema: Type[marshmallow.Schema] = None, + base_schema: Type[marshmallow.Schema] = None, ) -> Callable[[Type[_U]], Type[_U]]: ... @overload def add_schema( - _cls: Type[_U], - base_schema: Type[marshmallow.Schema] = None, - cls_frame: types.FrameType = None, + _cls: Type[_U], + base_schema: Type[marshmallow.Schema] = None, + cls_frame: types.FrameType = None, ) -> Type[_U]: ... @@ -230,9 +231,9 @@ def decorator(clazz: Type[_U]) -> Type[_U]: def class_schema( - clazz: type, - base_schema: Optional[Type[marshmallow.Schema]] = None, - clazz_frame: types.FrameType = None, + clazz: type, + base_schema: Optional[Type[marshmallow.Schema]] = None, + clazz_frame: types.FrameType = None, ) -> Type[marshmallow.Schema]: """ Convert a class to a marshmallow schema @@ -369,9 +370,9 @@ def class_schema( @lru_cache(maxsize=MAX_CLASS_SCHEMA_CACHE_SIZE) def _internal_class_schema( - clazz: type, - base_schema: Optional[Type[marshmallow.Schema]] = None, - clazz_frame: types.FrameType = None, + clazz: type, + base_schema: Optional[Type[marshmallow.Schema]] = None, + clazz_frame: types.FrameType = None, ) -> Type[marshmallow.Schema]: _RECURSION_GUARD.seen_classes[clazz] = clazz.__name__ try: @@ -428,20 +429,20 @@ def _internal_class_schema( def _field_by_type( - typ: Union[type, Any], base_schema: Optional[Type[marshmallow.Schema]] + typ: Union[type, Any], base_schema: Optional[Type[marshmallow.Schema]] ) -> Optional[Type[marshmallow.fields.Field]]: return ( - base_schema and base_schema.TYPE_MAPPING.get(typ) - ) or marshmallow.Schema.TYPE_MAPPING.get(typ) + base_schema and base_schema.TYPE_MAPPING.get(typ) + ) or marshmallow.Schema.TYPE_MAPPING.get(typ) def _field_by_supertype( - typ: Type, - default: Any, - newtype_supertype: Type, - metadata: dict, - base_schema: Optional[Type[marshmallow.Schema]], - typ_frame: Optional[types.FrameType], + typ: Type, + default: Any, + newtype_supertype: Type, + metadata: dict, + base_schema: Optional[Type[marshmallow.Schema]], + typ_frame: Optional[types.FrameType], ) -> marshmallow.fields.Field: """ Return a new field for fields based on a super field. (Usually spawned from NewType) @@ -494,10 +495,10 @@ def _generic_type_add_any(typ: type) -> type: def _field_for_generic_type( - typ: type, - base_schema: Optional[Type[marshmallow.Schema]], - typ_frame: Optional[types.FrameType], - **metadata: Any, + typ: type, + base_schema: Optional[Type[marshmallow.Schema]], + typ_frame: Optional[types.FrameType], + **metadata: Any, ) -> Optional[marshmallow.fields.Field]: """ If the type is a generic interface, resolve the arguments and construct the appropriate Field. @@ -601,11 +602,11 @@ def _field_for_generic_type( def field_for_schema( - typ: type, - default=marshmallow.missing, - metadata: Mapping[str, Any] = None, - base_schema: Optional[Type[marshmallow.Schema]] = None, - typ_frame: Optional[types.FrameType] = None, + typ: type, + default=marshmallow.missing, + metadata: Mapping[str, Any] = None, + base_schema: Optional[Type[marshmallow.Schema]] = None, + typ_frame: Optional[types.FrameType] = None, ) -> marshmallow.fields.Field: """ Get a marshmallow Field corresponding to the given python type. @@ -732,17 +733,17 @@ def field_for_schema( forward_reference = getattr(typ, "__forward_arg__", None) nested = ( - nested_schema - or forward_reference - or _RECURSION_GUARD.seen_classes.get(typ) - or _internal_class_schema(typ, base_schema, typ_frame) + nested_schema + or forward_reference + or _RECURSION_GUARD.seen_classes.get(typ) + or _internal_class_schema(typ, base_schema, typ_frame) ) return marshmallow.fields.Nested(nested, **metadata) def _base_schema( - clazz: type, base_schema: Optional[Type[marshmallow.Schema]] = None + clazz: type, base_schema: Optional[Type[marshmallow.Schema]] = None ) -> Type[marshmallow.Schema]: """ Base schema factory that creates a schema for `clazz` derived either from `base_schema` @@ -780,10 +781,10 @@ def _get_field_default(field: dataclasses.Field): def NewType( - name: str, - typ: Type[_U], - field: Optional[Type[marshmallow.fields.Field]] = None, - **kwargs, + name: str, + typ: Type[_U], + field: Optional[Type[marshmallow.fields.Field]] = None, + **kwargs, ) -> Callable[[_U], _U]: """NewType creates simple unique types to which you can attach custom marshmallow attributes. From d12d08f15ab1d2dc3309c0d8831c18a0c7d63f9b Mon Sep 17 00:00:00 2001 From: Lorenzo Sciuto Date: Tue, 30 Aug 2022 10:35:42 +0200 Subject: [PATCH 3/3] setup rollback --- setup.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/setup.py b/setup.py index 9b3d911..8b80afa 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = "8.5.9" +VERSION = "8.5.8" CLASSIFIERS = [ "Development Status :: 4 - Beta", @@ -33,11 +33,11 @@ ], } EXTRAS_REQUIRE["dev"] = ( - EXTRAS_REQUIRE["enum"] - + EXTRAS_REQUIRE["union"] - + EXTRAS_REQUIRE["lint"] - + EXTRAS_REQUIRE["docs"] - + EXTRAS_REQUIRE["tests"] + EXTRAS_REQUIRE["enum"] + + EXTRAS_REQUIRE["union"] + + EXTRAS_REQUIRE["lint"] + + EXTRAS_REQUIRE["docs"] + + EXTRAS_REQUIRE["tests"] ) setup(