|
1 | 1 | """Tests for the annotations module."""
|
2 | 2 |
|
3 |
| -import textwrap |
4 |
| -import annotationlib |
5 | 3 | import builtins
|
6 | 4 | import collections
|
7 | 5 | import functools
|
8 | 6 | import itertools
|
9 | 7 | import pickle
|
10 |
| -from string.templatelib import Template |
| 8 | +import textwrap |
11 | 9 | import typing
|
12 | 10 | 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 |
22 | 12 | from test import support
|
23 | 13 | 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) |
28 | 23 |
|
29 | 24 |
|
30 | 25 | def times_three(fn):
|
@@ -1683,6 +1678,28 @@ def test_fwdref_invalid_syntax(self):
|
1683 | 1678 | with self.assertRaises(SyntaxError):
|
1684 | 1679 | fr.evaluate()
|
1685 | 1680 |
|
| 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 | + |
1686 | 1703 |
|
1687 | 1704 | class TestAnnotationLib(unittest.TestCase):
|
1688 | 1705 | def test__all__(self):
|
|
0 commit comments