Skip to content

Conversation

@sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Mar 14, 2025

Summary

A follow-up to #16705 which documents various kinds of diagnostics that can appear when assigning to an attribute.

Test Plan

New snapshot tests.

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

mypy_primer results

No ecosystem changes detected ✅

Comment on lines +35 to +36
11 | instance.attr = 1 # error: [invalid-assignment]
| ^^^^^^^^^^^^^ Invalid assignment to data descriptor attribute `attr` on type `C` with custom `__set__` method
Copy link
Contributor

Choose a reason for hiding this comment

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

Not for fixing in this PR, but just noting it seems like we are currently inconsistent about whether we blame an invalid-assignment error on the target, on the RHS, or on the entire expression. I'm seeing that in case of x: int = "foo" we mark the entire expression, in x = "foo" (with a separate prior x: int) we mark just the target, and now here in these attribute cases we also mark just the target. Pyright seems to be consistent about always marking the RHS, and I can see the justification for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also thought it would be more intuitive to highlight the RHS, but didn't want to change behavior in my last PR or here. But that should be easy to fix.

@sharkdp sharkdp merged commit fe27572 into main Mar 14, 2025
22 checks passed
@sharkdp sharkdp deleted the david/document-attribute-assignment-diagnostics branch March 14, 2025 19:34
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