|
| 1 | +[case testSignaturesBasic] |
| 2 | +import inspect |
| 3 | + |
| 4 | +def f1(a, /, b, c=None, *args, d=None, **h): pass |
| 5 | +def f2(a=None, /): pass |
| 6 | +def f3(*, a): pass |
| 7 | +def f4(): pass |
| 8 | + |
| 9 | +def test_basic() -> None: |
| 10 | + assert str(inspect.signature(f1)) == "(a, /, b, c=None, *args, d=None, **h)" |
| 11 | + assert str(inspect.signature(f2)) == "(a=None, /)" |
| 12 | + assert str(inspect.signature(f3)) == "(*, a)" |
| 13 | + assert str(inspect.signature(f4)) == "()" |
| 14 | + |
| 15 | +[case testSignaturesValidDefaults] |
| 16 | +import inspect |
| 17 | + |
| 18 | +def valid_defaults( |
| 19 | + int=1, |
| 20 | + str="a", |
| 21 | + float=1.0, |
| 22 | + false=False, |
| 23 | + true=True, |
| 24 | + none=None, |
| 25 | + empty_tuple=(), |
| 26 | + misc_tuple=(1, "a", 1.0, False, True, None, (), (1,2,(3,4))), |
| 27 | +): pass |
| 28 | + |
| 29 | +def valid_tuple_singleton(x=(1,)): pass |
| 30 | + |
| 31 | +def test_valid_defaults() -> None: |
| 32 | + assert ( |
| 33 | + str(inspect.signature(valid_defaults)) |
| 34 | + == "(int=1, str='a', float=1.0, false=False, true=True, none=None, " |
| 35 | + "empty_tuple=(), misc_tuple=(1, 'a', 1.0, False, True, None, (), (1, 2, (3, 4))))" |
| 36 | + ) |
| 37 | + |
| 38 | + # Check __text_signature__ directly since inspect.signature produces |
| 39 | + # an incorrect signature for 1-tuple default arguments prior to |
| 40 | + # Python 3.12 (cpython#102379). |
| 41 | + assert getattr(valid_tuple_singleton, "__text_signature__") == "(x=(1,))" |
| 42 | + |
| 43 | +[case testSignaturesStringDefaults] |
| 44 | +import inspect |
| 45 | + |
| 46 | +def f1(x="'foo"): pass |
| 47 | +def f2(x='"foo'): pass |
| 48 | +def f3(x=""""Isn\'t," they said."""): pass |
| 49 | +def f4(x="\\ \a \b \f \n \r \t \v \x00"): pass |
| 50 | +def f5(x="\U0001F34Csv"): pass |
| 51 | + |
| 52 | +def test_string_defaults() -> None: |
| 53 | + assert str(inspect.signature(f1)) == """(x="'foo")""" |
| 54 | + assert str(inspect.signature(f2)) == """(x='"foo')""" |
| 55 | + assert str(inspect.signature(f3)) == r"""(x='"Isn\'t," they said.')""" |
| 56 | + assert str(inspect.signature(f4)) == r"""(x='\\ \x07 \x08 \x0c \n \r \t \x0b \x00')""" |
| 57 | + assert str(inspect.signature(f5)) == """(x='\U0001F34Csv')""" |
| 58 | + |
| 59 | +[case testSignaturesIrrepresentableDefaults] |
| 60 | +import inspect |
| 61 | +from typing import Any |
| 62 | + |
| 63 | +from testutil import assertRaises |
| 64 | + |
| 65 | +def bad1(x=[]): pass |
| 66 | +def bad2(x={}): pass |
| 67 | +def bad3(x=set()): pass |
| 68 | +def bad4(x=int): pass |
| 69 | +def bad5(x=lambda: None): pass |
| 70 | +def bad6(x=bad1): pass |
| 71 | +# note: inspect supports constant folding for defaults in text signatures |
| 72 | +def bad7(x=1+2): pass |
| 73 | +def bad8(x=1-2): pass |
| 74 | +def bad9(x=1|2): pass |
| 75 | +def bad10(x=float("nan")): pass |
| 76 | + |
| 77 | +def test_irrepresentable_defaults() -> None: |
| 78 | + bad: Any |
| 79 | + for bad in [bad1, bad2, bad3, bad4, bad5, bad6, bad7, bad8, bad9, bad10]: |
| 80 | + assert bad.__text_signature__ is None, f"{bad.__name__} has unexpected __text_signature__" |
| 81 | + with assertRaises(ValueError, "no signature found for builtin"): |
| 82 | + inspect.signature(bad) |
| 83 | + |
| 84 | + |
| 85 | +[case testSignaturesMethods] |
| 86 | +import inspect |
| 87 | + |
| 88 | +class Foo: |
| 89 | + def f1(self, x): pass |
| 90 | + @classmethod |
| 91 | + def f2(cls, x): pass |
| 92 | + @staticmethod |
| 93 | + def f3(x): pass |
| 94 | + |
| 95 | +def test_methods() -> None: |
| 96 | + assert getattr(Foo.f1, "__text_signature__") == "($self, x)" |
| 97 | + assert str(inspect.signature(Foo.f1)) == "(self, /, x)" |
| 98 | + |
| 99 | + assert getattr(Foo.f2, "__text_signature__") == "($cls, x)" |
| 100 | + assert str(inspect.signature(Foo.f2)) == "(x)" |
| 101 | + |
| 102 | + assert getattr(Foo.f3, "__text_signature__") == "(x)" |
| 103 | + assert str(inspect.signature(Foo.f3)) == "(x)" |
| 104 | + |
| 105 | + assert getattr(Foo().f1, "__text_signature__") == "($self, x)" |
| 106 | + assert str(inspect.signature(Foo().f1)) == "(x)" |
| 107 | + |
| 108 | + assert getattr(Foo().f2, "__text_signature__") == "($cls, x)" |
| 109 | + assert str(inspect.signature(Foo().f2)) == "(x)" |
| 110 | + |
| 111 | + assert getattr(Foo().f3, "__text_signature__") == "(x)" |
| 112 | + assert str(inspect.signature(Foo().f3)) == "(x)" |
0 commit comments