Skip to content

Commit

Permalink
Raise if a non-str is passed as the first parameter to @deprecated
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWaygood committed Oct 29, 2023
1 parent fda0c15 commit a4f71b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- All parameters on `NewType.__call__` are now positional-only. This means that
the signature of `typing_extensions.NewType.__call__` now exactly matches the
signature of `typing.NewType.__call__`. Patch by Alex Waygood.
- `typing.deprecated` now gives a better error message if you pass a non-`str`
argument to the `msg` parameter. Patch by Alex Waygood.

# Release 4.8.0 (September 17, 2023)

Expand Down
15 changes: 15 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,21 @@ def d():
warnings.simplefilter("error")
d()

def test_only_strings_allowed(self):
with self.assertRaisesRegex(
TypeError,
"Expected an object of type str for 'msg', not 'type'"
):
@deprecated
class Foo: ...

with self.assertRaisesRegex(
TypeError,
"Expected an object of type str for 'msg', not 'function'"
):
@deprecated
def foo(): ...


class AnyTests(BaseTestCase):
def test_can_subclass(self):
Expand Down
5 changes: 5 additions & 0 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2331,6 +2331,11 @@ def g(x: str) -> int: ...
See PEP 702 for details.
"""
if not isinstance(msg, str):
raise TypeError(
f"Expected an object of type str for 'msg', not {type(msg).__name__!r}"
)

def decorator(arg: _T, /) -> _T:
if category is None:
arg.__deprecated__ = msg
Expand Down

0 comments on commit a4f71b8

Please sign in to comment.