summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_inspect.py
diff options
context:
space:
mode:
authorBatuhan Taskaya <batuhanosmantaskaya@gmail.com>2020-10-06 20:03:02 (GMT)
committerGitHub <noreply@github.com>2020-10-06 20:03:02 (GMT)
commit044a1048ca93d466965afc027b91a5a9eb9ce23c (patch)
tree94ef2bca072693d83448edef4009dc840c58a3e2 /Lib/test/test_inspect.py
parentbef7d299eb911086ea5a7ccf7a9da337e38a8491 (diff)
downloadcpython-044a1048ca93d466965afc027b91a5a9eb9ce23c.zip
cpython-044a1048ca93d466965afc027b91a5a9eb9ce23c.tar.gz
cpython-044a1048ca93d466965afc027b91a5a9eb9ce23c.tar.bz2
bpo-38605: Make 'from __future__ import annotations' the default (GH-20434)
The hard part was making all the tests pass; there are some subtle issues here, because apparently the future import wasn't tested very thoroughly in previous Python versions. For example, `inspect.signature()` returned type objects normally (except for forward references), but strings with the future import. We changed it to try and return type objects by calling `typing.get_type_hints()`, but fall back on returning strings if that function fails (which it may do if there are future references in the annotations that require passing in a specific namespace to resolve).
Diffstat (limited to 'Lib/test/test_inspect.py')
-rw-r--r--Lib/test/test_inspect.py44
1 files changed, 20 insertions, 24 deletions
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 6667dc9..71c4f27 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -862,7 +862,7 @@ class TestClassesAndFunctions(unittest.TestCase):
self.assertFullArgSpecEquals(mod2.annotated, ['arg1'],
ann_e={'arg1' : list},
- formatted='(arg1: list)')
+ formatted="(arg1: list)")
self.assertFullArgSpecEquals(mod2.keyword_only_arg, [],
kwonlyargs_e=['arg'],
formatted='(*, arg)')
@@ -2211,8 +2211,8 @@ class TestSignatureObject(unittest.TestCase):
pass
self.assertEqual(self.signature(test),
((('a', ..., ..., "positional_or_keyword"),
- ('b', ..., 'foo', "positional_or_keyword")),
- 123))
+ ('b', ..., repr('foo'), "positional_or_keyword")),
+ '123'))
def test_signature_on_wkwonly(self):
def test(*, a:float, b:str) -> int:
@@ -2227,11 +2227,11 @@ class TestSignatureObject(unittest.TestCase):
pass
self.assertEqual(self.signature(test),
((('a', ..., ..., "positional_or_keyword"),
- ('b', 10, 'foo', "positional_or_keyword"),
- ('args', ..., 'bar', "var_positional"),
- ('spam', ..., 'baz', "keyword_only"),
+ ('b', 10, repr('foo'), "positional_or_keyword"),
+ ('args', ..., repr('bar'), "var_positional"),
+ ('spam', ..., repr('baz'), "keyword_only"),
('ham', 123, ..., "keyword_only"),
- ('kwargs', ..., int, "var_keyword")),
+ ('kwargs', ..., 'int', "var_keyword")),
...))
def test_signature_without_self(self):
@@ -2640,12 +2640,12 @@ class TestSignatureObject(unittest.TestCase):
self.assertEqual(self.signature(partial(partial(test, 1))),
((('b', ..., ..., "positional_or_keyword"),
- ('c', ..., int, "positional_or_keyword")),
- 42))
+ ('c', ..., 'int', "positional_or_keyword")),
+ '42'))
self.assertEqual(self.signature(partial(partial(test, 1), 2)),
- ((('c', ..., int, "positional_or_keyword"),),
- 42))
+ ((('c', ..., 'int', "positional_or_keyword"),),
+ '42'))
psig = inspect.signature(partial(partial(test, 1), 2))
@@ -2764,12 +2764,12 @@ class TestSignatureObject(unittest.TestCase):
((('it', ..., ..., 'positional_or_keyword'),
('a', ..., ..., 'positional_or_keyword'),
('c', 1, ..., 'keyword_only')),
- 'spam'))
+ repr('spam')))
self.assertEqual(self.signature(Spam().ham),
((('a', ..., ..., 'positional_or_keyword'),
('c', 1, ..., 'keyword_only')),
- 'spam'))
+ repr('spam')))
class Spam:
def test(self: 'anno', x):
@@ -2778,7 +2778,7 @@ class TestSignatureObject(unittest.TestCase):
g = partialmethod(test, 1)
self.assertEqual(self.signature(Spam.g),
- ((('self', ..., 'anno', 'positional_or_keyword'),),
+ ((('self', ..., repr('anno'), 'positional_or_keyword'),),
...))
def test_signature_on_fake_partialmethod(self):
@@ -3116,20 +3116,16 @@ class TestSignatureObject(unittest.TestCase):
with self.assertRaisesRegex(TypeError, 'unhashable type'):
hash(inspect.signature(foo))
- def foo(a) -> {}: pass
- with self.assertRaisesRegex(TypeError, 'unhashable type'):
- hash(inspect.signature(foo))
-
def test_signature_str(self):
def foo(a:int=1, *, b, c=None, **kwargs) -> 42:
pass
self.assertEqual(str(inspect.signature(foo)),
- '(a: int = 1, *, b, c=None, **kwargs) -> 42')
+ '(a: \'int\' = 1, *, b, c=None, **kwargs) -> \'42\'')
def foo(a:int=1, *args, b, c=None, **kwargs) -> 42:
pass
self.assertEqual(str(inspect.signature(foo)),
- '(a: int = 1, *args, b, c=None, **kwargs) -> 42')
+ '(a: \'int\' = 1, *args, b, c=None, **kwargs) -> \'42\'')
def foo():
pass
@@ -3172,8 +3168,8 @@ class TestSignatureObject(unittest.TestCase):
self.assertIs(sig.return_annotation, None)
sig = sig.replace(return_annotation=sig.empty)
self.assertIs(sig.return_annotation, sig.empty)
- sig = sig.replace(return_annotation=42)
- self.assertEqual(sig.return_annotation, 42)
+ sig = sig.replace(return_annotation='42')
+ self.assertEqual(sig.return_annotation, '42')
self.assertEqual(sig, inspect.signature(test))
def test_signature_on_mangled_parameters(self):
@@ -3185,8 +3181,8 @@ class TestSignatureObject(unittest.TestCase):
self.assertEqual(self.signature(Spam.foo),
((('self', ..., ..., "positional_or_keyword"),
- ('_Spam__p1', 2, 1, "positional_or_keyword"),
- ('_Spam__p2', 3, 2, "keyword_only")),
+ ('_Spam__p1', 2, '1', "positional_or_keyword"),
+ ('_Spam__p2', 3, '2', "keyword_only")),
...))
self.assertEqual(self.signature(Spam.foo),