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 #3806

Closed
lucatrv opened this issue Mar 4, 2020 · 1 comment · Fixed by #3821
Closed

Issue with function to customize argparse formatter class #3806

lucatrv opened this issue Mar 4, 2020 · 1 comment · Fixed by #3821
Labels
stubs: false positive Type checkers report false errors

Comments

@lucatrv
Copy link

lucatrv commented Mar 4, 2020

I'm reopening here the issue originally opened for mypy, see reply.

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.

@srittau srittau added size-small stubs: false positive Type checkers report false errors labels Mar 4, 2020
@srittau
Copy link
Collaborator

srittau commented Mar 4, 2020

The formatter_class argument and field should be typed using a callable protocol. Its just called once like this:

        return self.formatter_class(prog=self.prog)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stubs: false positive Type checkers report false errors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants