Improve error message for overload overrides w/ swapped variants#5369
Merged
ilevkivskyi merged 1 commit intopython:masterfrom Aug 2, 2018
Merged
Conversation
This commit is intended to resolve python#1270. Currently, running mypy on the following program: from typing import overload class Parent: @overload def foo(self, x: A) -> A: ... @overload def foo(self, x: B) -> B: ... class Child(Parent): @overload def foo(self, x: B) -> B: ... @overload def foo(self, x: A) -> A: ... ...will make mypy report the following error -- it considers the fact that we've swapped the order of the two variants to be unsafe: test.pyi:10: error: Signature of "foo" incompatible with supertype "Parent" This error message can be confusing for some users who may not be aware that the order in which your overloads are defined can sometimes matter/is something mypy relies on. This commit modifies the error message to the following to try and make this more clear: test.pyi:10: error: Signature of "foo" incompatible with supertype "Parent" test.pyi:10: note: Overload variants must be defined in the same order as they are in "Parent"
Collaborator
Author
|
As a misc note, one idea the issue I linked to above mentioned was that we could maybe print out the signature of the parent method. I decided not to do this for now because it felt inconsistent: we don't do this in other cases where the user incorrectly overrides a method. If printing out the parent signature seems like a helpful thing to do in general, I'm happy to make a new PR w/ that change. |
ilevkivskyi
approved these changes
Aug 2, 2018
Member
ilevkivskyi
left a comment
There was a problem hiding this comment.
Thanks! This additional note makes sense. Also I think it is right to only show in in obvious cases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit is intended to resolve #1270.
Currently, running mypy on the following program:
...will make mypy report the following error -- it considers the fact that we've swapped the order of the two variants to be unsafe:
This error message can be confusing for some users who may not be aware that the order in which your overloads are defined can sometimes matter/is something mypy relies on.
This commit modifies the error message to the following to try
and make this more clear: