-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
base: main
Are you sure you want to change the base?
Conversation
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 resolved
Hide resolved
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 |
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. |
HI! Sorry to disturb you, but could you please re-review this pull request? TiA |
@JelleZijlstra or @AlexWaygood Could you please check this PR again? It has been over a month I have been waiting. |
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. |
@JelleZijlstra It took me so long to write that :) |
@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) |
@JelleZijlstra please review. I added the tests and everything works the test fail is unrelated |
@JelleZijlstra I think you forgot to review again. Please do get to it soon. |
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
).
class MyType: | ||
pass | ||
|
||
class TestGenericAliasHandling(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!
Misc/NEWS.d/next/Library/2025-03-05-21-48-22.gh-issue-130870.uDz6AQ.rst
Outdated
Show resolved
Hide resolved
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 |
@JelleZijlstra You want a failing test? I'll give you a failing test: class TestCallableAlias(BaseTestCase):
def test_callable_alias_preserves_subclass(self):
C = ABCallable[[str, ForwardRef('int')], int]
class A:
c: C
# Explicitly pass global namespace to ensure correct resolution
hints = 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 origin
self.assertEqual(hints['c'].__origin__, C.__origin__)
# Instead of comparing raw ForwardRef, check if the resolution is correct
expected_args = tuple(int if isinstance(arg, ForwardRef) else arg for arg in C.__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:
|
Test fails are unrelated: 504 gateway timeout in http/urllib tests |
Look at the comment I just posted |
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_type
is not preservingGenericAlias
subclasses #130870