Skip to content

[pylint] Redefined outer names (PLW0621) #15903

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
@@ -0,0 +1,158 @@
from __future__ import annotations

import pytest


@pytest.fixture
def f(): ...

@pytest.yield_fixture
def yf(): ...


### Errors

global_a = 1

def outer_1(a):
def inner_1(a): ...


def outer_2():
a = 1
def inner_2(a): ...


def outer_3():
def inner_3(a): ...
a = 1


def outer_4():
def inner_4(outer_4): ...


def outer_5(global_a): ...


def outer_6():
def inner_6(global_a): ...


def outer_7():
def inner_7(f): ...


def outer_8():
def inner_8(yf): ...


def outer_9(outer_100):
...


def outer_10():
def inner_10(outer_100): ...


class Outer11[T]:
def inner_11(self, T): ...


def outer_12():
a = 0
inner_12 = lambda a: a + 1


def outer_13():
inner_13 = lambda a: lambda a, b: a + b - 1


def outer_14():
inner_14 = (lambda a, b: a - b / 2 for a, b in [])


def outer_15():
try:
...
except RuntimeError as e:
def inner_15(e): ...


def outer_16():
try:
...
except RuntimeError as e:
inner_16 = lambda e: ...


### No errors

def outer_100(): ...


def outer_101(f): ...


def outer_102(yf): ...


def outer_103():
outer_103 = 0


def outer_104():
def inner_104[outer_104](): ...


class Outer105:
a = 1
def inner_105(self, a): ...


class Outer106:
def inner_105(self, a): ...
a = 1


class Outer107:
def inner_105(self, a): ...
def a(self): ...


class Outer108:
_ = lambda a: a + 1
a = 0


class Outer109:
a = 0
inner_109 = [lambda a: a @ 3 for _ in [1]]


def outer_110(annotations): ...


def outer_111():
_a = 0

def inner_111(_a): ...


def outer_112():
inner_112 = lambda _a: ...
_a = 0


class Outer113:
try:
...
except RuntimeError as e:
def inner_113(e): ...


class Outer114:
try:
...
except RuntimeError as e:
inner_114 = lambda e: ...
6 changes: 6 additions & 0 deletions crates/ruff_linter/src/checkers/ast/analyze/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub(crate) fn bindings(checker: &Checker) {
Rule::ForLoopWrites,
Rule::CustomTypeVarForSelf,
Rule::PrivateTypeParameter,
Rule::RedefinedOuterName,
]) {
return;
}
Expand Down Expand Up @@ -129,5 +130,10 @@ pub(crate) fn bindings(checker: &Checker) {
checker.report_diagnostic(diagnostic);
}
}
if checker.enabled(Rule::RedefinedOuterName) {
if let Some(diagnostic) = pylint::rules::redefined_outer_name(checker, binding) {
checker.report_diagnostic(diagnostic);
}
}
}
}
1 change: 1 addition & 0 deletions crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Pylint, "W0602") => (RuleGroup::Stable, rules::pylint::rules::GlobalVariableNotAssigned),
(Pylint, "W0603") => (RuleGroup::Stable, rules::pylint::rules::GlobalStatement),
(Pylint, "W0604") => (RuleGroup::Stable, rules::pylint::rules::GlobalAtModuleLevel),
(Pylint, "W0621") => (RuleGroup::Preview, rules::pylint::rules::RedefinedOuterName),
(Pylint, "W0642") => (RuleGroup::Stable, rules::pylint::rules::SelfOrClsAssignment),
(Pylint, "W0711") => (RuleGroup::Stable, rules::pylint::rules::BinaryOpException),
(Pylint, "W1501") => (RuleGroup::Stable, rules::pylint::rules::BadOpenMode),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,7 @@ impl<'a> Visitor<'a> for SkipFunctionsVisitor<'a> {
}
}

fn fixture_decorator<'a>(
pub(crate) fn fixture_decorator<'a>(
decorators: &'a [Decorator],
semantic: &SemanticModel,
) -> Option<&'a Decorator> {
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_linter/src/rules/pylint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ mod tests {
)]
#[test_case(Rule::InvalidEnvvarDefault, Path::new("invalid_envvar_default.py"))]
#[test_case(Rule::BadStrStripCall, Path::new("bad_str_strip_call.py"))]
#[test_case(Rule::RedefinedOuterName, Path::new("redefined_outer_name.py"))]
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!(
"preview__{}_{}",
Expand Down
2 changes: 2 additions & 0 deletions crates/ruff_linter/src/rules/pylint/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub(crate) use property_with_parameters::*;
pub(crate) use redeclared_assigned_name::*;
pub(crate) use redefined_argument_from_local::*;
pub(crate) use redefined_loop_name::*;
pub(crate) use redefined_outer_name::*;
pub(crate) use redefined_slots_in_subclass::*;
pub(crate) use repeated_equality_comparison::*;
pub(crate) use repeated_isinstance_calls::*;
Expand Down Expand Up @@ -172,6 +173,7 @@ mod property_with_parameters;
mod redeclared_assigned_name;
mod redefined_argument_from_local;
mod redefined_loop_name;
mod redefined_outer_name;
mod redefined_slots_in_subclass;
mod repeated_equality_comparison;
mod repeated_isinstance_calls;
Expand Down
Loading
Loading