Skip to content

Commit

Permalink
Fix a few type annotations in fields and class_registry (#2756)
Browse files Browse the repository at this point in the history
* Fix a few type annotations in fields and class_registry

* Update changelog
  • Loading branch information
sloria authored Jan 11, 2025
1 parent f907ced commit 10332c5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Changelog
3.25.1 (unreleased)
*******************

Bug fixes:

- Typing: Fix type annotations for `Tuple <marshmallow.fields.Tuple>`,
`Boolean <marshmallow.fields.Boolean>`, and `Pluck <marshmallow.fields.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`).
Expand Down
6 changes: 2 additions & 4 deletions src/marshmallow/class_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 15 additions & 6 deletions src/marshmallow/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 10332c5

Please sign in to comment.