Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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