Skip to content

[red-knot] Fix more [redundant-cast] false positives - #17170

Merged
AlexWaygood merged 1 commit into
mainfrom
alex/cast-todos
Apr 3, 2025
Merged

AlexWaygood merged 1 commit into
mainfrom
alex/cast-todos

Conversation

@AlexWaygood

Copy link
Copy Markdown
Member

Fixes #17164. Simply checking whether one type is gradually equivalent to another is too simplistic here: Any is gradually equivalent to Todo, but we should permit users to cast from Todo or Unknown to Any without complaining about it. This changes our logic so that we only complain about redundant casts if:

  • the two types are exactly equal (when normalized) OR they are equivalent (we'll still complain about Any -> Any casts, and about Any | str | int -> str | int | Any casts, since their normalized forms are exactly equal, even though the type is not fully static -- and therefore does not participate in equivalence relations)
  • AND the casted type does not contain Todo

@AlexWaygood AlexWaygood added the ty The ty type checker label Apr 3, 2025
@github-actions

github-actions Bot commented Apr 3, 2025 •

Copy link
Copy Markdown
Contributor

mypy_primer results

Changes were detected when running on open source projects
scrapy (https://github.com/scrapy/scrapy)
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/settings/__init__.py:511:22: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/core/scheduler.py:369:20: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/middleware.py:120:19: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/middleware.py:126:19: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/extensions/httpcache.py:310:16: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/extensions/httpcache.py:392:20: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/extensions/feedexport.py:665:13: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/pipelines/media.py:171:15: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/pipelines/files.py:202:16: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/pipelines/files.py:319:16: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/pipelines/files.py:411:16: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/core/spidermw.py:166:28: Value is already of type `@Todo(generics)`
- warning[lint:redundant-cast] /tmp/mypy_primer/projects/scrapy/scrapy/core/spidermw.py:288:13: Value is already of type `@Todo(generics)`
- Found 1501 diagnostics
+ Found 1488 diagnostics

@sharkdp sharkdp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@AlexWaygood
AlexWaygood merged commit ca0cce3 into main Apr 3, 2025
@AlexWaygood
AlexWaygood deleted the alex/cast-todos branch April 3, 2025 14:00
dcreager added a commit that referenced this pull request Apr 3, 2025
* origin/main:
  [red-knot] Fix more [redundant-cast] false positives (#17170)
  [red-knot] Three-argument type-calls take 'str' as the first argument (#17168)
  Control flow: `return` and `raise` (#17121)
  Bump 0.11.3 (#17173)
  [red-knot] Improve `Debug` implementation for `semantic_index::SymbolTable` (#17172)
  [red-knot] Fix `str(…)` calls (#17163)
  [red-knot] visibility_constraint analysis for match cases (#17077)
  [red-knot] Fix playground crashes when diagnostics are stale (#17165)
dcreager added a commit that referenced this pull request Apr 3, 2025
* origin/main: (82 commits)
  [red-knot] Fix more [redundant-cast] false positives (#17170)
  [red-knot] Three-argument type-calls take 'str' as the first argument (#17168)
  Control flow: `return` and `raise` (#17121)
  Bump 0.11.3 (#17173)
  [red-knot] Improve `Debug` implementation for `semantic_index::SymbolTable` (#17172)
  [red-knot] Fix `str(…)` calls (#17163)
  [red-knot] visibility_constraint analysis for match cases (#17077)
  [red-knot] Fix playground crashes when diagnostics are stale (#17165)
  [red-knot] Callable types are disjoint from literals (#17160)
  [red-knot] Fix inference for `pow` between two literal integers (#17161)
  [red-knot] Add GitHub PR annotations when mdtests fail in CI (#17150)
  [red-knot] Fix equivalence of differently ordered unions that contain `Callable` types (#17145)
  [red-knot] Add initial set of tests for unreachable code (#17159)
  [`airflow`] Move `AIR302` to `AIR301` and `AIR303` to `AIR302` (#17151)
  ruff_db: simplify lifetimes on `DiagnosticDisplay`
  [red-knot] Detect division-by-zero in unions and intersections (#17157)
  [`airflow`] Add autofix infrastructure to `AIR302` name checks (#16965)
  [`flake8-bandit`] Mark `str` and `list[str]` literals as trusted input (`S603`) (#17136)
  [`airflow`] Add autofix for `AIR302` attribute checks (#16977)
  [`airflow`] Extend `AIR302` with additional symbols (#17085)
  ...
maxmynter pushed a commit to maxmynter/ruff that referenced this pull request Apr 3, 2025
Fixes astral-sh#17164. Simply checking whether one type is gradually equivalent
to another is too simplistic here: `Any` is gradually equivalent to
`Todo`, but we should permit users to cast from `Todo` or `Unknown` to
`Any` without complaining about it. This changes our logic so that we
only complain about redundant casts if:
- the two types are exactly equal (when normalized) OR they are
equivalent (we'll still complain about `Any -> Any` casts, and about
`Any | str | int` -> `str | int | Any` casts, since their normalized
forms are exactly equal, even though the type is not fully static -- and
therefore does not participate in equivalence relations)
- AND the casted type does not contain `Todo`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty The ty type checker

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[red-knot] Redudant-cast false positive for @Todo types

2 participants