Skip to content

Commit 17bd38f

Browse files
committed
Add test for re-evaluation of ForwardRef objects
1 parent b11afd3 commit 17bd38f

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

Lib/test/test_annotationlib.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
"""Tests for the annotations module."""
22

3-
import textwrap
4-
import annotationlib
53
import builtins
64
import collections
75
import functools
86
import itertools
97
import pickle
10-
from string.templatelib import Template
8+
import textwrap
119
import typing
1210
import unittest
13-
from annotationlib import (
14-
Format,
15-
ForwardRef,
16-
get_annotations,
17-
annotations_to_string,
18-
type_repr,
19-
)
20-
from typing import Unpack, get_type_hints, List, Union
21-
11+
from string.templatelib import Template
2212
from test import support
2313
from test.support import import_helper
24-
from test.test_inspect import inspect_stock_annotations
25-
from test.test_inspect import inspect_stringized_annotations
26-
from test.test_inspect import inspect_stringized_annotations_2
27-
from test.test_inspect import inspect_stringized_annotations_pep695
14+
from test.test_inspect import (inspect_stock_annotations,
15+
inspect_stringized_annotations,
16+
inspect_stringized_annotations_2,
17+
inspect_stringized_annotations_pep695)
18+
from typing import List, Union, Unpack, get_type_hints
19+
20+
import annotationlib
21+
from annotationlib import (Format, ForwardRef, annotations_to_string,
22+
get_annotations, type_repr)
2823

2924

3025
def times_three(fn):
@@ -1683,6 +1678,28 @@ def test_fwdref_invalid_syntax(self):
16831678
with self.assertRaises(SyntaxError):
16841679
fr.evaluate()
16851680

1681+
def test_re_evaluate(self):
1682+
class C:
1683+
x: alias
1684+
1685+
evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
1686+
alias = int
1687+
self.assertIs(evaluated.evaluate(), int)
1688+
1689+
del alias
1690+
evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
1691+
with self.assertRaises(NameError):
1692+
evaluated.evaluate()
1693+
1694+
class C:
1695+
x: alias2
1696+
1697+
evaluated = get_annotations(C, format=Format.FORWARDREF)["x"].evaluate(format=Format.FORWARDREF)
1698+
global alias2
1699+
alias2 = str
1700+
self.assertIs(evaluated.evaluate(), str)
1701+
1702+
16861703

16871704
class TestAnnotationLib(unittest.TestCase):
16881705
def test__all__(self):

0 commit comments

Comments
 (0)