Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
grayjk wants to merge7 commits intopython:master
base:master
Choose a base branch
Loading
fromgrayjk:warn-redundant-annotation

Conversation

@grayjk
Copy link
Contributor

@grayjkgrayjk commentedNov 14, 2025
edited
Loading

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

asottile and Apakottur reacted with heart emoji
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@A5rocksA5rocks 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 algorithmnow, 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:
fromtypingimportTypeVardeff(x:T)->T:returnxx: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
ContributorAuthor

@grayjkgrayjkNov 16, 2025
edited
Loading

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 (commitebf40a5). 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-defsdeff():return4defg():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

@A5rocksA5rocksNov 18, 2025
edited
Loading

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

This comment has been minimized.

@grayjk
Copy link
ContributorAuthor

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

a= (None, [])

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

@grayjkgrayjk requested a review fromA5rocksNovember 18, 2025 12:05
@A5rocks
Copy link
Collaborator

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

@grayjk
Copy link
ContributorAuthor

Testing this on an internal project that usessphinx-autodoc-typehints, this branch warns on a redundant type hint that is meant for the sphinx generated documentation. An unfortunate downside

@sterliakov
Copy link
Collaborator

sterliakov commentedNov 24, 2025
edited
Loading

I'd say this check shouldonly 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?

@grayjk
Copy link
ContributorAuthor

I'd say this check should only fire for function locals.

Updated to this

@github-actions
Copy link
Contributor

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

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

Reviewers

@A5rocksA5rocksAwaiting requested review from A5rocks

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@grayjk@A5rocks@sterliakov

[8]ページ先頭

©2009-2025 Movatter.jp