Skip to content
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

Add backport of evaluate_forward_ref #497

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

Conversation

Daraan
Copy link
Contributor

@Daraan Daraan commented Oct 30, 2024

Backports evaluate_forward_ref from JelleZijlstra's PR python/cpython#119891 / PEP 749.

The most tricky part seems to be in recursive ForwardRefs, at least in my tests they worked better in 3.9 and but not in 3.8 and 3.10. If someone has some nice examples how to break it I gladly add them.
Limitation has been added to the docs.

Tests are also tested against cpython main python/cpython@079875e39

@AlexWaygood AlexWaygood changed the title Add backport of evalaute_forward_ref Add backport of evaluate_forward_ref Oct 30, 2024
Comment on lines 3826 to 3827
SOURCE = 3
STRING = 3
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess, SOURCE will be removed soon?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just remove SOURCE since we never released this enum.

src/test_typing_extensions.py Outdated Show resolved Hide resolved
src/test_typing_extensions.py Outdated Show resolved Hide resolved
@Daraan Daraan marked this pull request as ready for review October 30, 2024 18:51
Comment on lines 3826 to 3827
SOURCE = 3
STRING = 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just remove SOURCE since we never released this enum.

src/typing_extensions.py Show resolved Hide resolved
value, exec, msg, is_argument=True, *, module=None, allow_special_forms=False
):
"""
A forgiving version of typing._type_check to be executed if the original fails
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this? Could we always add a function defined here so we don't have to rely on typing._type_check?

assert Y.__name__ not in globals()

minimal_locals = {
#"Y": Y,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

src/test_typing_extensions.py Outdated Show resolved Hide resolved
@@ -8098,6 +8100,563 @@ def test_pep_695_generics_with_future_annotations_nested_in_function(self):
set(results.generic_func.__type_params__)
)

class TestEvaluateForwardRefs(BaseTestCase):
def test_evaluate_forward_refs(self):
from typing import ForwardRef # noqa: F401 # needed for locals
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this? Better to pass an explicit namespace instead of relying on the locals of this function.

localns,
type_params,
owner,
) in unroll_cases(cases, locals()):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure all this indirection makes the test cases better. It becomes harder to verify the tests behave as expected with so much logic between the definition of the tests and the actual tests.

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

Successfully merging this pull request may close these issues.

Add typing_extensions.evaluate_forward_ref
2 participants