Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Mar 13, 2025

Summary

Uses the try_call_dunder infrastructure for augmented assignment and fixes the logic to work for types other than Type::Instance(…). This allows us to infer the correct type here:

x = (1, 2)
x += (3, 4)
reveal_type(x)  # revealed: tuple[Literal[1], Literal[2], Literal[3], Literal[4]]

Or in this (extremely weird) scenario:

class Meta(type):
    def __iadd__(cls, other: int) -> str:
        return ""

class C(metaclass=Meta): ...

cls = C
cls += 1

reveal_type(cls)  # revealed: str

Union and intersection handling could also be improved here, but I made no attempt to do so in this PR.

Test Plan

New MD tests

@sharkdp sharkdp added the ty Multi-file analysis & type inference label Mar 13, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Mar 13, 2025

mypy_primer results

No ecosystem changes detected ✅

@sharkdp sharkdp force-pushed the david/augmented-assignment branch from ca70a75 to c94240e Compare March 14, 2025 11:54
@sharkdp sharkdp changed the title [red-knot] Use try_call_dunder for augmented assignment [red-knot] Use try_call_dunder for augmented assignment Mar 14, 2025
@sharkdp sharkdp marked this pull request as ready for review March 14, 2025 11:56
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Fantastic!

@sharkdp sharkdp merged commit ebcad6e into main Mar 14, 2025
22 checks passed
@sharkdp sharkdp deleted the david/augmented-assignment branch March 14, 2025 19:36
dcreager added a commit that referenced this pull request Mar 14, 2025
* main:
  [red-knot] Use `try_call_dunder` for augmented assignment (#16717)
  [red-knot] Document current state of attribute assignment diagnostics (#16746)
  [red-knot] Case sensitive module resolver (#16521)
  [red-knot] Very minor simplification of the render tests (#16759)
  [syntax-errors] Unparenthesized assignment expressions in sets and indexes (#16404)
  ruff_db: add a new diagnostic renderer
  ruff_db: add `context` configuration
  red_knot: plumb through `DiagnosticFormat` to the CLI
  ruff_db: add concise diagnostic mode
  [syntax-errors] Star annotations before Python 3.11 (#16545)
  [syntax-errors] Star expression in index before Python 3.11 (#16544)
  Ruff 0.11.0 (#16723)
  [red-knot] Preliminary tests for typing.Final (#15917)
  [red-knot] fix: improve type inference for binary ops on tuples (#16725)
  [red-knot] Assignments to attributes (#16705)
  [`pygrep-hooks`]: Detect file-level suppressions comments without rul… (#16720)
  Fallback to requires-python in certain cases when target-version is not found (#16721)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants