Skip to content

Improve suggestion when using any as type #12185

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

Merged
merged 2 commits into from
Feb 16, 2022
Merged

Improve suggestion when using any as type #12185

merged 2 commits into from
Feb 16, 2022

Conversation

siiptuo
Copy link
Contributor

@siiptuo siiptuo commented Feb 15, 2022

Description

Using any instead of Any is an easy mistake to make because "any" has been used as a type in docstrings, editors may highlight it as it's the name for the built-in function and at least TypeScript uses it as a keyword. Here are some examples of this mistake in real-world Python code.

For example, for the following code:

def a(b: any): pass

mypy currently outputs:

any.py:1: error: Function "builtins.any" is not valid as a type
any.py:1: note: Perhaps you need "Callable[...]" or a callback protocol?

I think that the given suggestion is misleading and could be improved. This pull request changes the output to:

any.py:1: error: Function "builtins.any" is not valid as a type
any.py:1: note: Perhaps you meant "Any" instead of "any"?

Test Plan

Added a unit test to verify the new output.

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

Thanks, good idea!

@github-actions

This comment has been minimized.

Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Thanks, this is great! One nit: maybe the error message should suggest typing.Any? I could imagine some new user naively going any -> Any without the import.

Would you be interested in making a similar PR for callable? :-)

@siiptuo
Copy link
Contributor Author

siiptuo commented Feb 16, 2022

@hauntsaninja

Thanks, this is great! One nit: maybe the error message should suggest typing.Any? I could imagine some new user naively going any -> Any without the import.

Good idea! I've updated the output.

Would you be interested in making a similar PR for callable? :-)

I can open a similar PR after this one.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@hauntsaninja hauntsaninja merged commit 554606b into python:master Feb 16, 2022
@hauntsaninja
Copy link
Collaborator

Thank you!

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