summaryrefslogtreecommitdiff
path: root/Lib/inspect.py
diff options
context:
space:
mode:
authorNick Coghlan <ncoghlan@gmail.com>2016-06-04 14:40:03 -0700
committerNick Coghlan <ncoghlan@gmail.com>2016-06-04 14:40:03 -0700
commitb4b966ece26b4fc14c94fa9bccd831d5d5c93aab (patch)
treec9c4ef12406580a4bd91ca071be4125bf4855e2a /Lib/inspect.py
parentd62548afede899e71e59a2a0b31f19fdf031c560 (diff)
downloadcpython-git-b4b966ece26b4fc14c94fa9bccd831d5d5c93aab.tar.gz
Issue #19611: handle implicit parameters in inspect.signature
inspect.signature now reports the implicit ``.0`` parameters generated by the compiler for comprehension and generator expression scopes as if they were positional-only parameters called ``implicit0``. Patch by Jelle Zijlstra.
Diffstat (limited to 'Lib/inspect.py')
-rw-r--r--Lib/inspect.py14
1 files changed, 14 insertions, 0 deletions
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 582bb0e7fa..45d2110106 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -2396,6 +2396,20 @@ class Parameter:
if not isinstance(name, str):
raise TypeError("name must be a str, not a {!r}".format(name))
+ if name[0] == '.' and name[1:].isdigit():
+ # These are implicit arguments generated by comprehensions. In
+ # order to provide a friendlier interface to users, we recast
+ # their name as "implicitN" and treat them as positional-only.
+ # See issue 19611.
+ if kind != _POSITIONAL_OR_KEYWORD:
+ raise ValueError(
+ 'implicit arguments must be passed in as {}'.format(
+ _POSITIONAL_OR_KEYWORD
+ )
+ )
+ self._kind = _POSITIONAL_ONLY
+ name = 'implicit{}'.format(name[1:])
+
if not name.isidentifier():
raise ValueError('{!r} is not a valid parameter name'.format(name))