From 10332c59831c763d1ff7c7f92db9bb8ca0c5a386 Mon Sep 17 00:00:00 2001 From: Steven Loria Date: Sat, 11 Jan 2025 18:34:35 -0500 Subject: [PATCH] Fix a few type annotations in fields and class_registry (#2756) * Fix a few type annotations in fields and class_registry * Update changelog --- CHANGELOG.rst | 7 +++++++ src/marshmallow/class_registry.py | 6 ++---- src/marshmallow/fields.py | 21 +++++++++++++++------ 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e07b6c82a..4b4896c9d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,13 @@ Changelog 3.25.1 (unreleased) ******************* +Bug fixes: + +- Typing: Fix type annotations for `Tuple `, + `Boolean `, and `Pluck ` + constructors (:pr:`2756`). +- Typing: Fix overload for `marshmallow.class_registry.get_class` (:pr:`2756`). + Documentation: - Various documentation improvements (:pr:`2746`, :pr:`2747`, :pr:`2748`, :pr:`2749`, :pr:`2750`, :pr:`2751`). diff --git a/src/marshmallow/class_registry.py b/src/marshmallow/class_registry.py index a8e2d6cda..ddc1ae290 100644 --- a/src/marshmallow/class_registry.py +++ b/src/marshmallow/class_registry.py @@ -74,15 +74,13 @@ def get_class(classname: str, all: typing.Literal[False] = ...) -> SchemaType: . @typing.overload -def get_class( - classname: str, all: typing.Literal[True] = ... -) -> list[SchemaType] | SchemaType: ... +def get_class(classname: str, all: typing.Literal[True] = ...) -> list[SchemaType]: ... def get_class(classname: str, all: bool = False) -> list[SchemaType] | SchemaType: """Retrieve a class from the registry. - :raises: marshmallow.exceptions.RegistryError if the class cannot be found + :raises: `marshmallow.exceptions.RegistryError` if the class cannot be found or if there are multiple entries for the given class name. """ try: diff --git a/src/marshmallow/fields.py b/src/marshmallow/fields.py index adf65dbc5..8544eb776 100644 --- a/src/marshmallow/fields.py +++ b/src/marshmallow/fields.py @@ -718,9 +718,14 @@ def __init__( self, nested: Schema | SchemaMeta | str | typing.Callable[[], Schema], field_name: str, + *, + many: bool = False, + unknown: str | None = None, **kwargs, ): - super().__init__(nested, only=(field_name,), **kwargs) + super().__init__( + nested, only=(field_name,), many=many, unknown=unknown, **kwargs + ) self.field_name = field_name @property @@ -830,8 +835,12 @@ class Tuple(Field): #: Default error messages. default_error_messages = {"invalid": "Not a valid tuple."} - def __init__(self, tuple_fields: typing.Iterable[Field], *args, **kwargs): - super().__init__(*args, **kwargs) + def __init__( + self, + tuple_fields: typing.Iterable[Field] | typing.Iterable[type[Field]], + **kwargs, + ): + super().__init__(**kwargs) if not utils.is_collection(tuple_fields): raise ValueError( "tuple_fields must be an iterable of Field classes or " "instances." @@ -1198,8 +1207,8 @@ class Boolean(Field): def __init__( self, *, - truthy: set | None = None, - falsy: set | None = None, + truthy: typing.Iterable | None = None, + falsy: typing.Iterable | None = None, **kwargs, ): super().__init__(**kwargs) @@ -1557,7 +1566,7 @@ def _deserialize(self, value, attr, data, **kwargs): class Mapping(Field): - """An abstract class for objects with key-value pairs. + """An abstract class for objects with key-value pairs. This class should not be used within schemas. :param keys: A field class or instance for dict keys. :param values: A field class or instance for dict values.