Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Argument Clinic: add support for renaming a parameter #108271

Open
erlend-aasland opened this issue Aug 22, 2023 · 5 comments
Open

Argument Clinic: add support for renaming a parameter #108271

erlend-aasland opened this issue Aug 22, 2023 · 5 comments
Labels
3.13 bugs and security fixes topic-argument-clinic type-feature A feature request or enhancement

Comments

@erlend-aasland
Copy link
Contributor

erlend-aasland commented Aug 22, 2023

Originally posted by @serhiy-storchaka in #95065 (comment):

  1. Rename positional-or-keyword or keyword-only parameters. In meantime the function should accept both old and new keyword names, but raise an exception if both are passed or if the argument is passed as positional and keyword. For example see Wrong keyword parameter name in regex pattern methods #64482.
@erlend-aasland erlend-aasland added type-feature A feature request or enhancement topic-argument-clinic 3.13 bugs and security fixes labels Aug 22, 2023
@erlend-aasland
Copy link
Contributor Author

Brainstorming regarding syntax, my first thought would be to modify parse_parameter(). Currently, we support the as keyword, to rename the C argument of a parameter. I think it would be too messy if we added another keyword before :. Messy example where we want to rename char as ch:

func:
    char as c rename ch in 3.14: int

Something like this would be cleaner:

func:
    char as c: int  # rename to ch in 3.14

Here's another alternative, which require changes to state_parameter():

func:
    @rename to ch in 3.14
    char as c: int

@erlend-aasland
Copy link
Contributor Author

erlend-aasland commented Aug 22, 2023

An better alternative can be to add that support to CConverter:

func:
    char as c: int(rename=("ch", 3.14)

@serhiy-storchaka
Copy link
Member

I considered similar variants:

  1. Special comment or [...] at the end of the line, like in * [from ...]. It will be not easy to parse, because parameter declarations are longer, can span multiple physical lines, and we need to specify at least two options: the new name and the final version.
  2. Decorator-like directive. There is a precedence of using decorator-like directives (from @classmethod to @text_signature), but there is no precedence of using them for parameters, so it will look weird. Perhaps we should use other prefix than @ for parameter decorators. We could also use
    @from 3.14
    *
    
    instead of
    * [from 3.14]
    
  3. As keyword argument to converter , like unused=True. It is the simplest way. Although I am unsure about mixing converter arguments and parameter arguments, but we already do this. Perhaps in future we will change syntax to differentiate them.

In addition to adding this feature we can add also allow add a parameter alias without deprecating the old name. One feature can be considered an extension of the other. What are ideas for this syntax? How can it be extended to add the deprecation of the old name? Should we support more than two names for the same parameter?

@erlend-aasland
Copy link
Contributor Author

I think there is value in keeping the DSL simple, so perhaps an argument to converter is the best option. I agree it is not ideal mixing converter and param arguments, but as you say, we already do.

@erlend-aasland
Copy link
Contributor Author

I worry that the decorator variant will be too complex for little gain. I agree it also looks weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes topic-argument-clinic type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants