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

Improve message when an annotation is expected #6782

Merged

Conversation

@rafaelcaricio
Copy link
Contributor

commented May 6, 2019

Fixes #2463

Improve error message when partial types are found for built-in types Dict, List, Set, FrozenSet.

Messages used to be like:

main:3: error: Need type annotation for 'x'

This PR modifies it to look like:

main:3: error: Need type annotation for 'x' (hint: "x: List[<type>] = ...")

In case the variable is not of a built-in type the hints are not shown.

@JukkaL
Copy link
Collaborator

left a comment

Thanks for the PR! Looks mostly good, just a few minor comments.

type_dec = '<type>'
if alias == 'Dict':
type_dec = '{}, {}'.format(type_dec, type_dec)
if python_version < PYTHON3_VERSION:

This comment has been minimized.

Copy link
@JukkaL

JukkaL May 6, 2019

Collaborator

I'd prefer ... < (3, 6) here since PYTHON3_VERSION might get changed to (3, 7) in the future and the would then become incorrect.

@@ -133,7 +134,8 @@ def argument(self, ctx: 'mypy.plugin.ClassDefContext') -> Argument:
# assignment, which is where you would fix the issue.
node = self.info[self.name].node
assert node is not None
ctx.api.msg.need_annotation_for_var(node, self.context)
python_version: Tuple[int, int] = sys.version_info[:2]

This comment has been minimized.

Copy link
@JukkaL

JukkaL May 6, 2019

Collaborator

This is slightly questionable, since the target Python version could be different from the current Python version we are running on. If we can't easily get the target Python version, I'd prefer to either not provide a Python version at all (such as giving None as the version -- we'd omit the hint in this case), or to provide a constant Python version for consistency (say, (3, 7)).

rafaelcaricio added some commits May 6, 2019

@JukkaL

This comment has been minimized.

Copy link
Collaborator

commented May 6, 2019

The changes look good -- thanks for the updates! This is ready to merge if the builds are green.

@rafaelcaricio

This comment has been minimized.

Copy link
Contributor Author

commented May 6, 2019

@JukkaL Thank for the review! Glad to help.

@ilevkivskyi ilevkivskyi merged commit a202b42 into python:master May 6, 2019

2 checks passed

continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details

@rafaelcaricio rafaelcaricio deleted the rafaelcaricio:improve_error_msgs_with_hint branch May 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.