Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
Loading
from

Conversation

sharktide
Copy link
Contributor

@sharktide sharktide commented Mar 5, 2025

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)

@bedevere-app
Copy link

bedevere-app bot commented Mar 5, 2025

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 skip news label instead.

Lib/typing.py Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented Mar 6, 2025

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@sharktide
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-app
Copy link

bedevere-app bot commented Mar 6, 2025

Thanks for making the requested changes!

@JelleZijlstra: please review the changes made to this pull request.

@sharktide
Copy link
Contributor Author

@JelleZijlstra

HI! Sorry to disturb you, but could you please re-review this pull request? TiA

@sharktide
Copy link
Contributor Author

@JelleZijlstra or @AlexWaygood Could you please check this PR again? It has been over a month I have been waiting.

@JelleZijlstra
Copy link
Member

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
Copy link
Contributor Author

sharktide 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.

@JelleZijlstra
While the changes might seem to address internal behaviors, They also contribute to more robust handling of GenericAlias and Union types within the _eval_type function (imo), ensuring that edge cases like callable argument unpacking and recursive type evaluation are properly managed. This improvement could indirectly enhance the reliability of features relying on these internal implementations.

It took me so long to write that :)

@JelleZijlstra JelleZijlstra removed their request for review May 4, 2025 22:54
@sharktide
Copy link
Contributor Author

All of these tests still test only _eval_type, which is a private function and doesn't have any guaranteed behavior. Can you demonstrate that this fixes any operation that involves a public API?

@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 sharktide requested a review from JelleZijlstra May 5, 2025 13:07
@sharktide
Copy link
Contributor Author

@JelleZijlstra please review. I added the tests and everything works

the test fail is unrelated

@sharktide
Copy link
Contributor Author

@JelleZijlstra I think you forgot to review again. Please do get to it soon.

Copy link
Member

@JelleZijlstra JelleZijlstra left a 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):
Copy link
Member

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.

Copy link
Contributor Author

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!

Lib/test/test_typing.py Show resolved Hide resolved
@bedevere-app
Copy link

bedevere-app bot commented May 10, 2025

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@sharktide
Copy link
Contributor Author

sharktide commented May 12, 2025

@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:

  • Preserving Type Information: If Callable is incorrectly converted to GenericAlias, it loses its subclass-specific behavior, which could affect type introspection and runtime checks.

  • Ensuring Correct Type Resolution: ForwardRef('int') should be properly resolved to int, maintaining expected behavior when evaluating type hints.

  • Preventing Silent Type Corruption: If Callable is mishandled, it could lead to unexpected behavior in libraries and applications relying on precise type annotations.

@sharktide sharktide requested a review from JelleZijlstra May 12, 2025 20:41
@sharktide
Copy link
Contributor Author

Test fails are unrelated: 504 gateway timeout in http/urllib tests

@sharktide
Copy link
Contributor Author

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).

Look at the comment I just posted

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.