| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1448,7 +1448,10 @@ def getargvalues(frame): | |||
| 1448 | 1448 | ||
| 1449 | 1449 | def formatannotation(annotation, base_module=None): | |
| 1450 | 1450 | if getattr(annotation, '__module__', None) == 'typing': | |
| 1451 | - return repr(annotation).replace('typing.', '') | ||
| 1451 | + def repl(match): | ||
| 1452 | + text = match.group() | ||
| 1453 | + return text.removeprefix('typing.') | ||
| 1454 | + return re.sub(r'[\w\.]+', repl, repr(annotation)) | ||
| 1452 | 1455 | if isinstance(annotation, types.GenericAlias): | |
| 1453 | 1456 | return str(annotation) | |
| 1454 | 1457 | if isinstance(annotation, type): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1421,6 +1421,13 @@ def wrapper(a, b): | |||
| 1421 | 1421 | self.assertEqual(inspect.get_annotations(isa.MyClassWithLocalAnnotations, eval_str=True), {'x': int}) | |
| 1422 | 1422 | ||
| 1423 | 1423 | ||
| 1424 | + class TestFormatAnnotation(unittest.TestCase): | ||
| 1425 | + def test_typing_replacement(self): | ||
| 1426 | + from test.typinganndata.ann_module9 import ann, ann1 | ||
| 1427 | + self.assertEqual(inspect.formatannotation(ann), 'Union[List[str], int]') | ||
| 1428 | + self.assertEqual(inspect.formatannotation(ann1), 'Union[List[testModule.typing.A], int]') | ||
| 1429 | + | ||
| 1430 | + | ||
| 1424 | 1431 | class TestIsDataDescriptor(unittest.TestCase): | |
| 1425 | 1432 | ||
| 1426 | 1433 | def test_custom_descriptors(self): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,14 @@ | |||
| 1 | + # Test ``inspect.formatannotation`` | ||
| 2 | + # https://github.com/python/cpython/issues/96073 | ||
| 3 | + | ||
| 4 | + from typing import Union, List | ||
| 5 | + | ||
| 6 | + ann = Union[List[str], int] | ||
| 7 | + | ||
| 8 | + # mock typing._type_repr behaviour | ||
| 9 | + class A: ... | ||
| 10 | + | ||
| 11 | + A.__module__ = 'testModule.typing' | ||
| 12 | + A.__qualname__ = 'A' | ||
| 13 | + | ||
| 14 | + ann1 = Union[List[A], int] | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1 @@ | |||
| 1 | + In :mod:`inspect`, fix overeager replacement of "`typing.`" in formatting annotations. | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments