Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
gh-130870 Fix _eval_type Handling for GenericAlias with Unflattened Arguments and Union Types#130897
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
Uh oh!
There was an error while loading. Please reload this page.
Conversation
sharktide commented Mar 5, 2025 • edited by picnixz
Loading Uh oh!
There was an error while loading. Please reload this page.
edited by picnixz
Uh oh!
There was an error while loading. Please reload this page.
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Misc/NEWS.d/next/Library/2025-03-05-21-48-22.gh-issue-130870.uDz6AQ.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
sharktide commented Mar 6, 2025
I have made the requested changes; please review again |
Thanks for making the requested changes! @JelleZijlstra: please review the changes made to this pull request. |
sharktide commented Mar 15, 2025
HI! Sorry to disturb you, but could you please re-review this pull request? TiA |
sharktide commented Apr 12, 2025
@JelleZijlstra or @AlexWaygood Could you please check this PR again? It has been over a month I have been waiting. |
JelleZijlstra commented Apr 12, 2025
The test cases don't use any public APIs so as far as I can see this PR doesn't change any behaviors that we care about. |
sharktide commented Apr 12, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
@JelleZijlstra It took me so long to write that :) |
sharktide commented May 5, 2025
@JelleZijlstra Please review again. I added new tests to demonstrate the behavior with a public api (get type hints). They passed. Note: The fail of the Thread Sanatizer workflow is likely not due to this PR (asynicio test that failed) |
sharktide commented May 8, 2025
@JelleZijlstra please review. I added the tests and everything works the test fail is unrelated |
sharktide commented May 10, 2025
@JelleZijlstra I think you forgot to review again. Please do get to it soon. |
JelleZijlstra left a comment
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 reason I've been slow to review this is that I feel I'll basically have to rewrite everything; it's still not clear to me what publicly visible behavior this is supposed to fix.
Start with writing a test case that fails today and that uses only public APIs (get_type_hints or get_args maybe, not _eval_type).
Lib/test/test_typing.py Outdated
| classMyType: | ||
| pass | ||
| classTestGenericAliasHandling(BaseTestCase): |
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.
All of these tests pass on master today without your changes in typing.py.
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.
Not the one I sent in the comments!
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 shouldn't be adding tests that are unrelated to the change.
Uh oh!
There was an error while loading. Please reload this page.
Misc/NEWS.d/next/Library/2025-03-05-21-48-22.gh-issue-130870.uDz6AQ.rst Outdated Show resolvedHide resolved
Uh oh!
There was an error while loading. Please reload this page.
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
sharktide commented May 12, 2025 • edited
Loading Uh oh!
There was an error while loading. Please reload this page.
edited
Uh oh!
There was an error while loading. Please reload this page.
@JelleZijlstra You want a failing test? I'll give you a failing test: classTestCallableAlias(BaseTestCase): deftest_callable_alias_preserves_subclass(self): C=ABCallable[[str, ForwardRef('int')], int] classA: c: C# Explicitly pass global namespace to ensure correct resolutionhints=get_type_hints(A, globalns=globals()) # Ensure evaluated type retains the correct subclass (_CallableGenericAlias)self.assertEqual(hints['c'].__class__, C.__class__) # Ensure evaluated type retains correct originself.assertEqual(hints['c'].__origin__, C.__origin__) # Instead of comparing raw ForwardRef, check if the resolution is correctexpected_args=tuple(intifisinstance(arg, ForwardRef) elseargforarginC.__args__) self.assertEqual(hints['c'].__args__, expected_args)Out without the fix: (Note that this was when I had f string assertionError messages. I removed them) FAIL: test_callable_alias_preserves_subclass (__main__.TestCallableAlias.test_callable_alias_preserves_subclass) ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\Users\rihaan.meher\Documents\GitHub\cpython\zzz.py", line 30, in test_callable_alias_preserves_subclass self.assertEqual(hints['c'].__class__, C.__class__, f"Expected{C.__class__}, got{hints['c'].__class__}") AssertionError: <class 'types.GenericAlias'>!= <class 'collections.abc._CallableGenericAlias'>: Expected <class 'collections.abc._CallableGenericAlias'>, got <class 'types.GenericAlias'>With my PR, this passes. I added it to test_typing.py Essentially what this PR does:
|
sharktide commented May 12, 2025
Test fails are unrelated: 504 gateway timeout in http/urllib tests |
sharktide commented May 12, 2025
Look at the comment I just posted |
sharktide commented Jun 7, 2025
No reviewer action in at least 1mo —> @JelleZijlstra please review |
ZeroIntensity left a comment
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.
I agree with Jelle: I don't think this PR is solving an actual problem. This will end up exposing implemenation details, like _CallableGenericAlias. In practice, why do users care about something like _CallableGenericAlias over the (public) GenericAlias?
Lib/test/test_typing.py Outdated
| classMyType: | ||
| pass | ||
| classTestGenericAliasHandling(BaseTestCase): |
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 shouldn't be adding tests that are unrelated to the change.
sharktide commented Jun 9, 2025
Explained in issue |
sharktide commented Jun 9, 2025
I have made the requested changes; please review again |
Thanks for making the requested changes! @JelleZijlstra: please review the changes made to this pull request. |
JelleZijlstra commented Jul 6, 2025
Superseded by #131583. |
Fixes the handling of GenericAlias types in _eval_type by correctly unflattening callable arguments when necessary. Also improves the resolution of Union types by evaluating their arguments properly.
Tests (View comments below):
#130897 (comment)
typing._eval_typeis not preservingGenericAliassubclasses #130870