Skip to content

Commit

Permalink
Forward-port typing-extensions change
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Nov 29, 2023
1 parent e60ad5c commit d528dc8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,21 @@ def d():
py_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(): ...


def setUpModule():
py_warnings.onceregistry.clear()
Expand Down
4 changes: 4 additions & 0 deletions Lib/warnings.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@ def __init__(
category: type[Warning] | None = DeprecationWarning,
stacklevel: int = 1,
) -> None:
if not isinstance(msg, str):
raise TypeError(
f"Expected an object of type str for 'msg', not {type(msg).__name__!r}"
)
self.msg = msg
self.category = category
self.stacklevel = stacklevel
Expand Down

0 comments on commit d528dc8

Please sign in to comment.