gh-119180: Only fetch globals and locals if necessary in `annotationlib.get_annotations()` (GH-135644)
(cherry picked from commit 504ae606e1)
Co-authored-by: Victorien <65306057+Viicos@users.noreply.github.com>
gh-133684: Fix get_annotations() where PEP 563 is involved (GH-133795)
(cherry picked from commit 3e562b3942)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
gh-133551: Support t-strings in annotationlib (GH-133553)
I don't know why you'd use t-strings in annotations, but now if you do,
the STRING format will do a great job of recovering the source code.
(cherry picked from commit 90f476e0f8)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
As noted on the issue, making get_annotate_function() support both types and
mappings is problematic because one object may be both. So let's add a new one
that works with any mapping.
This leaves get_annotate_function() not very useful, so remove it.
I started with just moving ForwardRefTests to test_annotationlib,
but found that it contained a number of tests for no_type_check, which
I moved to a new class in test_typing, as well as a number of tests that
are more appropriately classified as tests for get_type_hints().
One test, test_forward_equality_namespace(), was somewhat accidentally
depending on a global class A in test_typing. I added a class A in the
annotationlib tests instead.
Also add a useful comment in annotationlib.
Often, ForwardRefs represent a single simple name. In that case, we
can avoid going through the overhead of creating AST nodes and code
objects and calling eval(): we can simply look up the name directly
in the relevant namespaces.
Co-authored-by: Victor Stinner <vstinner@python.org>
We were sometimes passing None as the globals argument to eval(), which makes it
inherit the globals from the calling scope. Instead, ensure that globals is always
non-None. The test was passing accidentally because I passed "annotationlib" as a
module object; fix that. Also document the parameters to ForwardRef() and remove
two unused private ones.
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>