@@ -1364,6 +1364,13 @@ def getargs(co):
1364
1364
'args' is the list of argument names. Keyword-only arguments are
1365
1365
appended. 'varargs' and 'varkw' are the names of the * and **
1366
1366
arguments or None."""
1367
+ import warnings
1368
+ warnings .warn (
1369
+ 'getargs is deprecated since Python3.13, '
1370
+ 'use Signature.from_code instead' ,
1371
+ DeprecationWarning ,
1372
+ )
1373
+
1367
1374
if not iscode (co ):
1368
1375
raise TypeError ('{!r} is not a code object' .format (co ))
1369
1376
@@ -2384,52 +2391,34 @@ def p(name_node, default_node, default=empty):
2384
2391
return cls (parameters , return_annotation = cls .empty )
2385
2392
2386
2393
2387
- def _signature_from_builtin (cls , func , skip_bound_arg = True ):
2394
+ def _signature_from_code (cls ,
2395
+ func_code ,
2396
+ globals = None ,
2397
+ locals = None ,
2398
+ eval_str = False ,
2399
+ is_duck_function = False ,
2400
+ func = None ):
2388
2401
"""Private helper function to get signature for
2389
- builtin callables .
2402
+ code objects .
2390
2403
"""
2391
-
2392
- if not _signature_is_builtin (func ):
2393
- raise TypeError ("{!r} is not a Python builtin "
2394
- "function" .format (func ))
2395
-
2396
- s = getattr (func , "__text_signature__" , None )
2397
- if not s :
2398
- raise ValueError ("no signature found for builtin {!r}" .format (func ))
2399
-
2400
- return _signature_fromstr (cls , func , s , skip_bound_arg )
2401
-
2402
-
2403
- def _signature_from_function (cls , func , skip_bound_arg = True ,
2404
- globals = None , locals = None , eval_str = False ):
2405
- """Private helper: constructs Signature for the given python function."""
2406
-
2407
- is_duck_function = False
2408
- if not isfunction (func ):
2409
- if _signature_is_functionlike (func ):
2410
- is_duck_function = True
2411
- else :
2412
- # If it's not a pure Python function, and not a duck type
2413
- # of pure function:
2414
- raise TypeError ('{!r} is not a Python function' .format (func ))
2415
-
2416
- s = getattr (func , "__text_signature__" , None )
2417
- if s :
2418
- return _signature_fromstr (cls , func , s , skip_bound_arg )
2419
-
2420
2404
Parameter = cls ._parameter_cls
2421
2405
2422
2406
# Parameter information.
2423
- func_code = func .__code__
2424
2407
pos_count = func_code .co_argcount
2425
2408
arg_names = func_code .co_varnames
2426
2409
posonly_count = func_code .co_posonlyargcount
2427
2410
positional = arg_names [:pos_count ]
2428
2411
keyword_only_count = func_code .co_kwonlyargcount
2429
2412
keyword_only = arg_names [pos_count :pos_count + keyword_only_count ]
2430
- annotations = get_annotations (func , globals = globals , locals = locals , eval_str = eval_str )
2431
- defaults = func .__defaults__
2432
- kwdefaults = func .__kwdefaults__
2413
+ if func is not None :
2414
+ annotations = get_annotations (func , globals = globals , locals = locals , eval_str = eval_str )
2415
+ defaults = func .__defaults__
2416
+ kwdefaults = func .__kwdefaults__
2417
+ else :
2418
+ # `func` can be `None` when we get a signature from just a `CodeObject`
2419
+ annotations = {}
2420
+ defaults = None
2421
+ kwdefaults = None
2433
2422
2434
2423
if defaults :
2435
2424
pos_default_count = len (defaults )
@@ -2495,6 +2484,46 @@ def _signature_from_function(cls, func, skip_bound_arg=True,
2495
2484
__validate_parameters__ = is_duck_function )
2496
2485
2497
2486
2487
+ def _signature_from_builtin (cls , func , skip_bound_arg = True ):
2488
+ """Private helper function to get signature for
2489
+ builtin callables.
2490
+ """
2491
+
2492
+ if not _signature_is_builtin (func ):
2493
+ raise TypeError ("{!r} is not a Python builtin "
2494
+ "function" .format (func ))
2495
+
2496
+ s = getattr (func , "__text_signature__" , None )
2497
+ if not s :
2498
+ raise ValueError ("no signature found for builtin {!r}" .format (func ))
2499
+
2500
+ return _signature_fromstr (cls , func , s , skip_bound_arg )
2501
+
2502
+
2503
+ def _signature_from_function (cls , func , skip_bound_arg = True ,
2504
+ globals = None , locals = None , eval_str = False ):
2505
+ """Private helper: constructs Signature for the given python function."""
2506
+
2507
+ is_duck_function = False
2508
+ if not isfunction (func ):
2509
+ if _signature_is_functionlike (func ):
2510
+ is_duck_function = True
2511
+ else :
2512
+ # If it's not a pure Python function, and not a duck type
2513
+ # of pure function:
2514
+ raise TypeError ('{!r} is not a Python function' .format (func ))
2515
+
2516
+ s = getattr (func , "__text_signature__" , None )
2517
+ if s :
2518
+ return _signature_fromstr (cls , func , s , skip_bound_arg )
2519
+ return _signature_from_code (cls , func .__code__ ,
2520
+ globals = globals ,
2521
+ locals = locals ,
2522
+ eval_str = eval_str ,
2523
+ is_duck_function = is_duck_function ,
2524
+ func = func )
2525
+
2526
+
2498
2527
def _signature_from_callable (obj , * ,
2499
2528
follow_wrapper_chains = True ,
2500
2529
skip_bound_arg = True ,
@@ -3107,6 +3136,17 @@ def from_callable(cls, obj, *,
3107
3136
follow_wrapper_chains = follow_wrapped ,
3108
3137
globals = globals , locals = locals , eval_str = eval_str )
3109
3138
3139
+ @classmethod
3140
+ def from_code (cls , co ):
3141
+ """Constructs Signature for the given code object.
3142
+
3143
+ Signatures created from code objects cannot know about annotations
3144
+ or default values.
3145
+ """
3146
+ if not iscode (co ):
3147
+ raise TypeError ('{!r} is not a code object' .format (co ))
3148
+ return _signature_from_code (cls , co )
3149
+
3110
3150
@property
3111
3151
def parameters (self ):
3112
3152
return self ._parameters
0 commit comments