-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
Type hinting ("typing" module) #107
Comments
Hi Jacob, Thank you for your interest and for the idea. I agree that we should support typing. Originally there was no convention on the usage of annotations, so I chose to tuck argument documentation there. It allowed to get rid of the Possible approach:
Obstacles:
|
Thanks for your input @neithere. I did not think of the annotation functionality of Python 3 and tbh I've never used it. Is there a way to combine both? I like the ability to idea of inlining the documentation also 😅. |
So, the library began its life when I had some free time in an airport. Today I had a similar time slot in the same place, so I decided to give argh+typing a try :) Unfortunately, when I started writing unit tests, I couldn't come up with anything meaningful. I kinda feel that it should make sense, but how? )) Do you have any hypothetical example that we could put into unit tests and try to make it work? Andy |
Where did this land in the last few years? I might have a look unless someone would suggest another library that does similar things? I have been quite happy with argh over the time I've been using it so might just try to kick this along. A typed example might be something like this:
|
I also found myself wanting this. Currently, if you have a function you want to
It would be a lot nicer IMO if you could just write
This would take the place of th documentation-type annotations argh currently supports. I can't think of a clean way to make them go together which wouldn't abuse the type hinting system, Maybe those are a better fit for a decorator?
I think I would be up for implementing this, if there is interest. |
So... This is definitely something that needs to go into Argh 1.0. Will bring the library to a whole new level of DRYness that was not technically achievable at the time when its first versions were created. Issue #144 adds more ideas which I like a lot: using So e.g. for
Mapping the typing hints to argparse argument declarations is not an easy task. There will be dubious cases. I think we need to start by drafting a list of possible mappings, from simple and obvious ones to complicated ones (with For example:
|
Here are some suggestions for two somewhat complex but (I think) useful type hints.
I'm a little unsure about what I'd expect to happen for |
* chore: cleanup (Mercurial files, etc.) - Mercurial: .hgtags and .hgignore (not used since migration to Git in 2013) - Travis config (not relevant since the adoption of Github Actions) - Unnecessary wrapper scripts * chore: drop outdated test-related stuff * chore: restructure dirs Also replace old complicated docs configs with minimal ones. * chore: replace setup.py with pyproject.toml * docs: remove/replace outdated text * chore: bump copy year in comments * refactor: simplify the code, drop Python 2 compat BREAKING CHANGE: Python 2.x is not supported any more. * docs: drop examples etc. about Python 2 * test: drop tests for outdated pythons * docs: fix config * chore: fix CI * chore: limit CI triggers * chore: fix CI * style: rearrange imports using isort * feat: drop support for Python 3.7 * chore: drop outdated code for unsupported Pythons * docs: specify version for deprecated args removal Some arguments were deprecated in 0.26 but no deadline was specified. This specifies the version in which they will be removed and adds tests to ensure that the deprecation warning is present. * feat: deprecate argument help as annotations Related to #107 * refactor: rename test methods * tests: fix for Python 3.11 Argparse in Python 3.11 has a check against duplicate subparser names * docs: fix config and cross-references * docs: fix path in Sphinx config for RTD * docs: clarified future breaking changes * docs: fix unresolved ref * chore: replace Poetry with Flit for PEP-621 Poetry will support PEP-621 at some point but Flit already does it and works well enough. * chore: pull info to Sphinx from pyproject.toml The core purpose is keeping `version` defined in just one place. * chore: use docs reqs from pyproject.toml Both for Tox and ReadTheDocs * chore: minor changes for tox + docs * style: use doc8, apply to docs * style: use isort, apply to code * chore: use pre-commit * style: use Black * chore: use yamllint * docs: update project URLs, clean empty line * chore: add optional dependency * style: use flake8 * chore(CI): use tox-gh-actions * chore(CI): include coverage, fail under 95% * docs: mention dropping of Py3.7 * docs: add notes on the list of contributors
Just a note: this can get tricky but also very interesting and useful if/when we decide to support overloading. Definitely not part of MVP though. |
FYI, I'm doing a significant revamp as part of #191 and keeping this in mind as the next step. Approximate roadmap, incremental and realistic:
|
Perhaps one of the best ways to replace Usage example: def load_dump(
path: Annotated[str, argh.Help("path to the dump file to load")],
format: Annotated[str, argh.Choices(FORMAT_CHOICES), argh.Help("dump file format")] = DEFAULT_CHOICE
) -> str:
... Accessed during parser assembly via |
#139 is a useful edge case example: |
FYI, basic support is almost ready, it will be added in 0.31 as planned. For now it will be enabled for any function which is not decorated with Upd.: supporting |
I take it "Goodbye decorators" and "Here’s what Argh is heading for (around 2024)." hasn't quite happened yet? https://argh.readthedocs.io/en/latest/the_story.html#goodbye-decorators I was looking at how to set the help test for an option... |
First of all, THANKS @neithere for this awesome project, it's never been easier to set up a command line tool!
Just wanted to check on you to see how you feel about supporting the new typing module? This way it would be possible to specify type on positional arguments without using the
@argh.arg
decorator.. This would makeargh
even more great i.m.o.The text was updated successfully, but these errors were encountered: