python / python/typeshed

argparse type=bool

Open
#6,821 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stubs: false negative
Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

When adding an argument with argparse.ArgumentParser.add_argument, it is tempting to pass type=bool to create an option that is specified without a value, such as --enable-foo. This doesn't work, and in fact creates a subtle bug: --enable-foo silently swallows the next argument, so a command like python3 program.py --enable-foo x y z will work but only leave y and z to be used as other arguments. https://stackoverflow.com/a/15008806

To prevent this, I think the best we can do is to add an overload that returns NoReturn when you pass in type=bool.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the typeshed stub for argparse.ArgumentParser.add_argument and inspect its existing overloads. Review the linked explanation of type=bool swallowing the next argument, then determine how the requested NoReturn overload should fit the stub and add or update the relevant typing checks. Done means type=bool is rejected by the stub without affecting valid argument declarations.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.