-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Mismatch of inferred type and return type note #3428
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Did a quick review pass.
mypy/checker.py
Outdated
|
||
def make_inferred_type_mismatch_note(msg: str, context: Context, subtype: Type, | ||
supertype: Type, supertype_str: str) -> str: | ||
'''The user does not expect an error if the inferred container type is the same as the return |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: Use """"
for docstrings, and format docstrings like this:
"""Short one-line summary.
Longer descriptions that may span multiple
lines.
"""
mypy/checker.py
Outdated
return type. This note suggests that they add a type annotation with the return type instead | ||
of relying on the inferred type. | ||
''' | ||
if (msg.startswith(messages.INCOMPATIBLE_RETURN_VALUE_TYPE) and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: Use indentation like this (double indent for second and later lines of the condition, and only one condition per line):
if (something and
another and
more):
whatever()
mypy/checker.py
Outdated
|
||
|
||
def make_inferred_type_mismatch_note(msg: str, context: Context, subtype: Type, | ||
supertype: Type, supertype_str: str) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this should be a method in messages.py
for consistency.
test-data/unit/check-functions.test
Outdated
from typing import Union, Dict, List | ||
def f() -> List[Union[int, str]]: | ||
x = ['a'] | ||
return x # E: Incompatible return value type (got List[str], expected List[Union[int, str]]) # N: Perhaps a type annotation is missing here? Suggestion: List[Union[int, str]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use \
to split the long lines so that # N: ...
is on a separate line. This makes the test cases easier to read.
mypy/checker.py
Outdated
for subtype_arg, supertype_arg in zip(subtype.args, supertype.args): | ||
if not is_subtype(subtype_arg, supertype_arg): | ||
return '' | ||
return 'Perhaps a type annotation is missing here? Suggestion: ' + supertype_str |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be better to also mention the name of the variable to make it clear that the proposed annotation is for the variable, not for the return statement. For example, Perhaps you need a type annotation for "x"? Suggestion: List[float]
Made all of the fixes but moving it to messages.py Since I use the I will try to figure out how to fix this, but might need some ideas. |
If you really need this, then you maybe can import |
Thanks, that did it. |
test-data/unit/check-functions.test
Outdated
def i() -> List[Union[int, float]]: | ||
x = [1] # Type: List[int] | ||
return x # E: Incompatible return value type (got List[int], expected List[Union[int, float]]) \ | ||
# N: Perhaps you need a type annotation for "x"? Suggestion: List[Union[int, float]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost there! I'd suggest adding test cases that cover cases that are mostly like these but that don't actually trigger the new message (i.e. one of the conditions in make_inferred_type_note
is false).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One more thing: Can you split the test case into multiple separate test cases? It's getting pretty big now. At least cases where we generate the note and where we don't generate a note could live in separate test cases.
Added more test cases and separated test cases. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me now.
* master: (23 commits) Make return type of open() more precise (python#3477) Add test cases that delete a file during incremental checking (python#3461) Parse each format-string component separately (python#3390) Don't warn about returning Any if it is a proper subtype of the return type (python#3473) Add __setattr__ support (python#3451) Remove bundled lib-typing (python#3337) Move version of extensions to post-release (python#3348) Fix None slice bounds with strict-optional (python#3445) Allow NewType subclassing NewType. (python#3465) Add console scripts (python#3074) Fix 'variance' label. Change label for variance section to just 'variance' (python#3429) Better error message for invalid package names passed to mypy (python#3447) Fix last character cut in html-report if file does not end with newline (python#3466) Print pytest output as it happens (python#3463) Add mypy roadmap (python#3460) Add flag to avoid interpreting arguments with a default of None as Optional (python#3248) Add type checking plugin support for functions (python#3299) Mismatch of inferred type and return type note (python#3428) Sync typeshed (python#3449) ...
Fixes #2529
Not sure about this function sitting in
checker.py
, might need to move it tomessages.py
but didn't look into that yet.I use the
context.expr.node.is_inferred
to determine if the type of the variable is inferred to determine if the message is printed. Apparentlyis_inferred
is stillTrue
even when the adding a type comment in the code. See below:Specifically, the
context.expr.node.is_inferred=True
forx
when I would expect it to beFalse
.