click icon indicating copy to clipboard operation
click copied to clipboard

Unable to properly inherit and hint Group due to use of Dict for parameters

Open altendky opened this issue 2 years ago • 0 comments

I am working on updating to Click 8 in https://github.com/Chia-Network/chia-blockchain/pull/11361 and wanted to make windows_expand_args=True default for all my uses and have a single place to apply any other adjustments I need at that level. I wrote a custom subclass of Group as below to achieve that, as well as a similar subclass of Command. The issue is that when I hint that only my _Command subclass should be passed mypy complains due to the variance of Dict. Modifying click.Group.__init__() to accept Mapping[str, Command] instead of Dict[str, Command] resolves the issue. I will submit a PR immediately following this.

chia/util/click.py:22: error: Argument "commands" to "__init__" of "Group" has incompatible type "Union[Dict[str, _Command], Sequence[_Command], None]"; expected "Union[Dict[str, Command], Sequence[Command], None]"  [arg-type]
class _Group(click.Group):
    def __init__(
        self,
        name: Optional[str] = None,
        commands: Optional[Union[Dict[str, _Command], Sequence[_Command]]] = None,
        **attrs: Any,
    ) -> None:
        # _commands = cast(Optional[Union[Dict[str, click.Command], Sequence[click.Command]]], commands)
        super().__init__(name=name, commands=commands, **attrs)
        self.command_class = _Command

    def main(
        self,
        args: Optional[Sequence[str]] = None,
        prog_name: Optional[str] = None,
        complete_var: Optional[str] = None,
        standalone_mode: bool = True,
        **extra: Any,
    ) -> Any:
        return super().main(
            args=args,
            prog_name=prog_name,
            complete_var=complete_var,
            standalone_mode=standalone_mode,
            windows_expand_args=True,
            **extra,
        )

Environment:

  • Python version: 3.10.1
  • Click version: 8.1.3

altendky avatar Apr 29 '22 15:04 altendky