Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Lib/annotationlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,21 @@ def evaluate(
type_params = getattr(owner, "__type_params__", None)

# Type parameters exist in their own scope, which is logically
# between the locals and the globals. We simulate this by adding
# them to the globals.
# between the locals and the globals.
type_param_scope = {}
if type_params is not None:
globals = dict(globals)
for param in type_params:
globals[param.__name__] = param
type_param_scope[param.__name__] = param

if self.__extra_names__:
locals = {**locals, **self.__extra_names__}

arg = self.__forward_arg__
if arg.isidentifier() and not keyword.iskeyword(arg):
if arg in locals:
return locals[arg]
elif arg in type_param_scope:
return type_param_scope[arg]
elif arg in globals:
return globals[arg]
elif hasattr(builtins, arg):
Expand All @@ -183,12 +185,12 @@ def evaluate(
else:
code = self.__forward_code__
try:
return eval(code, globals=globals, locals=locals)
return eval(code, globals=globals, locals={**type_param_scope, **locals})
except Exception:
if not is_forwardref_format:
raise
new_locals = _StringifierDict(
{**builtins.__dict__, **locals},
{**type_param_scope, **builtins.__dict__, **locals},
globals=globals,
owner=owner,
is_class=self.__forward_is_class__,
Expand Down
49 changes: 33 additions & 16 deletions Lib/test/test_annotationlib.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
"""Tests for the annotations module."""

import textwrap
import annotationlib
import builtins
import collections
import functools
import itertools
import pickle
from string.templatelib import Template
import textwrap
import typing
import unittest
from annotationlib import (
Format,
ForwardRef,
get_annotations,
annotations_to_string,
type_repr,
)
from typing import Unpack, get_type_hints, List, Union

from string.templatelib import Template
from test import support
from test.support import import_helper
from test.test_inspect import inspect_stock_annotations
from test.test_inspect import inspect_stringized_annotations
from test.test_inspect import inspect_stringized_annotations_2
from test.test_inspect import inspect_stringized_annotations_pep695
from test.test_inspect import (inspect_stock_annotations,
inspect_stringized_annotations,
inspect_stringized_annotations_2,
inspect_stringized_annotations_pep695)
from typing import List, Union, Unpack, get_type_hints

import annotationlib
from annotationlib import (Format, ForwardRef, annotations_to_string,
get_annotations, type_repr)


def times_three(fn):
Expand Down Expand Up @@ -1683,6 +1678,28 @@ def test_fwdref_invalid_syntax(self):
with self.assertRaises(SyntaxError):
fr.evaluate()

def test_re_evaluate(self):
class C:
x: alias

evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
alias = int
self.assertIs(evaluated.evaluate(), int)

del alias
evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
with self.assertRaises(NameError):
evaluated.evaluate()

class C:
x: alias2

evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
global alias2
alias2 = str
self.assertIs(evaluated.evaluate(), str)



class TestAnnotationLib(unittest.TestCase):
def test__all__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :meth:`annotationlib.ForwardRef.evaluate` returning :class:`annotationlib.ForwardRef`
objects which do not update in new contexts.
Loading