Add redundant-annotation warning
Relates to #18540
Adds a warning for redundant-annotation where a function local variable annotation type is the same as the inferred type
Based heavily on the code from @asottile in the issue
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅
When --disallow-any-generic is enabled, it requires an annotation for:
a = (None, [])
that conflicts with the warning from --warn-redundant-annotation
@JukkaL do you have opinions on how this interacts with (maybe) redefinition? I forgot what heuristics you've mentioned using for it before.
Testing this on an internal project that uses sphinx-autodoc-typehints, this branch warns on a redundant type hint that is meant for the sphinx generated documentation. An unfortunate downside
I'd say this check should only fire for function locals. Redundant annotations at top level (globals) and class members should be fine - they declare the public interface explicitly. If I say
STEPS_LIMIT: int = 3
at top level, I want it to mean "this is a max count of steps to perform, some integer, but make no assumptions about its value". It should not be reinterpreted as final or literal, for example, and the explicit type hint means "yes, really an int, it is not going to become a float later without semver-major update, but its value could change to 20 in a patch release".
This would also resolve your sphinx-autodoc-typehints issue, I believe?
I'd say this check should only fire for function locals.
Updated to this
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