Skip to content

Argument Clinic: add support for deprecating positional use of parameters #95065

Description

@erlend-aasland

Feature or enhancement

Suggesting to add syntax for producing deprecation warnings for positional use of optional parameters.

I made a proof-of-concept patch for this where I introduced a new special symbol x. Example use:

/*[clinic input]
mod.func
    stuff: object
    /
    x
    optarg: int = 128

This will then generate code that emits a DeprecationWarning if optarg is passed as a positional argument, but not if it is passed as a keyword.

We can use this feature to introduce deprecation warnings for parameters that will become keyword-only in future releases. When the deprecation period is done, we can simply replace the x with *, to really make the optional params keyword-only.

Pitch

Quoting @serhiy-storchaka, in issue #93057:

It is recommended to make optional rarely used arguments keyword-only. I do not think it will break much code, but we need a deprecation period for this.

The problem is that sqlite3.connect() was converted to Argument Clinic, and it was much easier to add deprecation warning in the old code.

Previous discussion

Linked PRs

Activity

  1. erlend-aasland commented on Jul 20, 2022

    @erlend-aasland
    ContributorAuthor

    If there is sufficient support for this feature, I'll create a PR.

  2. erlend-aasland commented on Jul 22, 2022

    @erlend-aasland
    ContributorAuthor

    If there is sufficient support for this feature, I'll create a PR.

    Two Three thumbs up; I'll create the PR 😎

  3. zware commented on Jul 22, 2022

    @zware
    Member

    I'd suggest to go full warnings._deprecated on it, with an error if the specified removal version has reached beta without the change.

  4. erlend-aasland commented on Jul 23, 2022

    @erlend-aasland
    ContributorAuthor

    I'd suggest to go full warnings._deprecated on it, with an error if the specified removal version has reached beta without the change.

    Maybe I'm misunderstanding you, but that would imply extending the new syntax with optional deprecated version and removed version fields, right?

  5. serhiy-storchaka commented on Jul 23, 2022

    @serhiy-storchaka
    Member

    I think it is great idea, although I would like to use something more distinguishable and searchable than just x.

    We should consider more general problem, when we want to change parameters in incompatible way.

    1. Turn positional-or-keyword parameters into keyword-only parameters. A directive which means "the following positional-or-keyword parameters will be keyword-only parameters in future".
    2. Turn positional-or-keyword parameters into positional-only parameters. A directive which means "the preceding positional-or-keyword parameters will be positional-only parameters in future".
    3. Remove parameter. For some converters we can use NULL or something like to distinguish absent argument, but for others all values after conversion are correct, so we need a special directive for this. When remove positional-only or positional-or-keyword parameter, all following parameters should become keyword-only parameters.
    4. 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.

    There may be other possible incompatible changes, but these four are common.

  6. erlend-aasland commented on Jul 23, 2022

    @erlend-aasland
    ContributorAuthor

    I think it is great idea, although I would like to use something more distinguishable and searchable than just x.

    Thanks for the support! I'm up for any other symbol; x was just the first thing that popped up in my head when I did the first proof-of-concept implementation. OTOH, * and / are not very searchable either.

    We should consider more general problem, when we want to change parameters in incompatible way. [...]

    There may be other possible incompatible changes, but these four are common.

    +1 However, I think we should create separate issues for each feature :)

  7. added this to IDLE Issues and removed this from IDLE Issueson Jul 23, 2022
  8. hauntsaninja commented on Dec 1, 2022

    @hauntsaninja
    Contributor

    Thanks for suggesting this! This would be nice, e.g. for #56166

    A quick bikeshed, do we need to use a new symbol? Something like the following seems pretty readable:

    posonlyarg
    /
    futureposonlyarg
    /-in-future
    posorkwarg
    *-in-3.12
    futurekwonlyarg
    *
    kwonlyarg
    

    Over here *-in-3.12 is a suggestion for how we could specify we want warnings._deprecated semantics.

  9. erlend-aasland commented on Dec 2, 2022

    @erlend-aasland
    ContributorAuthor

    do we need to use a new symbol?

    I don't know; I just picked that path, because it was very easy to implement as a proof of concept :)

    Regarding the proposed extended format: I'd replace the hyphens with single whitespace to make it more readable.

  10. erlend-aasland commented on Jan 2, 2023

    @erlend-aasland
    ContributorAuthor

    cc. @colorfulappl: this might be of interest to you.

  11. removed
    triagedThe issue has been accepted as valid by a triager.
    on Apr 29, 2023
  12. 18 remaining items

  13. added 6 commits that reference this issue on Aug 8, 2023
  14. added 2 commits that reference this issue on Aug 19, 2023
  15. serhiy-storchaka commented on Aug 21, 2023

    @serhiy-storchaka
    Member

    @erlend-aasland, do you mind to create issues for other two problems mentioned in #95065 (comment)? I could do them, but I am not sure about syntax.

  16. erlend-aasland commented on Aug 22, 2023

    @erlend-aasland
    ContributorAuthor

    @erlend-aasland, do you mind to create issues for other two problems mentioned in #95065 (comment)? I could do them, but I am not sure about syntax.

    I'll get to it right away.

  17. erlend-aasland commented on Aug 22, 2023

    @erlend-aasland
    ContributorAuthor

    I think we can close this now. The only thing missing is the preprocessor tests; I'm not sure it is worth it to add all the boilerplate code needed to get such tests up and running. If we feel it is needed, we can create a follow-up issue for that particular item, but I don't think it should keep blocking the resolution of this issue.

  18. erlend-aasland commented on Aug 22, 2023

    @erlend-aasland
    ContributorAuthor

    Thanks again, to everyone involved!

  19. added a commit that references this issue on Sep 13, 2023
  20. added a commit that references this issue on Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions