Comments (4)
I would like to second this. With the current implementation I am not sure how this is supposed to work, but in the example you provide, the code to access args.foo or args.bar gives a type exception (VS Code/pylance). Which makes sense since they are defined in separate classes.
What I expected:
from tap import Tap
class MainParser(Tap):
shared: str
def configure(self) -> None:
self.add_subparser('a', SubParserA)
self.add_subparser('b', SubParserB)
class SubParserA(MainParser):
foo: str
class SubParserB(MainParser):
bar: str
if __name__ == '__main__':
args = MainParser().parse_args()
if isinstance(args, SubParserA):
print(args.shared)
print(args.foo)
That code does not show any errors in the editor, but when run goes into an infinite loop which seems to involve _get_class_variables and _get_from_self_and_super. And without the subclasses, the "isinstance" part will never be true. Is there a trick for that?
As an aside, it would be nice to allow "None" as the subparse class, for subparsers without additional arguments. I created a NoArgs object instead, so maybe that's good enough, but maybe include as an example?
from typed-argument-parser.
Hi @JasonGilholme,
Thank you for raising this issue! We agree that the current behavior is not ideal. We've already put some thought into this problem #17 (comment), but we're still thinking about it.
For future us and perhaps others, we'll leave some runnable code that breaks (heavily inspired by your code) when you run python file.py a --foo biz
:
from tap import Tap
class SubParserA(Tap):
foo: str
def do_foo(self):
pass
class SubParserB(Tap):
bar: str
def do_bar(self):
pass
class MainParser(Tap):
def configure(self) -> None:
self.add_subparsers()
self.add_subparser('a', SubParserA)
self.add_subparser('b', SubParserB)
if __name__ == '__main__':
main_parser = MainParser()
args = main_parser.parse_args()
args.do_foo()
Best,
Jesse and Kyle
from typed-argument-parser.
Hi all,
We’re working on sketching a plan for a Tap version 2 release. We have a sketch of the solution to a number of issues including this one. This includes a breaking change to the treatment of subparsers and a different way that we treat comments to support argument groups. We’re excited to start the implementation and would love feedback and recommendations from everyone. We’re so appreciative of all of the wonderful pull requests, issues, and ideas from all of you!
Best,
Kyle and Jesse
from typed-argument-parser.
See my comments at #69 (comment). Thanks!
from typed-argument-parser.
Related Issues (20)
- Add useful type hints to `Tap.add_subparser` HOT 5
- Refactor `tapify` to enable subparsers HOT 1
- Type safe way to access subparser arguments? HOT 2
- Fix `tapify` to correctly handle **kwargs HOT 1
- [Suggestion] Allow to reference previous arguments as default values HOT 1
- Allow for multi-line documentation of the function HOT 1
- Tuple parsing with literals HOT 1
- [help] Adding `version` arg to print version but bypassing required args HOT 3
- tapify help string order is random HOT 1
- More convenient syntax for subparsers
- ImportError: cannot import name 'Tap' from 'tap' HOT 3
- What's the best way to create a parser from a Pydantic model? HOT 3
- Human readable JSON for saved Python object HOT 1
- Add support for using the `Annotated` type to provide comments for the help string HOT 1
- Tapify, docstrings and typing.NamedTuples
- [suggestion] Class variable arguments with multiline defaults do not respect help comments HOT 1
- add_argument should not overwrite properties defined in class member. HOT 1
- Versions should not be compared as strings HOT 1
- Tap eats my custom error messages :( HOT 4
- [Feature Request] Add new config file types HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typed-argument-parser.