Closed
Description
Consider the following code:
def send(from):
...
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:
def f(source):
pass
from inspect import Signature, Parameter
f.__signature__ = Signature([Parameter("from", P.KEYWORD_ONLY)], return_annotation=None)
# Or, as the real-world motivation,
class Model(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.