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

Issue with function to customize argparse formatter class #8487

Closed
lucatrv opened this issue Mar 4, 2020 · 5 comments
Closed

Issue with function to customize argparse formatter class #8487

lucatrv opened this issue Mar 4, 2020 · 5 comments

Comments

@lucatrv
Copy link

lucatrv commented Mar 4, 2020

I'm using mypy 0.761 with Python 3.8.2 on Windows.

The suggested way to customize the argparse formatter class to control text width is to use a lambda function, as follows:

formatter = lambda prog: argparse.HelpFormatter(prog, width=100)
parser = argparse.ArgumentParser(description=dummy_text, formatter_class=formatter)

but with that code mypy emits the following error:

541: error: Argument "formatter_class" to "ArgumentParser" has incompatible type "Callable[[Any], RawDescriptionHelpFormatter]"; expected "Type[HelpFormatter]"

I could not find any workaround, the only solution is to avoid a function and redefine the formatter class.

See example and discussion.

@hauntsaninja
Copy link
Collaborator

This issue should be moved to typeshed (which defines the signature for argparse.ArgumentParser that mypy uses). Given the discussion on BPO, seems reasonable to change the type of formatter_class to Callable[[str], HelpFormatter].

@lucatrv
Copy link
Author

lucatrv commented Mar 5, 2020

@hauntsaninja, @srittau, I have one question: if the formatter_class type is changed, then I guess it would not accept actual formatter classes anymore, so for instance this would not be accepted anymore?

formatter_class=argparse.RawDescriptionHelpFormatter

Se formatter_class documentation.

@srittau
Copy link
Contributor

srittau commented Mar 5, 2020

@lucatrv Actually, a class is a callable as well, with parameters equal to the parameters of it's __init__ method and an instance if itself as return value. It would still be accepted.

@srittau
Copy link
Contributor

srittau commented Mar 6, 2020

This has been fixed in python/typeshed#3821 by @hauntsaninja.

@chet-manley
Copy link

chet-manley commented Jun 2, 2022

I ran into this yesterday when subclassing an ArgumentParser. It does not seem that Mypy accepts a HelpFormatter subclass as the formatter_class argument. Here's a cut-down repro to show what's happening:

class ArgumentParser(argparse.ArgumentParser):
    def __init__( # pylint: disable=too-many-arguments
        self,
        ...
        formatter_class: Optional[Type[argparse.HelpFormatter]] = None,
        ...
    ) -> None:
        ...
        if formatter_class is None:
            formatter_class = argparse.RawTextHelpFormatter

        super().__init__(
            name, usage, description, epilog, parents,
            formatter_class, DEFAULT_PREFIX, fromfile_prefix_chars,
            argument_default, conflict_handler, False, allow_abbrev
        )

Produces: Argument 6 to "__init__" of "ArgumentParser" has incompatible type "Type[HelpFormatter]"; expected "_FormatterClass" [arg-type]

mgor added a commit to mgor/grizzly-cli that referenced this issue Feb 27, 2023
formater_class seems to be a typeshed bug, see python/mypy#8487 (comment)

we're just going to ignore it for now, since it works.
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

No branches or pull requests

5 participants