FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-150641: Fix evaluating forward references in STRING format can 'leak' internal names in `typing` by IvyXu420 · Pull Request #150648 · python/cpython · GitHub

Repository navigation

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

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
12 changes: 12 additions & 0 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7561,6 +7561,18 @@ def test_with_module(self):
typing.evaluate_forward_ref(
fwdref_module.fw,)

def test_evaluate_forward_ref_string_format(self):
# Test evaluating forward references in STRING format
# does not 'leak' internal names
# See https://github.com/python/cpython/issues/150641

def f(arg: unknown | str | int | list[str] | tuple[int, ...]): ...

ref = annotationlib.get_annotations(f, format=annotationlib.Format.FORWARDREF)['arg']
self.assertEqual(
typing.evaluate_forward_ref(ref, format=annotationlib.Format.STRING),
"unknown | str | int | list[str] | tuple[int, ...]",
)

class CollectionsAbcTests(BaseTestCase):

Expand Down
2 changes: 1 addition & 1 deletion Lib/typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ def evaluate_forward_ref(

"""
if format == annotationlib.Format.STRING:
return forward_ref.__forward_arg__
return forward_ref.__resolved_str__
if forward_ref.__forward_arg__ in _recursive_guard:
return forward_ref

Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where :func:`typing.evaluate_forward_ref` with the ``STRING`` format
could leak internal names used by the annotation machinery.
Loading

Back | FazBrowse Home | New Git URL