Code Monkey home page Code Monkey logo

Comments (4)

pdc1 avatar pdc1 commented on June 10, 2024 1

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.

martinjm97 avatar martinjm97 commented on June 10, 2024

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.

swansonk14 avatar swansonk14 commented on June 10, 2024

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.

pdc1 avatar pdc1 commented on June 10, 2024

See my comments at #69 (comment). Thanks!

from typed-argument-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.