Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,9 @@ def func(x):
class Bar(type(foo)):
def foo_method(self):
pass

# https://github.com/astral-sh/ruff/issues/18459
class Example:
@classmethod
def function(this):
cls = 1234
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,9 @@ def __subclasscheck__(cls, other): ...

class MyProtocolMeta(type(Protocol)):
def __subclasscheck__(cls, other): ...


# https://github.com/astral-sh/ruff/issues/18459
class C:
def f(this):
self = 123
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use ruff_text_size::{Ranged, TextRange};

use crate::checkers::ast::{Checker, DiagnosticGuard};
use crate::registry::Rule;
use crate::renamer::Renamer;
use crate::renamer::{Renamer, ShadowedKind};
use crate::{Fix, Violation};

/// ## What it does
Expand Down Expand Up @@ -272,6 +272,7 @@ pub(crate) fn invalid_first_argument_name(checker: &Checker, scope: &Scope) {
function_type.diagnostic_kind(checker, self_or_cls.name.to_string(), self_or_cls.range());
diagnostic.try_set_optional_fix(|| {
rename_parameter(
checker,
scope,
self_or_cls,
parameters,
Expand All @@ -284,6 +285,7 @@ pub(crate) fn invalid_first_argument_name(checker: &Checker, scope: &Scope) {

/// Rename the first parameter to `self` or `cls`, if no other parameter has the target name.
fn rename_parameter(
checker: &Checker,
scope: &Scope<'_>,
self_or_cls: &ast::Parameter,
parameters: &ast::Parameters,
Expand All @@ -299,6 +301,16 @@ fn rename_parameter(
{
return Ok(None);
}
let binding = scope
.get(&self_or_cls.name)
.map(|binding_id| semantic.binding(binding_id))
.unwrap();

// Don't provide autofix if `self` or `cls` is already defined in the scope.
if ShadowedKind::new(binding, function_type.valid_first_argument_name(), checker).shadows_any()
{
return Ok(None);
}

let (edit, rest) = Renamer::rename(
&self_or_cls.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,13 @@ N804.py:74:20: N804 [*] First argument of a class method should be named `cls`
76 76 |
77 77 | def func(x):
78 78 | return x

N804.py:88:18: N804 First argument of a class method should be named `cls`
|
86 | class Example:
87 | @classmethod
88 | def function(this):
| ^^^^ N804
89 | cls = 1234
|
= help: Rename `this` to `cls`
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ N805.py:115:20: N805 [*] First argument of a method should be named `self`
119 119 | def bad_method(this):
120 120 | self = this

N805.py:119:20: N805 [*] First argument of a method should be named `self`
N805.py:119:20: N805 First argument of a method should be named `self`
|
117 | this
118 |
Expand All @@ -257,18 +257,6 @@ N805.py:119:20: N805 [*] First argument of a method should be named `self`
|
= help: Rename `this` to `self`

ℹ Unsafe fix
116 116 | this = this
117 117 | this
118 118 |
119 |- def bad_method(this):
120 |- self = this
119 |+ def bad_method(self):
120 |+ self = self
121 121 |
122 122 |
123 123 | class RenamingWithNFKC:

N805.py:124:17: N805 [*] First argument of a method should be named `self`
|
123 | class RenamingWithNFKC:
Expand All @@ -289,3 +277,13 @@ N805.py:124:17: N805 [*] First argument of a method should be named `self`
126 126 |
127 127 |
128 128 | from typing import Protocol

N805.py:141:11: N805 First argument of a method should be named `self`
|
139 | # https://github.com/astral-sh/ruff/issues/18459
140 | class C:
141 | def f(this):
| ^^^^ N805
142 | self = 123
|
= help: Rename `this` to `self`
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ N805.py:115:20: N805 [*] First argument of a method should be named `self`
119 119 | def bad_method(this):
120 120 | self = this

N805.py:119:20: N805 [*] First argument of a method should be named `self`
N805.py:119:20: N805 First argument of a method should be named `self`
|
117 | this
118 |
Expand All @@ -200,18 +200,6 @@ N805.py:119:20: N805 [*] First argument of a method should be named `self`
|
= help: Rename `this` to `self`

ℹ Unsafe fix
116 116 | this = this
117 117 | this
118 118 |
119 |- def bad_method(this):
120 |- self = this
119 |+ def bad_method(self):
120 |+ self = self
121 121 |
122 122 |
123 123 | class RenamingWithNFKC:

N805.py:124:17: N805 [*] First argument of a method should be named `self`
|
123 | class RenamingWithNFKC:
Expand All @@ -232,3 +220,13 @@ N805.py:124:17: N805 [*] First argument of a method should be named `self`
126 126 |
127 127 |
128 128 | from typing import Protocol

N805.py:141:11: N805 First argument of a method should be named `self`
|
139 | # https://github.com/astral-sh/ruff/issues/18459
140 | class C:
141 | def f(this):
| ^^^^ N805
142 | self = 123
|
= help: Rename `this` to `self`
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ N805.py:115:20: N805 [*] First argument of a method should be named `self`
119 119 | def bad_method(this):
120 120 | self = this

N805.py:119:20: N805 [*] First argument of a method should be named `self`
N805.py:119:20: N805 First argument of a method should be named `self`
|
117 | this
118 |
Expand All @@ -238,18 +238,6 @@ N805.py:119:20: N805 [*] First argument of a method should be named `self`
|
= help: Rename `this` to `self`

ℹ Unsafe fix
116 116 | this = this
117 117 | this
118 118 |
119 |- def bad_method(this):
120 |- self = this
119 |+ def bad_method(self):
120 |+ self = self
121 121 |
122 122 |
123 123 | class RenamingWithNFKC:

N805.py:124:17: N805 [*] First argument of a method should be named `self`
|
123 | class RenamingWithNFKC:
Expand All @@ -270,3 +258,13 @@ N805.py:124:17: N805 [*] First argument of a method should be named `self`
126 126 |
127 127 |
128 128 | from typing import Protocol

N805.py:141:11: N805 First argument of a method should be named `self`
|
139 | # https://github.com/astral-sh/ruff/issues/18459
140 | class C:
141 | def f(this):
| ^^^^ N805
142 | self = 123
|
= help: Rename `this` to `self`
Loading