Skip to content

ArgumentParser's add_argument returns the Action that was created. #1881

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

Merged
merged 3 commits into from
Mar 29, 2018

Conversation

EvanKrall
Copy link
Contributor

Fixes #1878

I added the member variables of Action to fix this error with mypy_selftest:

mypy/main.py:239: error: "Action" has no attribute "dest"

However, currently I'm getting:

mypy/main.py:240: note: unused 'type: ignore' comment

I suppose to fix this I would have to remove the type: ignore line in mypy (now that it's unnecessary), but it seems that would cause a chicken-and-egg problem: such a PR against mypy would fail tests until this PR is merged.

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Feb 16, 2018

Here's what I think we should do:

  1. Merge this PR (after reviewing it, which I haven't done yet)
  2. Make a PR to mypy that syncs typeshed and removes the now-unused type ignore
  3. Merge it

EvanKrall added a commit to EvanKrall/mypy that referenced this pull request Feb 16, 2018
@EvanKrall
Copy link
Contributor Author

Running typeshed's mypy_selftest.py against the PR'd version of both mypy and typeshed results in:

mypy/main.py:235: error: Argument "dest" to "add_argument" of "ArgumentParser" has incompatible type "Optional[str]"; expected "str"

It appears that parser.add_argument(..., dest=None) works fine:

Python 2.7.10 (default, Feb  7 2017, 00:08:15)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--blah', dest=None)
_StoreAction(option_strings=['--blah'], dest='blah', nargs=None, const=None, default=None, type=None, choices=None, help=None, metavar=None)

(same behavior in 3.6.1).

dest=None handled here:

I guess I'll also change dest to Optional[str].

Copy link
Member

@emmatyping emmatyping left a comment

Choose a reason for hiding this comment

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

Look forward to this and python/mypy#4578 :)

@JelleZijlstra JelleZijlstra merged commit 4563c5a into python:master Mar 29, 2018
msullivan pushed a commit to python/mypy that referenced this pull request Apr 4, 2018
yedpodtrzitko pushed a commit to yedpodtrzitko/typeshed that referenced this pull request Jan 23, 2019
…ython#1881)

* ArgumentParser's add_argument returns the Action that was created.

* Make add_argument's dest parameter Optional. _ArgumentGroup's add_argument should also return the Action.
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.

argparse.ArgumentParser.add_action should return Action, not None
3 participants