@@ -3796,26 +3796,36 @@ def foo(a:int=1, *, b, c=None, **kwargs) -> 42:
37963796 pass
37973797 self .assertEqual (str (inspect .signature (foo )),
37983798 '(a: int = 1, *, b, c=None, **kwargs) -> 42' )
3799+ self .assertEqual (str (inspect .signature (foo )),
3800+ inspect .signature (foo ).format ())
37993801
38003802 def foo (a :int = 1 , * args , b , c = None , ** kwargs ) -> 42 :
38013803 pass
38023804 self .assertEqual (str (inspect .signature (foo )),
38033805 '(a: int = 1, *args, b, c=None, **kwargs) -> 42' )
3806+ self .assertEqual (str (inspect .signature (foo )),
3807+ inspect .signature (foo ).format ())
38043808
38053809 def foo ():
38063810 pass
38073811 self .assertEqual (str (inspect .signature (foo )), '()' )
3812+ self .assertEqual (str (inspect .signature (foo )),
3813+ inspect .signature (foo ).format ())
38083814
38093815 def foo (a : list [str ]) -> tuple [str , float ]:
38103816 pass
38113817 self .assertEqual (str (inspect .signature (foo )),
38123818 '(a: list[str]) -> tuple[str, float]' )
3819+ self .assertEqual (str (inspect .signature (foo )),
3820+ inspect .signature (foo ).format ())
38133821
38143822 from typing import Tuple
38153823 def foo (a : list [str ]) -> Tuple [str , float ]:
38163824 pass
38173825 self .assertEqual (str (inspect .signature (foo )),
38183826 '(a: list[str]) -> Tuple[str, float]' )
3827+ self .assertEqual (str (inspect .signature (foo )),
3828+ inspect .signature (foo ).format ())
38193829
38203830 def test_signature_str_positional_only (self ):
38213831 P = inspect .Parameter
@@ -3826,19 +3836,85 @@ def test(a_po, /, *, b, **kwargs):
38263836
38273837 self .assertEqual (str (inspect .signature (test )),
38283838 '(a_po, /, *, b, **kwargs)' )
3839+ self .assertEqual (str (inspect .signature (test )),
3840+ inspect .signature (test ).format ())
3841+
3842+ test = S (parameters = [P ('foo' , P .POSITIONAL_ONLY )])
3843+ self .assertEqual (str (test ), '(foo, /)' )
3844+ self .assertEqual (str (test ), test .format ())
38293845
3830- self .assertEqual (str (S (parameters = [P ('foo' , P .POSITIONAL_ONLY )])),
3831- '(foo, /)' )
3846+ test = S (parameters = [P ('foo' , P .POSITIONAL_ONLY ),
3847+ P ('bar' , P .VAR_KEYWORD )])
3848+ self .assertEqual (str (test ), '(foo, /, **bar)' )
3849+ self .assertEqual (str (test ), test .format ())
38323850
3833- self . assertEqual ( str ( S (parameters = [
3834- P ('foo ' , P .POSITIONAL_ONLY ),
3835- P ( ' bar' , P . VAR_KEYWORD )])),
3836- '(foo, /, **bar)' )
3851+ test = S (parameters = [P ( 'foo' , P . POSITIONAL_ONLY ),
3852+ P ('bar ' , P .VAR_POSITIONAL )])
3853+ self . assertEqual ( str ( test ), '(foo, /, * bar)' )
3854+ self . assertEqual ( str ( test ), test . format () )
38373855
3838- self .assertEqual (str (S (parameters = [
3839- P ('foo' , P .POSITIONAL_ONLY ),
3840- P ('bar' , P .VAR_POSITIONAL )])),
3841- '(foo, /, *bar)' )
3856+ def test_signature_format (self ):
3857+ from typing import Annotated , Literal
3858+
3859+ def func (x : Annotated [int , 'meta' ], y : Literal ['a' , 'b' ], z : 'LiteralString' ):
3860+ pass
3861+
3862+ expected_singleline = "(x: Annotated[int, 'meta'], y: Literal['a', 'b'], z: 'LiteralString')"
3863+ expected_multiline = """(
3864+ x: Annotated[int, 'meta'],
3865+ y: Literal['a', 'b'],
3866+ z: 'LiteralString'
3867+ )"""
3868+ self .assertEqual (
3869+ inspect .signature (func ).format (),
3870+ expected_singleline ,
3871+ )
3872+ self .assertEqual (
3873+ inspect .signature (func ).format (max_width = None ),
3874+ expected_singleline ,
3875+ )
3876+ self .assertEqual (
3877+ inspect .signature (func ).format (max_width = len (expected_singleline )),
3878+ expected_singleline ,
3879+ )
3880+ self .assertEqual (
3881+ inspect .signature (func ).format (max_width = len (expected_singleline ) - 1 ),
3882+ expected_multiline ,
3883+ )
3884+ self .assertEqual (
3885+ inspect .signature (func ).format (max_width = 0 ),
3886+ expected_multiline ,
3887+ )
3888+ self .assertEqual (
3889+ inspect .signature (func ).format (max_width = - 1 ),
3890+ expected_multiline ,
3891+ )
3892+
3893+ def test_signature_format_all_arg_types (self ):
3894+ from typing import Annotated , Literal
3895+
3896+ def func (
3897+ x : Annotated [int , 'meta' ],
3898+ / ,
3899+ y : Literal ['a' , 'b' ],
3900+ * ,
3901+ z : 'LiteralString' ,
3902+ ** kwargs : object ,
3903+ ) -> None :
3904+ pass
3905+
3906+ expected_multiline = """(
3907+ x: Annotated[int, 'meta'],
3908+ /,
3909+ y: Literal['a', 'b'],
3910+ *,
3911+ z: 'LiteralString',
3912+ **kwargs: object
3913+ ) -> None"""
3914+ self .assertEqual (
3915+ inspect .signature (func ).format (max_width = - 1 ),
3916+ expected_multiline ,
3917+ )
38423918
38433919 def test_signature_replace_parameters (self ):
38443920 def test (a , b ) -> 42 :
0 commit comments