🏷️ Change the many type back to bool in the __init__ method

This commit is contained in:
Daniil Fajnberg 2023-03-13 15:20:33 +01:00
parent fe5decad4f
commit 2a5e35b334
Signed by: daniil-berg
GPG Key ID: BE187C50903BEE97
2 changed files with 9 additions and 11 deletions

View File

@ -54,7 +54,7 @@ class GenericSchema(GenericInsightMixin1[Model], Schema):
dump_only: Union[Sequence[str], set[str]] = (), dump_only: Union[Sequence[str], set[str]] = (),
partial: Union[bool, Sequence[str], set[str]] = False, partial: Union[bool, Sequence[str], set[str]] = False,
unknown: Optional[str] = None, unknown: Optional[str] = None,
many: Optional[bool] = None, many: bool = False, # usage discouraged
) -> None: ) -> None:
""" """
Emits a warning, if the `many` argument is not `None`. Emits a warning, if the `many` argument is not `None`.
@ -90,22 +90,20 @@ class GenericSchema(GenericInsightMixin1[Model], Schema):
fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`. fields in the data. Use `EXCLUDE`, `INCLUDE` or `RAISE`.
many: many:
!!! warning !!! warning
Specifying this option schema-wide undermines the type Changing this option schema-wide undermines the type
safety that this class aims to provide and passing any safety that this class aims to provide. Passing `True`
value other than `None` will trigger a warning. Use the will therefore trigger a warning. You should instead use
method-specific `many` parameter, when calling the method-specific `many` parameter, when calling
[`dump`][marshmallow_generic.GenericSchema.dump]/ [`dump`][marshmallow_generic.GenericSchema.dump]/
[`dumps`][marshmallow_generic.GenericSchema.dumps] or [`dumps`][marshmallow_generic.GenericSchema.dumps] or
[`load`][marshmallow_generic.GenericSchema.load]/ [`load`][marshmallow_generic.GenericSchema.load]/
[`loads`][marshmallow_generic.GenericSchema.loads] instead. [`loads`][marshmallow_generic.GenericSchema.loads].
""" """
if many is not None: if many:
warn( warn(
"Setting `many` schema-wide breaks type safety. Use the the " "Setting `many` schema-wide breaks type safety. Use the the "
"`many` parameter of specific methods (like `load`) instead." "`many` parameter of specific methods (like `load`) instead."
) )
else:
many = bool(many)
super().__init__( super().__init__(
only=only, only=only,
exclude=exclude, exclude=exclude,

View File

@ -19,10 +19,10 @@ class GenericSchemaTestCase(TestCase):
"dump_only": object(), "dump_only": object(),
"partial": object(), "partial": object(),
"unknown": object(), "unknown": object(),
"many": None, "many": False,
} }
schema.GenericSchema[Foo](**kwargs) schema.GenericSchema[Foo](**kwargs)
mock_super_init.assert_called_once_with(**kwargs | {"many": False}) mock_super_init.assert_called_once_with(**kwargs)
mock_super_init.reset_mock() mock_super_init.reset_mock()
kwargs["many"] = True kwargs["many"] = True
with self.assertWarns(UserWarning): with self.assertWarns(UserWarning):