Skip to content

Commit 9de9fd6

Browse files
authored
Raise if a non-str is passed as the first parameter to @deprecated (#296)
1 parent fda0c15 commit 9de9fd6

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
- All parameters on `NewType.__call__` are now positional-only. This means that
44
the signature of `typing_extensions.NewType.__call__` now exactly matches the
55
signature of `typing.NewType.__call__`. Patch by Alex Waygood.
6+
- `typing.deprecated` now gives a better error message if you pass a non-`str`
7+
argument to the `msg` parameter. Patch by Alex Waygood.
68

79
# Release 4.8.0 (September 17, 2023)
810

src/test_typing_extensions.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,21 @@ def d():
480480
warnings.simplefilter("error")
481481
d()
482482

483+
def test_only_strings_allowed(self):
484+
with self.assertRaisesRegex(
485+
TypeError,
486+
"Expected an object of type str for 'msg', not 'type'"
487+
):
488+
@deprecated
489+
class Foo: ...
490+
491+
with self.assertRaisesRegex(
492+
TypeError,
493+
"Expected an object of type str for 'msg', not 'function'"
494+
):
495+
@deprecated
496+
def foo(): ...
497+
483498

484499
class AnyTests(BaseTestCase):
485500
def test_can_subclass(self):

src/typing_extensions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2331,6 +2331,11 @@ def g(x: str) -> int: ...
23312331
See PEP 702 for details.
23322332
23332333
"""
2334+
if not isinstance(msg, str):
2335+
raise TypeError(
2336+
f"Expected an object of type str for 'msg', not {type(msg).__name__!r}"
2337+
)
2338+
23342339
def decorator(arg: _T, /) -> _T:
23352340
if category is None:
23362341
arg.__deprecated__ = msg

0 commit comments

Comments
 (0)