Skip to content

Commit 6d7380b

Browse files
tirkarthisloria
andauthored
Add stacklevel to print appropriate location in warnings. (#1986)
* Add stacklevel to print appropriate location in warnings. * Add to authors. * Update changelog Co-authored-by: Steven Loria <sloria1@gmail.com>
1 parent 062ed28 commit 6d7380b

4 files changed

Lines changed: 13 additions & 0 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,4 @@ Contributors (chronological)
168168
- Ben Windsor `@bwindsor <https://github.com/bwindsor>`_
169169
- Kevin Kirsche `@kkirsche <https://github.com/kkirsche>`_
170170
- Isira Seneviratne `@Isira-Seneviratne <https://github.com/Isira-Seneviratne>`_
171+
- Karthikeyan Singaravelan `@tirkarthi <https://github.com/tirkarthi>`_

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Other changes:
1313

1414
- Set lower bound for `packaging` requirement (:issue:`1957`).
1515
Thanks :user:`MatthewNicolTR` for reporting and thanks :user:`sirosen` for the PR.
16+
- Improve warning messages by passing `stacklevel` (:pr:`1986`).
17+
Thanks :user:`tirkarthi` for the PR.
1618

1719
3.15.0 (2022-03-12)
1820
*******************

src/marshmallow/fields.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ def __init__(
174174
"The 'default' argument to fields is deprecated. "
175175
"Use 'dump_default' instead.",
176176
RemovedInMarshmallow4Warning,
177+
stacklevel=2,
177178
)
178179
if dump_default is missing_:
179180
dump_default = default
@@ -182,6 +183,7 @@ def __init__(
182183
"The 'missing' argument to fields is deprecated. "
183184
"Use 'load_default' instead.",
184185
RemovedInMarshmallow4Warning,
186+
stacklevel=2,
185187
)
186188
if load_default is missing_:
187189
load_default = missing
@@ -220,6 +222,7 @@ def __init__(
220222
"explicit `metadata=...` argument instead. "
221223
f"Additional metadata: {additional_metadata}",
222224
RemovedInMarshmallow4Warning,
225+
stacklevel=2,
223226
)
224227

225228
self._creation_index = Field._creation_index
@@ -298,6 +301,7 @@ def fail(self, key: str, **kwargs):
298301
key
299302
),
300303
RemovedInMarshmallow4Warning,
304+
stacklevel=2,
301305
)
302306
raise self.make_error(key=key, **kwargs)
303307

@@ -439,6 +443,7 @@ def default(self):
439443
"The 'default' attribute of fields is deprecated. "
440444
"Use 'dump_default' instead.",
441445
RemovedInMarshmallow4Warning,
446+
stacklevel=2,
442447
)
443448
return self.dump_default
444449

@@ -448,6 +453,7 @@ def default(self, value):
448453
"The 'default' attribute of fields is deprecated. "
449454
"Use 'dump_default' instead.",
450455
RemovedInMarshmallow4Warning,
456+
stacklevel=2,
451457
)
452458
self.dump_default = value
453459

@@ -457,6 +463,7 @@ def missing(self):
457463
"The 'missing' attribute of fields is deprecated. "
458464
"Use 'load_default' instead.",
459465
RemovedInMarshmallow4Warning,
466+
stacklevel=2,
460467
)
461468
return self.load_default
462469

@@ -466,6 +473,7 @@ def missing(self, value):
466473
"The 'missing' attribute of fields is deprecated. "
467474
"Use 'load_default' instead.",
468475
RemovedInMarshmallow4Warning,
476+
stacklevel=2,
469477
)
470478
self.load_default = value
471479

@@ -550,6 +558,7 @@ def __init__(
550558
"Passing 'self' to `Nested` is deprecated. "
551559
"Use `Nested(lambda: MySchema(...))` instead.",
552560
RemovedInMarshmallow4Warning,
561+
stacklevel=2,
553562
)
554563
self.nested = nested
555564
self.only = only

src/marshmallow/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def pprint(obj, *args, **kwargs) -> None:
8484
warnings.warn(
8585
"marshmallow's pprint function is deprecated and will be removed in marshmallow 4.",
8686
RemovedInMarshmallow4Warning,
87+
stacklevel=2,
8788
)
8889
if isinstance(obj, collections.OrderedDict):
8990
print(json.dumps(obj, *args, **kwargs))

0 commit comments

Comments
 (0)