Skip to content

Commit f217d48

Browse files
committed
Refactor inspect tests(+doctests) with string annotations
1 parent 14e7c2d commit f217d48

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

Doc/library/inspect.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ Classes and functions
10051005
... pass
10061006
...
10071007
>>> formatargspec(*getfullargspec(f))
1008-
'(a: int, b: float)'
1008+
"(a: 'int', b: 'float')"
10091009

10101010
.. deprecated:: 3.5
10111011
Use :func:`signature` and

Lib/test/test_inspect.py

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -861,8 +861,8 @@ def test_getfullargspec(self):
861861
formatted='(*arg1, arg2=1)')
862862

863863
self.assertFullArgSpecEquals(mod2.annotated, ['arg1'],
864-
ann_e={'arg1' : list},
865-
formatted='(arg1: list)')
864+
ann_e={'arg1' : 'list'},
865+
formatted="(arg1: 'list')")
866866
self.assertFullArgSpecEquals(mod2.keyword_only_arg, [],
867867
kwonlyargs_e=['arg'],
868868
formatted='(*, arg)')
@@ -2211,27 +2211,27 @@ def test(a, b:'foo') -> 123:
22112211
pass
22122212
self.assertEqual(self.signature(test),
22132213
((('a', ..., ..., "positional_or_keyword"),
2214-
('b', ..., 'foo', "positional_or_keyword")),
2215-
123))
2214+
('b', ..., repr('foo'), "positional_or_keyword")),
2215+
'123'))
22162216

22172217
def test_signature_on_wkwonly(self):
22182218
def test(*, a:float, b:str) -> int:
22192219
pass
22202220
self.assertEqual(self.signature(test),
2221-
((('a', ..., float, "keyword_only"),
2222-
('b', ..., str, "keyword_only")),
2223-
int))
2221+
((('a', ..., 'float', "keyword_only"),
2222+
('b', ..., 'str', "keyword_only")),
2223+
'int'))
22242224

22252225
def test_signature_on_complex_args(self):
22262226
def test(a, b:'foo'=10, *args:'bar', spam:'baz', ham=123, **kwargs:int):
22272227
pass
22282228
self.assertEqual(self.signature(test),
22292229
((('a', ..., ..., "positional_or_keyword"),
2230-
('b', 10, 'foo', "positional_or_keyword"),
2231-
('args', ..., 'bar', "var_positional"),
2232-
('spam', ..., 'baz', "keyword_only"),
2230+
('b', 10, repr('foo'), "positional_or_keyword"),
2231+
('args', ..., repr('bar'), "var_positional"),
2232+
('spam', ..., repr('baz'), "keyword_only"),
22332233
('ham', 123, ..., "keyword_only"),
2234-
('kwargs', ..., int, "var_keyword")),
2234+
('kwargs', ..., 'int', "var_keyword")),
22352235
...))
22362236

22372237
def test_signature_without_self(self):
@@ -2466,7 +2466,7 @@ def __call__(*, a):
24662466
self.assertEqual(self.signature(Test().m1),
24672467
((('arg1', ..., ..., "positional_or_keyword"),
24682468
('arg2', 1, ..., "positional_or_keyword")),
2469-
int))
2469+
'int'))
24702470

24712471
self.assertEqual(self.signature(Test().m2),
24722472
((('args', ..., ..., "var_positional"),),
@@ -2490,7 +2490,7 @@ def m1d(*args, **kwargs):
24902490
self.assertEqual(self.signature(m1d),
24912491
((('arg1', ..., ..., "positional_or_keyword"),
24922492
('arg2', 1, ..., "positional_or_keyword")),
2493-
int))
2493+
'int'))
24942494

24952495
def test_signature_on_classmethod(self):
24962496
class Test:
@@ -2640,12 +2640,12 @@ def test(a, b, c:int) -> 42:
26402640

26412641
self.assertEqual(self.signature(partial(partial(test, 1))),
26422642
((('b', ..., ..., "positional_or_keyword"),
2643-
('c', ..., int, "positional_or_keyword")),
2644-
42))
2643+
('c', ..., 'int', "positional_or_keyword")),
2644+
'42'))
26452645

