Skip to content

Add variable name to "Need type annotation for variable" error #4496

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 5 commits into from
Jan 21, 2018

Conversation

elliott-beach
Copy link
Contributor

@elliott-beach elliott-beach commented Jan 21, 2018

fixes #4431 by changing "need annotation for variable" messages to "need to annotation for 'x'".

Copy link
Member

@ilevkivskyi ilevkivskyi 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 looks straightforward. I just have few comments. Could you please also fix the lint failure (and never use tabs)?

mypy/messages.py Outdated
@@ -62,7 +62,7 @@
MUST_HAVE_NONE_RETURN_TYPE = 'The return type of "{}" must be None'
INVALID_TUPLE_INDEX_TYPE = 'Invalid tuple index type'
TUPLE_INDEX_OUT_OF_RANGE = 'Tuple index out of range'
NEED_ANNOTATION_FOR_VAR = 'Need type annotation for variable'
NEED_ANNOTATION_FOR_VAR = 'Need type annotation for variable \"{}\"'
Copy link
Member

Choose a reason for hiding this comment

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

This is now never used, IIUC and should be removed.

mypy/messages.py Outdated
@@ -962,6 +962,9 @@ def unimported_type_becomes_any(self, prefix: str, typ: Type, ctx: Context) -> N
self.fail("{} becomes {} due to an unfollowed import".format(prefix, self.format(typ)),
ctx)

def need_annotation_for_var(self, node: SymbolNode, context: Context) -> None:
self.fail("Need type annotation for \'{}\'".format(node.name()), context)
Copy link
Member

Choose a reason for hiding this comment

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

You don't need to escape quotes here.

==
main:5: error: Need type annotation for variable
main:5: error: Need type annotation for 'x'
Copy link
Member

Choose a reason for hiding this comment

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

I think you should also add dedicated tests for more tricky situations like:

x, y = [], []
self.a, self.b = None, None

(and maybe some others if you have more ideas).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a case for multiple assignment. I think classes would be a valid case too but currently mypy doesn't appear to require annotations for self assignments. Other cases I can't think of. My understanding is the only assignments that can trigger this are assignments with variable names literal as lvalue, which is covered.

@elliott-beach
Copy link
Contributor Author

elliott-beach commented Jan 21, 2018

Sorry, I will lint before pushing next time. I had an interesting configuration issue with PyCharm, causing the tabs, which I just resolved.

@@ -1787,3 +1787,8 @@ reveal_type(a.__pow__(2)) # E: Revealed type is 'builtins.int'
reveal_type(a.__pow__(a)) # E: Revealed type is 'Any'
a.__pow__() # E: Too few arguments for "__pow__" of "int"
[builtins fixtures/ops.pyi]

[case testTypeAnnotationNeededMultipleAssignment]
x,y = [],[] # E: Need type annotation for 'x' \
Copy link
Member

Choose a reason for hiding this comment

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

I know this is just a test but why not use PEP 8 rules here? (space after comma)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes yes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

because I need to read PEP 8 :P

Copy link
Member

@ilevkivskyi ilevkivskyi left a comment

Choose a reason for hiding this comment

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

LGTM now.

@ilevkivskyi ilevkivskyi merged commit 86f4e01 into python:master Jan 21, 2018
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.

Add variable name to "Need type annotation for variable" error
2 participants