Skip to content

Conversation

dhruvmanila
Copy link
Member

Summary

Previously, the name field was on Parameter which required it to be always optional regardless of the parameter kind because a typing.Callable signature does not have name for the parameters. This is the case for positional-only parameters. This wasn't enforced at the type level which meant that downstream usages would have to unwrap on name even though it's guaranteed to be present.

This commit moves the name field from Parameter to the ParameterKind variants and makes it optional only for ParameterKind::PositionalOnly variant while required for all other variants.

One change that's now required is that a Callable form using a gradual form for parameter types (...) would have a default args and kwargs name used for variadic and keyword-variadic parameter kind respectively. This is also the case for invalid Callable type forms. I think this is fine as names are not relevant in this context but happy to make it optional even in variadic variants.

Test Plan

No new tests; make sure existing tests are passing.

Previously, the `name` field was on `Parameter` which required it to
be always optional regardless of the parameter kind because a
`typing.Callable` signature does not have name for the parameters.
This is the case for positional-only parameters. This wasn't enforced
at the type level which meant that downstream usages would have to
unwrap on `name` even though it's guaranteed to be present.

This commit moves the `name` field from `Parameter` to the
`ParameterKind` variants and makes it optional only for
`ParameterKind::PositionalOnly` variant while required for all other
variants.

One change that's now required is that a `Callable` form using a
gradual form for parameter types (`...`) would have a default `args`
and `kwargs` name used for variadic and keyword-variadic parameter
kind respectively. This is also the case for invalid `Callable` type
forms. I think this is fine as names are not relevant in this context
but happy to make it optional even in variadic variants.
@dhruvmanila dhruvmanila added the ty Multi-file analysis & type inference label Mar 18, 2025
Copy link
Contributor

mypy_primer results

No ecosystem changes detected ✅

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@dhruvmanila dhruvmanila merged commit c3d429d into main Mar 18, 2025
23 checks passed
@dhruvmanila dhruvmanila deleted the dhruv/parameter-name branch March 18, 2025 17:17
dcreager added a commit that referenced this pull request Mar 18, 2025
* main:
  [playground] Avoid concurrent deployments (#16834)
  [red-knot] Infer `lambda` return type as `Unknown` (#16695)
  [red-knot] Move `name` field on parameter kind (#16830)
  [red-knot] Emit errors for more AST nodes that are invalid (or only valid in specific contexts) in type expressions (#16822)
  [playground] Use cursor for clickable elements (#16833)
  [red-knot] Deploy playground on main (#16832)
  Red Knot Playground (#12681)
  [syntax-errors] PEP 701 f-strings before Python 3.12 (#16543)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ty Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants