Restrict PYI034 for in-place operations to enclosing class#24511
Restrict PYI034 for in-place operations to enclosing class#24511charliermarsh wants to merge 2 commits intomainfrom
Conversation
|
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PYI034 | 2 | 0 | 2 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+0 -2 violations, +0 -0 fixes in 1 projects; 55 projects unchanged)
python/typeshed (+0 -2 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --no-fix --output-format concise --preview --select E,F,FA,I,PYI,RUF,UP,W
- stdlib/typing_extensions.pyi:265:9: PYI034 `__ior__` methods in classes like `_TypedDict` usually return `self` at runtime - stubs/boltons/boltons/dictutils.pyi:96:9: PYI034 `__ior__` methods in classes like `FrozenDict` usually return `self` at runtime
Changes by rule (1 rules affected)
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PYI034 | 2 | 0 | 2 | 0 | 0 |
| /// Return `true` if the return type for an in-place operator should be replaced with `Self`. | ||
| fn is_bad_inplace_return_type(checker: &Checker, returns: &ast::Expr, class_name: &str) -> bool { | ||
| if checker.source_type.is_stub() { | ||
| !is_self(returns, checker) |
There was a problem hiding this comment.
Do you think the special case is worth it? To me, defining __iadd__(self, other: A) -> int seems a pretty high signal that using Self here is not what the user wants.
There was a problem hiding this comment.
Hmm, not sure. I was following @AlexWaygood's guidance in #24462 (comment) where he said it did make sense to enforce this in stub files, though I also agree that (without that guidance) I would've made this change for both .py and .pyi files.
There was a problem hiding this comment.
The least confusing thing for Ruff might be just to make the rule consistent between .py and .pyi files. If the rule only supported .pyi files, I think there probably would not be a strong motivation for any change. But if Ruff makes this change for both .py files and .pyi files, maybe it makes sense for us to make the same change upstream in flake8-pyi, so that Ruff and flake8-pyi continue to be consistent about how they enforce the rule.
There was a problem hiding this comment.
(I updated the PR to match that logic.)
7ae9771 to
fa8470a
Compare
fa8470a to
8b21ffb
Compare
Summary
In
.pyand.pyifiles, we now only flag cases in which the return type is the enclosing class, like:As opposed to:
Closes #24462.