From 2a5e35b334ec434f49ef9d7d4fc48a5a95e3db60 Mon Sep 17 00:00:00 2001 From: Daniil Fajnberg Date: Mon, 13 Mar 2023 15:20:33 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=B7=EF=B8=8F=20Change=20the=20`many`?= =?UTF-8?q?=20type=20back=20to=20`bool`=20in=20the=20`=5F=5Finit=5F=5F`=20?= =?UTF-8?q?method?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/marshmallow_generic/schema.py | 16 +++++++--------- tests/test_schema.py | 4 ++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/marshmallow_generic/schema.py b/src/marshmallow_generic/schema.py index f077092..9572c27 100644 --- a/src/marshmallow_generic/schema.py +++ b/src/marshmallow_generic/schema.py @@ -54,7 +54,7 @@ class GenericSchema(GenericInsightMixin1[Model], Schema): dump_only: Union[Sequence[str], set[str]] = (), partial: Union[bool, Sequence[str], set[str]] = False, unknown: Optional[str] = None, - many: Optional[bool] = None, + many: bool = False, # usage discouraged ) -> 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`. many: !!! warning - Specifying this option schema-wide undermines the type - safety that this class aims to provide and passing any - value other than `None` will trigger a warning. Use the - method-specific `many` parameter, when calling + Changing this option schema-wide undermines the type + safety that this class aims to provide. Passing `True` + will therefore trigger a warning. You should instead use + the method-specific `many` parameter, when calling [`dump`][marshmallow_generic.GenericSchema.dump]/ [`dumps`][marshmallow_generic.GenericSchema.dumps] or [`load`][marshmallow_generic.GenericSchema.load]/ - [`loads`][marshmallow_generic.GenericSchema.loads] instead. + [`loads`][marshmallow_generic.GenericSchema.loads]. """ - if many is not None: + if many: warn( "Setting `many` schema-wide breaks type safety. Use the the " "`many` parameter of specific methods (like `load`) instead." ) - else: - many = bool(many) super().__init__( only=only, exclude=exclude, diff --git a/tests/test_schema.py b/tests/test_schema.py index aed910d..5dfa763 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -19,10 +19,10 @@ class GenericSchemaTestCase(TestCase): "dump_only": object(), "partial": object(), "unknown": object(), - "many": None, + "many": False, } 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() kwargs["many"] = True with self.assertWarns(UserWarning):