26462646
self.assertEqual(self.signature(partial(partial(test, 1), 2)),
2647-
((('c', ..., int, "positional_or_keyword"),),
2648-
42))
2647+
((('c', ..., 'int', "positional_or_keyword"),),
2648+
'42'))
26492649

26502650
psig = inspect.signature(partial(partial(test, 1), 2))
26512651

@@ -2764,12 +2764,12 @@ def test(it, a, *, c) -> 'spam':
27642764
((('it', ..., ..., 'positional_or_keyword'),
27652765
('a', ..., ..., 'positional_or_keyword'),
27662766
('c', 1, ..., 'keyword_only')),
2767-
'spam'))
2767+
repr('spam')))
27682768

27692769
self.assertEqual(self.signature(Spam().ham),
27702770
((('a', ..., ..., 'positional_or_keyword'),
27712771
('c', 1, ..., 'keyword_only')),
2772-
'spam'))
2772+
repr('spam')))
27732773

27742774
class Spam:
27752775
def test(self: 'anno', x):
@@ -2778,7 +2778,7 @@ def test(self: 'anno', x):
27782778
g = partialmethod(test, 1)
27792779

27802780
self.assertEqual(self.signature(Spam.g),
2781-
((('self', ..., 'anno', 'positional_or_keyword'),),
2781+
((('self', ..., repr('anno'), 'positional_or_keyword'),),
27822782
...))
27832783

27842784
def test_signature_on_fake_partialmethod(self):
@@ -3116,20 +3116,16 @@ def foo(a={}): pass
31163116
with self.assertRaisesRegex(TypeError, 'unhashable type'):
31173117
hash(inspect.signature(foo))
31183118

3119-
def foo(a) -> {}: pass
3120-
with self.assertRaisesRegex(TypeError, 'unhashable type'):
3121-
hash(inspect.signature(foo))
3122-
31233119
def test_signature_str(self):
31243120
def foo(a:int=1, *, b, c=None, **kwargs) -> 42:
31253121
pass
31263122
self.assertEqual(str(inspect.signature(foo)),
3127-
'(a: int = 1, *, b, c=None, **kwargs) -> 42')
3123+
'(a: \'int\' = 1, *, b, c=None, **kwargs) -> \'42\'')
31283124

31293125
def foo(a:int=1, *args, b, c=None, **kwargs) -> 42:
31303126
pass
31313127
self.assertEqual(str(inspect.signature(foo)),
3132-
'(a: int = 1, *args, b, c=None, **kwargs) -> 42')
3128+
'(a: \'int\' = 1, *args, b, c=None, **kwargs) -> \'42\'')
31333129

31343130
def foo():
31353131
pass
@@ -3172,8 +3168,8 @@ def test() -> 42:
31723168
self.assertIs(sig.return_annotation, None)
31733169
sig = sig.replace(return_annotation=sig.empty)
31743170
self.assertIs(sig.return_annotation, sig.empty)
3175-
sig = sig.replace(return_annotation=42)
3176-
self.assertEqual(sig.return_annotation, 42)
3171+
sig = sig.replace(return_annotation='42')
3172+
self.assertEqual(sig.return_annotation, '42')
31773173
self.assertEqual(sig, inspect.signature(test))
31783174

31793175
def test_signature_on_mangled_parameters(self):
@@ -3185,8 +3181,8 @@ class Ham(Spam):
31853181

31863182
self.assertEqual(self.signature(Spam.foo),
31873183
((('self', ..., ..., "positional_or_keyword"),
3188-
('_Spam__p1', 2, 1, "positional_or_keyword"),
3189-
('_Spam__p2', 3, 2, "keyword_only")),
3184+
('_Spam__p1', 2, '1', "positional_or_keyword"),
3185+
('_Spam__p2', 3, '2', "keyword_only")),
31903186
...))
31913187

31923188
self.assertEqual(self.signature(Spam.foo),

0 commit comments

Comments
 (0)