-
Notifications
You must be signed in to change notification settings - Fork 0
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
Updated get_group and get_group_from_info to return a Click Group object #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -213,7 +213,7 @@ def __call__(self) -> Any: | |
return get_command(self)() | ||
|
||
|
||
def get_group(typer_instance: Typer) -> click.Command: | ||
def get_group(typer_instance: Typer) -> click.Group: | ||
group = get_group_from_info(TyperInfo(typer_instance)) | ||
return group | ||
|
||
|
@@ -228,11 +228,11 @@ def get_command(typer_instance: Typer) -> click.Command: | |
or len(typer_instance.registered_commands) > 1 | ||
): | ||
# Create a Group | ||
click_command = get_group(typer_instance) | ||
click_group = get_group(typer_instance) | ||
if typer_instance._add_completion: | ||
click_command.params.append(click_install_param) | ||
click_command.params.append(click_show_param) | ||
return click_command | ||
click_group.params.append(click_install_param) | ||
click_group.params.append(click_show_param) | ||
return click_group | ||
elif len(typer_instance.registered_commands) == 1: | ||
# Create a single Command | ||
click_command = get_command_from_info(typer_instance.registered_commands[0]) | ||
|
@@ -339,7 +339,7 @@ def solve_typer_info_defaults(typer_info: TyperInfo) -> TyperInfo: | |
return TyperInfo(**values) | ||
|
||
|
||
def get_group_from_info(group_info: TyperInfo) -> click.Command: | ||
def get_group_from_info(group_info: TyperInfo) -> click.Group: | ||
assert ( | ||
group_info.typer_instance | ||
), "A Typer instance is needed to generate a Click Group" | ||
|
@@ -356,7 +356,7 @@ def get_group_from_info(group_info: TyperInfo) -> click.Command: | |
convertors, | ||
context_param_name, | ||
) = get_params_convertors_ctx_param_name_from_function(solved_info.callback) | ||
cls = solved_info.cls or click.Group | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So from the Click docs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure of this one (don't know enough about the Click internals). |
||
cls = click.Group | ||
group = cls( # type: ignore | ||
name=solved_info.name or "", | ||
commands=commands, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed from
click_command
toclick_group
mainly because otherwise it would returnIncompatible types in assignment (expression has type "Command", variable has type "Group")
. Seems to be a scope issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a type error! My understanding is that Group is more specific than Command, and this function returns a Command. So you'd probably want to cast the result of get_group.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@juped Thank you.
Do you suggest making so that
main.get_group
returns aclick.Group
(I believe this is a requirement formypy
to run successfully)? By looking at the example code in the issue page, the user was callingget_group
from their script, so I'm assuming formypy
to run successfully we needmain.get_group
to return aclick.Group
?If yes, then I'm trying to figure out how to cast the result of
main.get_group_from_info
(which is whatmain.get_group
is calling) fromclick.Command
toclick.Group
. Is this how you do it in Python:Thank you!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is at the type hinting level, where I believe it's
cast()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried using
return cast(click.Group, group)
inmain.get_group
(this requires an import ofcast
from thetyping
module), like this:However when I run
mypy
on the sample script in the issue I getwhich isn't that helpful as line 16 is just a series of imports. Mmm...