-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
report warn-redundant-annotation (similar to warn-redundant-cast) #18540
Comments
Great feature! I always wanted something like this, PR is very welcome. Feel free to ping me on it. I would suggest using |
updated my patch a little bit after playing around with diff --git a/mypy/checker.py b/mypy/checker.py
index c69b80a55..9cd8bc60a 100644
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -3077,6 +3077,16 @@ class TypeChecker(NodeVisitor[None], CheckerPluginInterface):
Handle all kinds of assignment statements (simple, indexed, multiple).
"""
+ if (
+ not s.is_final_def
+ and not s.is_alias_def
+ and s.unanalyzed_type is not None
+ and s.type is not None
+ and not is_same_type(s.type, AnyType(TypeOfAny.special_form))
+ and is_same_type(s.type, self.expr_checker.accept(s.rvalue))
+ ):
+ self.msg.redundant_annotation(s.type, s.rvalue)
+
# Avoid type checking type aliases in stubs to avoid false
# positives about modern type syntax available in stubs such
# as X | Y.
and I'm slightly worried that this idea is dead-in-the-water. on the one hand, it points out some pretty nice fixes such as: Line 32 in f44a60d
on the other hand it also points out some stuff that might be there intentionally to prevent a typo such as: Line 34 in f44a60d
(I also would need to do some special casing for thoughts? |
Maybe we can start small maybe, for non-generic types only? 🤔 |
Feature
I frequently see people new to python typing over typing everything -- think
x: int = function_that_obviously_returns_int()
. I would like this to be an optional errorPitch
in most cases it's somewhat harmless -- it adds a little bit of extra reading and occasionally a slight amount of runtime overhead. but in some cases it makes refactoring and typing a burdensome task (especially if the manual annotation was wrong or subtly wrong, or slightly incorrect, or correct at the time but inconsistent after a refactor).
I would like to suggest a new feature which allows mypy to report this as an optional diagnostic when someone adds an inline annotation which is unnecessary (when mypy already knows the rvalue's type given its expression).
I initially attempted to write something like this as a mypy plugin but I don't think the plugin interface actually gives enough interface points to make this diagnostic possible so I instead looked at how much work it would be to implement this in mypy directly. at a first pass it seems to be not that difficult!
with an example file like this:
my hacked version of mypy produces the desired diagnostic (though I didn't do the part where it's disabled by default and opted into but I just wanted to show a proof of concept!):
The text was updated successfully, but these errors were encountered: