-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add redundant-annotation warning #20238
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
base: master
Are you sure you want to change the base?
Conversation
Relates to python#18540
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
A5rocks
left a comment
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.
I'm not completely sure this feature is fine. I thought it was, but when bringing it up in conversation, others pointed out that redefinition might depend on whether there's an annotation. Obviously mypy doesn't implement that algorithm now, but it might be nice to eventually have it? Or at least have the possibility of having it? And this feature would mean we can't.
To be concrete, I'm referring to how pyright/ty assume a declaration with a type (a: int = 5) can't be redefined but a declaration without (a = 5) can.
| class f: | ||
| g: int = 1 | ||
| [builtins fixtures/tuple.pyi] | ||
|
|
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.
Could you also test:
x: Literal[1] = 1y: list[str] = []- this:
from typing import TypeVar
def f(x: T) -> T:
return x
x: Literal[1] = f(1)
y: list[str] = f([])And uh, I guess it would be nice to test a case where running type checking without type context would error. (you may need to silence errors for a specific run?)... unfortunately I cannot think of any examples.
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.
And this feature would mean we can't.
If that redefinition algorithm is implemented, couldn't this flag be mutually exclusive with the redefinition flag?
Could you also test:
Tests added (commit ebf40a5). In the TypeVar test, it looks like is_same_type does not handle this. May be related to #19761
test a case where running type checking without type context would error
Does that mean a test like:
# mypy: check-untyped-defs
def f():
return 4
def g():
j: int = f() 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.
If that redefinition algorithm is implemented, couldn't this flag be mutually exclusive with the redefinition flag?
Yeah, potentially. I don't think that would be good UX...
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.
Does that mean a test like:
No, there's been issues like #20013 where part of the issue is that we're running type inference twice (once without type context for heuristics reasons, and once with) and the one without type context is erroring.
I can't think of any small reproducer because it would be a bug in mypy I think...
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
When a = (None, [])that conflicts with the warning from |
|
@JukkaL do you have opinions on how this interacts with (maybe) redefinition? I forgot what heuristics you've mentioned using for it before. |
Relates to #18540
Adds a warning for redundant-annotation where the annotation type is the same as the inferred type
Based heavily on the code from @asottile in the issue
This MR does not warn on ClassVar, dataclass, or NamedTuple