From 862a5170189f4029479f2769f848cb07824a0488 Mon Sep 17 00:00:00 2001 From: Daniil Fajnberg Date: Fri, 21 Apr 2023 15:52:33 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A8=20Set=20explicit=20`stacklevel`=20?= =?UTF-8?q?when=20warning=20about=20schema-wide=20`many`=20setting=20depen?= =?UTF-8?q?ding=20on=20initialization=20state.=20This=20should=20help=20us?= =?UTF-8?q?ers=20see=20the=20exact=20line=20they=20wrote=20that=20triggere?= =?UTF-8?q?d=20the=20warning.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/marshmallow_generic/schema.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/marshmallow_generic/schema.py b/src/marshmallow_generic/schema.py index 9fb9822..dacb2ef 100644 --- a/src/marshmallow_generic/schema.py +++ b/src/marshmallow_generic/schema.py @@ -16,6 +16,11 @@ from .decorators import post_load Model = TypeVar("Model") +MANY_SCHEMA_UNSAFE = ( + "Changing `many` schema-wide breaks type safety. " + "Use the the `many` parameter of specific methods (like `load`) instead." +) + class GenericSchema(GenericInsightMixin1[Model], Schema): """ @@ -100,6 +105,7 @@ class GenericSchema(GenericInsightMixin1[Model], Schema): [`load`][marshmallow_generic.GenericSchema.load]/ [`loads`][marshmallow_generic.GenericSchema.loads]. """ + self._pre_init = True super().__init__( only=only, exclude=exclude, @@ -110,6 +116,7 @@ class GenericSchema(GenericInsightMixin1[Model], Schema): partial=partial, unknown=unknown, ) + self._pre_init = False def __setattr__(self, name: str, value: Any) -> None: """ @@ -119,10 +126,7 @@ class GenericSchema(GenericInsightMixin1[Model], Schema): [`object.__setattr__`](https://docs.python.org/3/reference/datamodel.html#object.__setattr__). """ if name == "many" and value is not False: - warn( - "Changing `many` schema-wide breaks type safety. Use the the " - "`many` parameter of specific methods (like `load`) instead." - ) + warn(MANY_SCHEMA_UNSAFE, stacklevel=4 if self._pre_init else 2) super().__setattr__(name, value) @post_load