Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-126180: Undeprecate getopt and optparse #126186

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

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Oct 30, 2024

Copy link
Contributor

@willingc willingc left a comment

Choose a reason for hiding this comment

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

@serhiy-storchaka This looks good. Thanks for the detailed comments too.

@@ -1787,14 +1787,6 @@ New Deprecations
Check membership in :data:`~dis.hasarg` instead.
(Contributed by Irit Katriel in :gh:`109319`.)

* :mod:`getopt` and :mod:`optparse`:
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we typically remove from the What's New for the version introduced or do we annotate that it is being changed in 3.14?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is an interesting question. Obviously undeprecation should be retroactive. There is no sense in discouraging the use of getopt and optparse in 3.12 and 3.13. For 3.13, this may even be considered a bugfix.

But how to indicate this in What's New? If say nothing, then users will continue to think that getopt and optparse are deprecated. If write about this in 3.12 and 3.13, most users will not read this. So we should write something in 3.14, even if this change will be applied to 3.12 and 3.13.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't have a good answer to this. Perhaps @hugovk would like to weigh in as release manager.

Copy link
Member

Choose a reason for hiding this comment

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

@Yhg1s is the 3.13 RM so punting to him :)

However, optparse was deprecated back in 3.2, some 13 years ago, so for one of them, this isn't a sudden change in 3.13.

If we do undeprecate, perhaps adding a note to the existing "What's New in 3.13" entry to note it's future change?

In any case, I would also hold off merging this and allow some more time for the discussion, it's not yet been 24 hours:

https://discuss.python.org/t/getopt-and-optparse-vs-argparse/69618

Copy link
Contributor

Choose a reason for hiding this comment

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

In drafting my PR, I took the view that 3.13 is new enough that most of the people who will read the What's New haven't actually read it yet, and hence just changed this to a regular 3.13 What's New entry for optparse (getopt is reverting to the 3.12 status quo, so it only got mentioned in NEWS).

@serhiy-storchaka serhiy-storchaka force-pushed the undeprecate-getopt-optparse branch from 507bdfc to 097cba2 Compare October 30, 2024 15:16
@picnixz picnixz changed the title Undeprecate getopt and optparse gh-126180: Undeprecate getopt and optparse Oct 30, 2024
Co-authored-by: Carol Willing <[email protected]>
serhiy-storchaka and others added 3 commits October 31, 2024 07:34
@ncoghlan
Copy link
Contributor

ncoghlan commented Oct 31, 2024

I missed that @serhiy-storchaka had already started on a PR for this, so #126227 is an alternative take on the same change which goes a bit further than this one does in some respects, but not as far in others:

  • retains a stronger use argparse default position to avoid triggering analysis paralysis in beginners. Yes, argparse has genuine problems (which Serhiy has been valiantly attempting to resolve), but most people won't hit them, and if they do click is often going to be a better answer than raw optparse.
  • keeps the initial note in the getopt docs
  • adds an initial note to the optparse docs that goes into exactly where argparse runs into problems
  • adds "See Also" links for click and Typer to the optparse docs (I left them out of the argparse docs due to the analysis paralysis concern)
  • splits out a new "Command Line Implementation Libraries" chapter in the docs, with these 3 modules, getpass, and curses (I was tempted to move fileinput as well, but ended up leaving it alone and eventually gave in to that temptation)
  • Changes the 3.13 What's New to explicitly note the optparse undeprecation. I still dropped any mention of getopt at all, since this is a reversion to the 3.12 status quo where that module is concerned.

I like some of the other changes Serhiy has included here, so I'm going to include them to the other PR with a Co-Authored-By note.

I've also added the DO-NOT-MERGE label to both PRs, as I agree with @hugovk that we want to let the discussion on this play out for a while longer yet.

Copy link
Contributor

@ncoghlan ncoghlan left a comment

Choose a reason for hiding this comment

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

I'm definitely in favour of the general idea, but had drafted my own PR before I saw the notification that Serhiy had started this one. I prefer the overall structure of my PR (it splits out a new docs chapter for "Command Line Interface Libraries"), so I added several of Serhiy's content changes as Co-Authored-By additions over there, rather than suggesting my structural changes here.

@willingc
Copy link
Contributor

@serhiy-storchaka Are you cool with @ncoghlan's approach or do you want to work together to refine either one of the existing PRs?

@serhiy-storchaka
Copy link
Member Author

I am fine with either option, but I think that optparse should not be downplayed. For now, it is the safest option for beginners. argparse is too risky.

@ncoghlan
Copy link
Contributor

ncoghlan commented Oct 31, 2024

@serhiy-storchaka Perhaps we can separate the two concerns? I think there's a piece the two of us agree on, and a piece where we still disagree (at least for now):

  • (the bit I think at least the two of us agree on) the documented deprecations of getopt and optparse should be reverted, and replaced with guidance on times when optparse is unequivocally the better option (because argparse doesn't work properly for those use cases)
  • (the still controversial bit) the default argparse recommendation should be dropped entirely because there are some signature definitions that argparse will mishandle

There are lots of CLIs where the problem of argparse mishandling leading - characters (and any similar issues) simply won't come up, because those kinds of names don't occur in practice (e.g. very few people using leading underscores in file names, so if your only options with value parameters accept file names, it probably doesn't matter to you that argparse handles them strangely).

While there are places where it might make sense for people to use optparse directly, I think it's limited to the following:

  • the solution must be in the standard library (if that isn't the case, use click or Typer instead)
  • the solution must handle leading - with the more standard conventions implemented by optparse rather than the quirky behaviours exhibited by argparse

We've spent ~15 years of telling people to use argparse over optparse, and the GitHub code search results suggest that by and large people have either been happy with that recommendation, or else sought out third party solutions like click.

It might be reasonable to include those third party recommendations in the argparse docs (in addition to having them in the optparse docs), but I don't think we'd be genuinely helping people by directing them to the lower level optparse API unless they already knew what they were doing.

Edit: Serhiy clarified on the other PR that my assumption that mishandling leading - characters was the only problematic argparse quirk is incorrect. That does strengthen Serhiy's point that the underlying problem with argparse is that when it fails, it can often be because it is behaving unreasonably, not because it is being asked to do something unreasonable, leading beginners to think they're the ones at fault, when the problem is really with argparse's behaviour being quirky.

@serhiy-storchaka
Copy link
Member Author

Closing this in favor of @ncoghlan's #126227, although I disagree with continuing to recommend argparse even if we know about its flaws which were not fixed in past 15 years.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants