-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
gh-148680: Replace internal names with type_reprs of objects in string representations of ForwardRef #148682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-148680: Replace internal names with type_reprs of objects in string representations of ForwardRef #148682
Changes from all commits
12d7a45
e1e258e
ade5f8e
f3b6293
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1961,6 +1961,15 @@ def test_forward_repr(self): | |
| "typing.List[ForwardRef('int', owner='class')]", | ||
| ) | ||
|
|
||
| def test_forward_repr_extra_names(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make the tests rely less on direct assignment to
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I can do that here. This was partly done to avoid the The other example of this in I need this in Perhaps the |
||
| def f(a: undefined | str): ... | ||
|
|
||
| annos = get_annotations(f, format=Format.FORWARDREF) | ||
|
|
||
| self.assertRegex( | ||
| repr(annos['a']), r"ForwardRef\('undefined \| str'.*\)" | ||
| ) | ||
|
|
||
| def test_forward_recursion_actually(self): | ||
| def namespace1(): | ||
| a = ForwardRef("A") | ||
|
|
@@ -2037,6 +2046,20 @@ def test_evaluate_string_format(self): | |
| fr = ForwardRef("set[Any]") | ||
| self.assertEqual(fr.evaluate(format=Format.STRING), "set[Any]") | ||
|
|
||
| def test_evaluate_string_format_extra_names(self): | ||
| # Test that internal extra_names are replaced when evaluating as strings | ||
|
|
||
| # As identifier | ||
| fr = ForwardRef("__annotationlib_name_1__") | ||
| fr.__extra_names__ = {"__annotationlib_name_1__": str} | ||
| self.assertEqual(fr.evaluate(format=Format.STRING), "str") | ||
|
|
||
| # Via AST visitor | ||
| def f(a: ref | str): ... | ||
|
|
||
| fr = get_annotations(f, format=Format.FORWARDREF)['a'] | ||
| self.assertEqual(fr.evaluate(format=Format.STRING), "ref | str") | ||
|
|
||
| def test_evaluate_forwardref_format(self): | ||
| fr = ForwardRef("undef") | ||
| evaluated = fr.evaluate(format=Format.FORWARDREF) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might need to cache this, it's probably pretty slow. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is in fact, not very fast. Definitely worth caching if it's going to be accessed frequently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.