8000 Using types from 'typing' when using singledispatch now raises a Type… · python/cpython@756c2b5 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 756c2b5

Browse files
committed
Using types from 'typing' when using singledispatch now raises a TypeError
1 parent 23b5ceb commit 756c2b5

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Lib/functools.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,11 @@ def register(cls, func=None):
848848
# only import typing if annotation parsing is necessary
849849
from typing import get_type_hints
850850
argname, cls = next(iter(get_type_hints(func).items()))
851-
assert isinstance(cls, type), (
852-
f"Invalid annotation for {argname!r}. {cls!r} is not a class."
853-
)
851+
if not isinstance(cls, type):
852+
raise TypeError(
853+
f"Invalid annotation for {argname!r}. "
854+
f"{cls!r} is not a class."
855+
)
854856
registry[cls] = func
855857
if cache_token is None and hasattr(cls, '__abstractmethods__'):
856858
cache_token = get_cache_token()

Lib/test/test_functools.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2304,10 +2304,12 @@ def _(arg: typing.Iterable[str]):
23042304
# types from `typing`. Instead, annotate with regular types
23052305
# or ABCs.
23062306
return "I annotated with a generic collection"
2307-
self.assertTrue(str(exc.exception).startswith(msg_prefix +
2308-
"<function TestSingleDispatch.test_invalid_registrations.<locals>._"
2307+
self.assertTrue(str(exc.exception).startswith(
2308+
"Invalid annotation for 'arg'."
2309+
))
2310+
self.assertTrue(str(exc.exception).endswith(
2311+
'typing.Iterable[str] is not a class.'
23092312
))
2310-
self.assertTrue(str(exc.exception).endswith(msg_suffix))
23112313

23122314
def test_invalid_positional_argument(self):
23132315
@functools.singledispatch

0 commit comments

Comments
 (0)
0