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