59
59
TypeGuard ,
60
60
)
61
61
from .find_unused import used
62
- from .signature import SigParameter , Signature , ParameterKind
62
+ from .signature import ELLIPSIS_PARAM , SigParameter , Signature , ParameterKind
63
63
from .safe import is_typing_name , is_instance_of_typing_name
64
64
from . import type_evaluation
65
65
from .value import (
75
75
MultiValuedValue ,
76
76
NO_RETURN_VALUE ,
77
77
NoReturnGuardExtension ,
78
+ ParamSpecArgsValue ,
79
+ ParamSpecKwargsValue ,
78
80
ParameterTypeGuardExtension ,
79
81
TypeGuardExtension ,
80
82
TypedValue ,
@@ -391,7 +393,7 @@ def _type_from_runtime(val: Any, ctx: Context, is_typeddict: bool = False) -> Va
391
393
args = typing_inspect .get_args (val )
392
394
return _value_of_origin_args (Callable , args , val , ctx )
393
395
elif val is AsynqCallable :
394
- return CallableValue (Signature .make ([], is_ellipsis_args = True , is_asynq = True ))
396
+ return CallableValue (Signature .make ([ELLIPSIS_PARAM ] , is_asynq = True ))
395
397
elif isinstance (val , type ):
396
398
return _maybe_typed_value (val )
397
399
elif val is None :
@@ -425,6 +427,10 @@ def _type_from_runtime(val: Any, ctx: Context, is_typeddict: bool = False) -> Va
425
427
return TypeVarValue (tv , bound = bound , constraints = constraints )
426
428
elif is_instance_of_typing_name (val , "ParamSpec" ):
427
429
return TypeVarValue (val , is_paramspec = True )
430
+ elif is_instance_of_typing_name (val , "ParamSpecArgs" ):
431
+ return ParamSpecArgsValue (val .__origin__ )
432
+ elif is_instance_of_typing_name (val , "ParamSpecKwargs" ):
433
+ return ParamSpecKwargsValue (val .__origin__ )
428
434
elif is_typing_name (val , "Final" ) or is_typing_name (val , "ClassVar" ):
429
435
return AnyValue (AnySource .incomplete_annotation )
430
436
elif typing_inspect .is_classvar (val ) or typing_inspect .is_final_type (val ):
@@ -467,14 +473,9 @@ def _type_from_runtime(val: Any, ctx: Context, is_typeddict: bool = False) -> Va
467
473
[TypeGuardExtension (_type_from_runtime (val .__type__ , ctx ))],
468
474
)
469
475
elif isinstance (val , AsynqCallable ):
470
- params , is_ellipsis_args = _callable_args_from_runtime (
471
- val .args , "AsynqCallable" , ctx
472
- )
476
+ params = _callable_args_from_runtime (val .args , "AsynqCallable" , ctx )
473
477
sig = Signature .make (
474
- params ,
475
- _type_from_runtime (val .return_type , ctx ),
476
- is_asynq = True ,
477
- is_ellipsis_args = is_ellipsis_args ,
478
+ params , _type_from_runtime (val .return_type , ctx ), is_asynq = True
478
479
)
479
480
return CallableValue (sig )
480
481
elif isinstance (val , ExternalType ):
@@ -506,22 +507,22 @@ def _type_from_runtime(val: Any, ctx: Context, is_typeddict: bool = False) -> Va
506
507
507
508
def _callable_args_from_runtime (
508
509
arg_types : Any , label : str , ctx : Context
509
- ) -> Tuple [ Sequence [SigParameter ], bool ]:
510
+ ) -> Sequence [SigParameter ]:
510
511
if arg_types is Ellipsis or arg_types == [Ellipsis ]:
511
- return [], True
512
+ return [ELLIPSIS_PARAM ]
512
513
elif type (arg_types ) in (tuple , list ):
513
514
if len (arg_types ) == 1 :
514
515
(arg ,) = arg_types
515
516
if arg is Ellipsis :
516
- return [], True
517
+ return [ELLIPSIS_PARAM ]
517
518
elif is_typing_name (getattr (arg , "__origin__" , None ), "Concatenate" ):
518
519
return _args_from_concatenate (arg , ctx )
519
520
elif is_instance_of_typing_name (arg , "ParamSpec" ):
520
521
param_spec = TypeVarValue (arg , is_paramspec = True )
521
522
param = SigParameter (
522
523
"__P" , kind = ParameterKind .PARAM_SPEC , annotation = param_spec
523
524
)
524
- return [param ], False
525
+ return [param ]
525
526
types = [_type_from_runtime (arg , ctx ) for arg in arg_types ]
526
527
params = [
527
528
SigParameter (
@@ -533,23 +534,21 @@ def _callable_args_from_runtime(
533
534
)
534
535
for i , typ in enumerate (types )
535
536
]
536
- return params , False
537
+ return params
537
538
elif is_instance_of_typing_name (arg_types , "ParamSpec" ):
538
539
param_spec = TypeVarValue (arg_types , is_paramspec = True )
539
540
param = SigParameter (
540
541
"__P" , kind = ParameterKind .PARAM_SPEC , annotation = param_spec
541
542
)
542
- return [param ], False
543
+ return [param ]
543
544
elif is_typing_name (getattr (arg_types , "__origin__" , None ), "Concatenate" ):
544
545
return _args_from_concatenate (arg_types , ctx )
545
546
else :
546
547
ctx .show_error (f"Invalid arguments to { label } : { arg_types !r} " )
547
- return [], True
548
+ return [ELLIPSIS_PARAM ]
548
549
549
550
550
- def _args_from_concatenate (
551
- concatenate : Any , ctx : Context
552
- ) -> Tuple [Sequence [SigParameter ], bool ]:
551
+ def _args_from_concatenate (concatenate : Any , ctx : Context ) -> Sequence [SigParameter ]:
553
552
types = [_type_from_runtime (arg , ctx ) for arg in concatenate .__args__ ]
554
553
params = [
555
554
SigParameter (
@@ -561,7 +560,7 @@ def _args_from_concatenate(
561
560
)
562
561
for i , annotation in enumerate (types )
563
562
]
564
- return params , False
563
+ return params
565
564
566
565
567
566
def _get_typeddict_value (
@@ -987,14 +986,8 @@ def _value_of_origin_args(
987
986
* arg_types , return_type = args
988
987
if len (arg_types ) == 1 and isinstance (arg_types [0 ], list ):
989
988
arg_types = arg_types [0 ]
990
- params , is_ellipsis_args = _callable_args_from_runtime (
991
- arg_types , "Callable" , ctx
992
- )
993
- sig = Signature .make (
994
- params ,
995
- _type_from_runtime (return_type , ctx ),
996
- is_ellipsis_args = is_ellipsis_args ,
997
- )
989
+ params = _callable_args_from_runtime (arg_types , "Callable" , ctx )
990
+ sig = Signature .make (params , _type_from_runtime (return_type , ctx ))
998
991
return CallableValue (sig )
999
992
elif is_typing_name (origin , "Annotated" ):
1000
993
origin , metadata = args
@@ -1077,10 +1070,7 @@ def _make_callable_from_value(
1077
1070
if args == KnownValue (Ellipsis ):
1078
1071
return CallableValue (
1079
1072
Signature .make (
1080
- [],
1081
- return_annotation = return_annotation ,
1082
- is_ellipsis_args = True ,
1083
- is_asynq = is_asynq ,
1073
+ [ELLIPSIS_PARAM ], return_annotation = return_annotation , is_asynq = is_asynq
1084
1074
)
1085
1075
)
1086
1076
elif isinstance (args , SequenceIncompleteValue ):
0 commit comments