Skip to content

Conversation

@copybara-service
Copy link

Support for anonymous repeated wildcards in pattern syntax.

(Non-anonymous repeated are syntactically allowed, for completeness, but discarded unless the name is _.)

This is a rough first pass, with several obvious gotchas:

  1. Since it uses globs, it depends on where a list occurs in the AST. For instance, dicts, which have a separate list of keys and values, can get spurious matches: {$...:$..., k1: v2, $...:$...} will match the dict {k1: v1, k2: v2}.

    That said, it does still let one sensibly write {$... : $...}, which is more than we can say for function calls!

  2. In the reverse of (1), if something is not a list, the glob doesn't apply there. For instance, one cannot currently use the pattern foo($...=$...) at all, because unlike dicts, they use a single list of keyword() nodes.

So followup CLs need to add special handling of repeated wildcards in specific places in the AST. In particular, a rough TODO list looks like:

  1. Either reject, or tie up the globs, for dicts.
  2. special-case the glob syntax in kwargs
  3. Special-case a glob that occurs in * or ** to also catch those. foo(*$..., **$...) should catch every call to foo with any args.

@copybara-service copybara-service bot force-pushed the copybara_568276609 branch 3 times, most recently from 105934a to 0236ee2 Compare September 27, 2023 17:53
(Non-anonymous repeated are syntactically allowed, for completeness, but discarded unless the name is `_`.)

This is a rough first pass, with several obvious gotchas:

1)  Since it uses globs, it depends on where a list occurs in the AST. For instance, dicts, which have a _separate_ list of keys and values, can get spurious matches: `{$...:$...,  k1: v2,  $...:$...}` will match the dict `{k1: v1, k2: v2}`.

    That said, it does still let one sensibly write `{$... : $...}`, which is more than we can say for function calls!

2) In the reverse of (1), if something is not a list, the glob doesn't apply there. For instance, one cannot currently use the pattern `foo($...=$...)` at all, because unlike dicts, they use a single list of `keyword()` nodes.

So followup CLs need to add special handling of repeated wildcards in specific places in the AST. In particular, a rough TODO list looks like:

1) Either reject, or tie up the globs, for dicts.
2) special-case the glob syntax in kwargs
3) Special-case a glob that occurs in `*` or `**` to also catch those. `foo(*$..., **$...)` should catch every call to `foo` with any args.

PiperOrigin-RevId: 568276609
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants