Skip to content

Conversation

@grayjk
Copy link

@grayjk grayjk commented Nov 14, 2025

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

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@A5rocks A5rocks left a 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]

Copy link
Collaborator

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] = 1
  • y: 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.

Copy link
Author

@grayjk grayjk Nov 16, 2025

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() 

Copy link
Collaborator

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...

Copy link
Collaborator

@A5rocks A5rocks Nov 18, 2025

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...

@github-actions
Copy link
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@grayjk
Copy link
Author

grayjk commented Nov 17, 2025

When --disallow-any-generic is enabled, it requires an annotation for:

a = (None, [])

that conflicts with the warning from --warn-redundant-annotation

@grayjk grayjk requested a review from A5rocks November 18, 2025 12:05
@A5rocks
Copy link
Collaborator

A5rocks commented Nov 18, 2025

@JukkaL do you have opinions on how this interacts with (maybe) redefinition? I forgot what heuristics you've mentioned using for it before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants