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

Changed with_argparser() and as_subcommand_to() to accept either an A… #1278

Merged
merged 1 commit into from
Oct 30, 2023

Conversation

anselor
Copy link
Contributor

@anselor anselor commented Sep 26, 2023

…rgumentParser or a

factory callable that returns an ArgumentParser.
Changed Cmd constructor to construct an instance-specific ArgumentParser using either the factory callable or by deep-copying the provided ArgumentParser.
With this change a new argparse instance should be created for each instance of Cmd.

Addresses #1002

@codecov
Copy link

codecov bot commented Sep 26, 2023

Codecov Report

All modified lines are covered by tests ✅

Comparison is base (9886b82) 98.55% compared to head (469876b) 98.56%.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1278   +/-   ##
=======================================
  Coverage   98.55%   98.56%           
=======================================
  Files          22       22           
  Lines        5729     5768   +39     
=======================================
+ Hits         5646     5685   +39     
  Misses         83       83           
Files Coverage Δ
cmd2/cmd2.py 98.28% <100.00%> (+0.02%) ⬆️
cmd2/decorators.py 100.00% <100.00%> (ø)

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@anselor anselor force-pushed the with_argparser_factory branch 3 times, most recently from 039f92b to e68867d Compare September 26, 2023 21:24
cmd2/cmd2.py Outdated
@@ -659,6 +668,43 @@ def register_command_set(self, cmdset: CommandSet) -> None:
cmdset.on_unregistered()
raise

def _build_parser(self, parent: Union['Cmd', CommandSet], parser_builder: Any) -> Optional[argparse.ArgumentParser]:
Copy link
Contributor

@federicoemartinez federicoemartinez Sep 27, 2023

Choose a reason for hiding this comment

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

parser_builder shouldn't be Optional[Union[argparse.ArgumentParser, Callable]] ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the point is to always result in an instance of a parser after this. either by deep-copying the provided one or calling the builder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh wait, misread your comment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, this got messy because staticmethod and classmethod are not considered callable for some reason and there's some complicated mess where mypy wants to treat them like a generic type but at runtime this causes a python parsing error and fails..

@anselor anselor force-pushed the with_argparser_factory branch 2 times, most recently from 3dec00d to 744311b Compare September 27, 2023 16:46
…rgumentParser or a

factory callable that returns an ArgumentParser.
Changed Cmd constructor to construct an instance-specific ArgumentParser using either the factory callable
or by deep-copying the provided ArgumentParser.
With this change a new argparse instance should be created for each instance of Cmd.

Addresses #1002
@anselor anselor force-pushed the with_argparser_factory branch from 744311b to 469876b Compare September 27, 2023 19:16
@anselor anselor merged commit 8d88c35 into master Oct 30, 2023
42 checks passed
@anselor anselor deleted the with_argparser_factory branch October 30, 2023 22:22
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.

3 participants