Skip to content

Python: type and share the Variable of a *args or **kwargs declaration - #10314

Merged
knutwannheden merged 4 commits into
mainfrom
python-type-the-declaration-of-args-and-kwargs
Dec 4, 2026
Merged

knutwannheden merged 4 commits into
mainfrom
python-type-the-declaration-of-args-and-kwargs

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

In rewrite-python, the declaration of a *args or **kwargs parameter got no type and no field type, though its uses inside the function were typed. For def g(*args: int): return args, the declared args had type and field_type of None, while the returned args was typed tuple[int, ...].

Type and binding lookups are keyed by a node's (start, end) byte range. Python's ast.arg for a star parameter starts at the name, but ty's Parameter node starts at the stars, so both lookups missed. The binding carries the symbol id that #10313 uses to share one JavaType.Variable between a declaration and its uses.

ty Parameter   6..16   '*args: int'   type 1057
ast.arg        7..16    'args: int'
use of args   45..49    'args'        type 1057

What the change does:

  • The parser passes the number of stars (1 or 2) to param_type_info.
  • _star_param_range finds ty's parameter node: the node that ends where the ast.arg ends and starts nearest before its name. It is accepted only when the source at that start is the stars, which also covers a line break or comment between the stars and the name.
  • Both the type and the binding are read at that range. _shared_variable takes the binding rather than the node, so each caller looks it up at the range that fits.

test_a_star_parameters_declaration_shares_the_variable_of_its_uses covers *args and **kwargs, the latter with a comment and line break after **. It asserts that each declaration's Variable is the same object as its use's field type. It was mutation-checked: it fails when the binding is looked up at the name's own range, and when no range is found.

Python's ast starts a vararg or kwarg parameter at its name, while ty's
Parameter node range includes the stars, so the (start, end) lookup
missed and the declared identifier got no type and no field type. The
parser now passes the star count, and the lookup starts at the stars.
…s uses

ty attaches its symbol id to the Parameter node, whose range starts at
the stars, so the binding lookup for a star parameter has to use the same
star-adjusted range as its type lookup. _shared_variable now takes the
binding rather than the node, and each caller looks it up.
ty's parameter node is the one ending where the ast.arg ends and starting
nearest before its name, so the range is found from the node index rather
than from the name's own line.
@knutwannheden knutwannheden self-assigned this Dec 4, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Dec 4, 2026
@knutwannheden
knutwannheden merged commit 6f91928 into main Dec 4, 2026
1 check passed
@knutwannheden
knutwannheden deleted the python-type-the-declaration-of-args-and-kwargs branch December 4, 2026 07:38
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant