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

MyPy/typing stubs for docopt. #334

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

cjwelborn
Copy link

I have some mypy/typing stubs here for Docopt. Would this be something that Docopt is interested in doing? I can always use these locally, but I wanted to at least ask if you were open to the idea of adding this stub file to Docopt. It doesn't add any dependencies or anything, or change the way Docopt behaves. It's an opt-in feature that only benefits people that want to use it (see mypy and pep-484).

There is another possible route for people that want to type check with the Docopt module, which is typeshed. They can be added there instead of this repo, but it still requires consent from the Docopt team.

This would catch type errors such as this:

mypy -c "
> from docopt import docopt
> usage = '''usage:
>     myscript
> '''
> argd = docopt(usage, version=True)"

<string>:6: error: Argument 2 to "docopt" has incompatible type "bool"; expected "str"

The stubs would need to be updated when function/method signatures change, or new ones are added. I wouldn't mind chipping in if no one else wants to.

I've tried to be as precise as possible with the types, and I've been running it against test_docopt.py. The tests use more of the docopt module than most projects I think. So far mypy doesn't generate any warnings/errors. You can check it out yourself in this branch:

# Using --verbose to make sure docopt.pyi is being parsed,
# and --silent-imports to quiet a warning about pytest's missing stubs.
# test_docopt.py doesn't have any stubs (probably not needed),
# so I'm telling mypy to check the function bodies anyway (against docopt.pyi).
mypy -s -v --check-untyped-defs test_docopt.py

What do you think? Is it too much for a simple library like Docopt?

@itdaniher
Copy link

Merged at bazaar-projects#1, thanks!

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.

None yet

2 participants