GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-46333: Honor `module` parameter in ForwardRef by aha79 · Pull Request #30536 · python/cpython

bpo-46333: Honor module parameter in ForwardRef #30536

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.

Sign up for GitHub

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

Merged
JelleZijlstra merged 13 commits into python:main from aha79:bpo_46333_eq_and_repr_for_forwardref
Feb 17, 2022

Uh oh!

There was an error while loading. Please reload this page.

Merged

bpo-46333: Honor module parameter in ForwardRef #30536

Changes from 1 commit
Commits
Show all changes
13 commits
Select commit Hold shift + click to select a range
980ea28
bpo-46333: Honor `module` parameter in ForwardRef
andreas-h-sie Jan 11, 2022
82fa061
📜🤖 Added by blurb_it.
blurb-it[bot] Jan 11, 2022
ac51d98
Update Lib/test/test_typing.py
aha79 Jan 11, 2022
d643861
Update Lib/typing.py
aha79 Jan 11, 2022
ac16c5c
Update Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst
aha79 Jan 11, 2022
a2e1377
Update ACKS
andreas-h-sie Jan 11, 2022
c084fdb
Update Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst
aha79 Jan 12, 2022
7577633
Remove module output from __repr__
andreas-h-sie Feb 10, 2022
d608058
Merge branch 'bpo_46333_eq_and_repr_for_forwardref' of github.com:aha…
andreas-h-sie Feb 10, 2022
3df5009
take out repr change
andreas-h-sie Feb 10, 2022
42fc26b
Update Misc/NEWS.d/next/Library/2022-01-11-15-54-15.bpo-46333.B1faiF.rst
aha79 Feb 11, 2022
9dd2f8f
add tests for ForwardRef.hash
andreas-h-sie Feb 11, 2022
94c9a69
move ForwardRef.__hash__ test to correct function
andreas-h-sie Feb 11, 2022
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
Update Lib/test/test_typing.py
Co-authored-by: Alex Waygood <[email protected]>
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@aha79 @AlexWaygood
aha79 and AlexWaygood authored Jan 11, 2022
commit ac51d98a75a4acf9e03d235c44025d163b9203ac

There are no files selected for viewing

2 changes: 1 addition & 1 deletion 2 Lib/test/test_typing.py
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -2614,7 +2614,7 @@ def test_forward_equality(self):
self.assertNotEqual(fr, typing.ForwardRef('int', module=__name__))
Comment thread

@Fidget-Spinner Fidget-Spinner Jan 12, 2022

Copy link
Copy Markdown
Member

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.

Hmm, I wonder if this test is run directly, will __name__ be `main``? Wouldn't that make the test pass incorrectly? (Throwing this question out there because I'm not too sure myself). Maybe we should be safe and give it a distinct name?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aha79 aha79 Jan 12, 2022

Copy link
Copy Markdown
Contributor Author

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.

The test is to ensure that ForwardRef with module set is different from ForwardRef with module not set (That was previsously not the case). The actual value of module (or __name__) is not relevant. Under the hood the comparison is None vs str

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@Fidget-Spinner Fidget-Spinner Jan 12, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

My bad, thanks for pointing that out. I mistakenly assumed it would grab a default value from the current scope to populate module in things like ClassVar['int'] due to the _type_check/_type_convert function (I'm now shocked that it doesn't and only does that for TypedDict).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@aha79 aha79 Jan 12, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

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.

Yes, unfortunately the current module is not captured automatically.
The current scope/module is typicallly obtained by get_type_hints because a class knows its module (and so all ForwardRefs in member annotations can be correctly resolved. But each forward Ref does originally not know its module).
However for type aliases this does not work, and there one needs the explicit module.

For example

Json = Union[ List['Json'], Dict[str,'Json'], int, float, bool, None ]

cannot be exported (and expect to work) unless one sets the module for each forward ref.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
frm = typing.ForwardRef('int', module=__name__)
self.assertEqual(frm, typing.ForwardRef('int', module=__name__))
self.assertNotEqual(frm, typing.ForwardRef('int', module='__other_name___'))
self.assertNotEqual(frm, typing.ForwardRef('int', module='__other_name__'))

def test_forward_equality_gth(self):
c1 = typing.ForwardRef('C')
Expand Down
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.