You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.Parameter checks that name is an identifier, but does not check for keywords #92062
This parses, but it doesn't compile: from is a keyword and so the code is syntactially invalid.
The problem is that inspect.Parameter checks that the given name is an identifier, but doesn't check that it's not a keyword. You can thus create invalid signatures, which cause considerable confusion on downstream introspection:
deff(source):
passfrominspectimportSignature, Parameterf.__signature__=Signature([Parameter("from", P.KEYWORD_ONLY)], return_annotation=None)
# Or, as the real-world motivation,classModel(pydantic.BaseModel):
source: None=pydantic.Field(None, alias="from")
We're fixing this downstream in HypothesisWorks/hypothesis#3317 and pydantic/pydantic#4012, but I think that inspect.Parameter("from", ...) should also raise an error, just as if the provided name was not an identifier at all.
I can see a scenario where this will break things: we try to get the signature for a function implemented in C that takes a positional-only argument with a name that happens to be a keyword. I can't find any instances of that pattern in typeshed or CPython, but they may exist elsewhere.
Hmm, that makes sense. I'll tighten the check to only apply if the kind is not P.POSITIONAL_ONLY, since I don't expect this to weaken it as a check on Python code 👍
I think we're fine to continue ignoring unicode names which normalize to keywords, since that's the existing behaviour of keyword.iskeyword and does actually work at runtime:
>>>deff(def): # non-ASCII "e" in argument name
... print(locals())
... f(1)
{'def': 1}
Activity
pythongh-92062: reject keyword parameter names
JelleZijlstra commentedon Apr 30, 2022
I can see a scenario where this will break things: we try to get the signature for a function implemented in C that takes a positional-only argument with a name that happens to be a keyword. I can't find any instances of that pattern in typeshed or CPython, but they may exist elsewhere.
Zac-HD commentedon Apr 30, 2022
Hmm, that makes sense. I'll tighten the check to only apply if the
kind is not P.POSITIONAL_ONLY, since I don't expect this to weaken it as a check on Python code 👍pythongh-92062: allow pos-only keyword names
Zac-HD commentedon May 2, 2022
I think we're fine to continue ignoring unicode names which normalize to keywords, since that's the existing behaviour of
keyword.iskeywordand does actually work at runtime:gh-92062: `inspect.Parameter` checks whether `name` is a keyword (GH-…