[Python-checkins] gh-92062: `inspect.Parameter` checks whether `name` is a keyword (GH-92065)
miss-islington
webhook-mailer at python.org
Tue May 3 16:52:41 EDT 2022
https://github.com/python/cpython/commit/65f88a6ef74c9b01017438e88e31570b02f1df9c
commit: 65f88a6ef74c9b01017438e88e31570b02f1df9c
branch: main
author: Zac Hatfield-Dodds <zac.hatfield.dodds at gmail.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2022-05-03T13:52:30-07:00
summary:
gh-92062: `inspect.Parameter` checks whether `name` is a keyword (GH-92065)
Fixes #92062.
files:
A Misc/NEWS.d/next/Library/2022-04-29-18-15-23.gh-issue-92062.X2c_Rj.rst
M Lib/inspect.py
M Lib/test/test_inspect.py
diff --git a/Lib/inspect.py b/Lib/inspect.py
index 5bc9c04b22e23..6e744712f014b 100644
--- a/Lib/inspect.py
+++ b/Lib/inspect.py
@@ -149,6 +149,7 @@
import types
import functools
import builtins
+from keyword import iskeyword
from operator import attrgetter
from collections import namedtuple, OrderedDict
@@ -1645,7 +1646,7 @@ def __new__(cls, filename, lineno, function, code_context, index, *, positions=N
instance = super().__new__(cls, filename, lineno, function, code_context, index)
instance.positions = positions
return instance
-
+
def __repr__(self):
return ('Traceback(filename={!r}, lineno={!r}, function={!r}, '
'code_context={!r}, index={!r}, positions={!r})'.format(
@@ -1683,7 +1684,7 @@ def getframeinfo(frame, context=1):
frame, *positions = (frame, lineno, *positions[1:])
else:
frame, *positions = (frame, *positions)
-
+
lineno = positions[0]
if not isframe(frame):
@@ -2707,7 +2708,10 @@ def __init__(self, name, kind, *, default=_empty, annotation=_empty):
self._kind = _POSITIONAL_ONLY
name = 'implicit{}'.format(name[1:])
- if not name.isidentifier():
+ # It's possible for C functions to have a positional-only parameter
+ # where the name is a keyword, so for compatibility we'll allow it.
+ is_keyword = iskeyword(name) and self._kind is not _POSITIONAL_ONLY
+ if is_keyword or not name.isidentifier():
raise ValueError('{!r} is not a valid parameter name'.format(name))
self._name = name
diff --git a/Lib/test/test_inspect.py b/Lib/test/test_inspect.py
index 115e97b77e079..fe0259ab609c7 100644
--- a/Lib/test/test_inspect.py
+++ b/Lib/test/test_inspect.py
@@ -3604,6 +3604,9 @@ def test_signature_parameter_object(self):
with self.assertRaisesRegex(ValueError, 'not a valid parameter name'):
inspect.Parameter('1', kind=inspect.Parameter.VAR_KEYWORD)
+ with self.assertRaisesRegex(ValueError, 'not a valid parameter name'):
+ inspect.Parameter('from', kind=inspect.Parameter.VAR_KEYWORD)
+
with self.assertRaisesRegex(TypeError, 'name must be a str'):
inspect.Parameter(None, kind=inspect.Parameter.VAR_KEYWORD)
diff --git a/Misc/NEWS.d/next/Library/2022-04-29-18-15-23.gh-issue-92062.X2c_Rj.rst b/Misc/NEWS.d/next/Library/2022-04-29-18-15-23.gh-issue-92062.X2c_Rj.rst
new file mode 100644
index 0000000000000..1ccb779a6d1d9
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-04-29-18-15-23.gh-issue-92062.X2c_Rj.rst
@@ -0,0 +1,2 @@
+:class:`inspect.Parameter` now raises :exc:`ValueError` if ``name`` is
+a keyword, in addition to the existing check that it is an identifier.
More information about the Python-checkins
mailing list