From 70f2f89e3f3faacbd3c40860b571b0e6dc200245 Mon Sep 17 00:00:00 2001 From: Garrett-R Date: Fri, 3 Jan 2025 04:41:59 +0000 Subject: [PATCH] [`ruff`] Fix false positive on global keyword (`RUF052`) --- .../resources/test/fixtures/ruff/RUF052.py | 15 +- ..._rules__ruff__tests__RUF052_RUF052.py.snap | 465 +++++++++--------- ...var_regexp_preset__RUF052_RUF052.py_1.snap | 465 +++++++++--------- ...var_regexp_preset__RUF052_RUF052.py_2.snap | 203 ++++---- crates/ruff_python_semantic/src/binding.rs | 2 +- crates/ruff_python_semantic/src/model.rs | 2 +- 6 files changed, 583 insertions(+), 569 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/ruff/RUF052.py b/crates/ruff_linter/resources/test/fixtures/ruff/RUF052.py index 390dbb489e112..247fcec5425ee 100644 --- a/crates/ruff_linter/resources/test/fixtures/ruff/RUF052.py +++ b/crates/ruff_linter/resources/test/fixtures/ruff/RUF052.py @@ -1,4 +1,4 @@ -# Correct +##################### Correct ##################### for _ in range(5): pass @@ -37,6 +37,17 @@ def fun(): _x = "reassigned global" return _x +# (we had a false positive when a global var is used like this in 2 functions) +_num: int + +def set_num(): + global _num + _num = 1 + +def print_num(): + print(_num) + + class _ValidClass: pass @@ -70,7 +81,7 @@ def fun(x): return ___ return x -# Incorrect +##################### Incorrect ##################### class Class_: def fun(self): diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF052_RUF052.py.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF052_RUF052.py.snap index 06ecf0da87a10..912b45d8e95c7 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF052_RUF052.py.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF052_RUF052.py.snap @@ -1,343 +1,344 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs +snapshot_kind: text --- -RUF052.py:77:9: RUF052 [*] Local dummy variable `_var` is accessed +RUF052.py:88:9: RUF052 [*] Local dummy variable `_var` is accessed | -75 | class Class_: -76 | def fun(self): -77 | _var = "method variable" # [RUF052] +86 | class Class_: +87 | def fun(self): +88 | _var = "method variable" # [RUF052] | ^^^^ RUF052 -78 | return _var +89 | return _var | = help: Remove leading underscores ℹ Unsafe fix -74 74 | -75 75 | class Class_: -76 76 | def fun(self): -77 |- _var = "method variable" # [RUF052] -78 |- return _var - 77 |+ var = "method variable" # [RUF052] - 78 |+ return var -79 79 | -80 80 | def fun(_var): # parameters are ignored -81 81 | return _var +85 85 | +86 86 | class Class_: +87 87 | def fun(self): +88 |- _var = "method variable" # [RUF052] +89 |- return _var + 88 |+ var = "method variable" # [RUF052] + 89 |+ return var +90 90 | +91 91 | def fun(_var): # parameters are ignored +92 92 | return _var -RUF052.py:84:5: RUF052 [*] Local dummy variable `_list` is accessed +RUF052.py:95:5: RUF052 [*] Local dummy variable `_list` is accessed | -83 | def fun(): -84 | _list = "built-in" # [RUF052] +94 | def fun(): +95 | _list = "built-in" # [RUF052] | ^^^^^ RUF052 -85 | return _list +96 | return _list | = help: Prefer using trailing underscores to avoid shadowing a built-in ℹ Unsafe fix -81 81 | return _var -82 82 | -83 83 | def fun(): -84 |- _list = "built-in" # [RUF052] -85 |- return _list - 84 |+ list_ = "built-in" # [RUF052] - 85 |+ return list_ -86 86 | -87 87 | x = "global" -88 88 | +92 92 | return _var +93 93 | +94 94 | def fun(): +95 |- _list = "built-in" # [RUF052] +96 |- return _list + 95 |+ list_ = "built-in" # [RUF052] + 96 |+ return list_ +97 97 | +98 98 | x = "global" +99 99 | -RUF052.py:91:5: RUF052 [*] Local dummy variable `_x` is accessed - | -89 | def fun(): -90 | global x -91 | _x = "shadows global" # [RUF052] - | ^^ RUF052 -92 | return _x - | - = help: Prefer using trailing underscores to avoid shadowing a variable +RUF052.py:102:5: RUF052 [*] Local dummy variable `_x` is accessed + | +100 | def fun(): +101 | global x +102 | _x = "shadows global" # [RUF052] + | ^^ RUF052 +103 | return _x + | + = help: Prefer using trailing underscores to avoid shadowing a variable ℹ Unsafe fix -88 88 | -89 89 | def fun(): -90 90 | global x -91 |- _x = "shadows global" # [RUF052] -92 |- return _x - 91 |+ x_ = "shadows global" # [RUF052] - 92 |+ return x_ -93 93 | -94 94 | def foo(): -95 95 | x = "outer" +99 99 | +100 100 | def fun(): +101 101 | global x +102 |- _x = "shadows global" # [RUF052] +103 |- return _x + 102 |+ x_ = "shadows global" # [RUF052] + 103 |+ return x_ +104 104 | +105 105 | def foo(): +106 106 | x = "outer" -RUF052.py:98:5: RUF052 [*] Local dummy variable `_x` is accessed +RUF052.py:109:5: RUF052 [*] Local dummy variable `_x` is accessed | - 96 | def bar(): - 97 | nonlocal x - 98 | _x = "shadows nonlocal" # [RUF052] +107 | def bar(): +108 | nonlocal x +109 | _x = "shadows nonlocal" # [RUF052] | ^^ RUF052 - 99 | return _x -100 | bar() +110 | return _x +111 | bar() | = help: Prefer using trailing underscores to avoid shadowing a variable ℹ Unsafe fix -95 95 | x = "outer" -96 96 | def bar(): -97 97 | nonlocal x -98 |- _x = "shadows nonlocal" # [RUF052] -99 |- return _x - 98 |+ x_ = "shadows nonlocal" # [RUF052] - 99 |+ return x_ -100 100 | bar() -101 101 | return x -102 102 | +106 106 | x = "outer" +107 107 | def bar(): +108 108 | nonlocal x +109 |- _x = "shadows nonlocal" # [RUF052] +110 |- return _x + 109 |+ x_ = "shadows nonlocal" # [RUF052] + 110 |+ return x_ +111 111 | bar() +112 112 | return x +113 113 | -RUF052.py:105:5: RUF052 [*] Local dummy variable `_x` is accessed +RUF052.py:116:5: RUF052 [*] Local dummy variable `_x` is accessed | -103 | def fun(): -104 | x = "local" -105 | _x = "shadows local" # [RUF052] +114 | def fun(): +115 | x = "local" +116 | _x = "shadows local" # [RUF052] | ^^ RUF052 -106 | return _x +117 | return _x | = help: Prefer using trailing underscores to avoid shadowing a variable ℹ Unsafe fix -102 102 | -103 103 | def fun(): -104 104 | x = "local" -105 |- _x = "shadows local" # [RUF052] -106 |- return _x - 105 |+ x_ = "shadows local" # [RUF052] - 106 |+ return x_ -107 107 | -108 108 | -109 109 | GLOBAL_1 = "global 1" +113 113 | +114 114 | def fun(): +115 115 | x = "local" +116 |- _x = "shadows local" # [RUF052] +117 |- return _x + 116 |+ x_ = "shadows local" # [RUF052] + 117 |+ return x_ +118 118 | +119 119 | +120 120 | GLOBAL_1 = "global 1" -RUF052.py:113:5: RUF052 Local dummy variable `_GLOBAL_1` is accessed +RUF052.py:124:5: RUF052 Local dummy variable `_GLOBAL_1` is accessed | -112 | def unfixables(): -113 | _GLOBAL_1 = "foo" +123 | def unfixables(): +124 | _GLOBAL_1 = "foo" | ^^^^^^^^^ RUF052 -114 | # unfixable because the rename would shadow a global variable -115 | print(_GLOBAL_1) # [RUF052] +125 | # unfixable because the rename would shadow a global variable +126 | print(_GLOBAL_1) # [RUF052] | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:121:5: RUF052 Local dummy variable `_local` is accessed +RUF052.py:132:5: RUF052 Local dummy variable `_local` is accessed | -120 | # unfixable because the rename would shadow a local variable -121 | _local = "local3" # [RUF052] +131 | # unfixable because the rename would shadow a local variable +132 | _local = "local3" # [RUF052] | ^^^^^^ RUF052 -122 | print(_local) +133 | print(_local) | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:125:9: RUF052 Local dummy variable `_GLOBAL_1` is accessed +RUF052.py:136:9: RUF052 Local dummy variable `_GLOBAL_1` is accessed | -124 | def nested(): -125 | _GLOBAL_1 = "foo" +135 | def nested(): +136 | _GLOBAL_1 = "foo" | ^^^^^^^^^ RUF052 -126 | # unfixable because the rename would shadow a global variable -127 | print(_GLOBAL_1) # [RUF052] +137 | # unfixable because the rename would shadow a global variable +138 | print(_GLOBAL_1) # [RUF052] | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:130:9: RUF052 Local dummy variable `_local` is accessed +RUF052.py:141:9: RUF052 Local dummy variable `_local` is accessed | -129 | # unfixable because the rename would shadow a variable from the outer function -130 | _local = "local4" +140 | # unfixable because the rename would shadow a variable from the outer function +141 | _local = "local4" | ^^^^^^ RUF052 -131 | print(_local) +142 | print(_local) | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:138:5: RUF052 [*] Local dummy variable `_P` is accessed +RUF052.py:149:5: RUF052 [*] Local dummy variable `_P` is accessed | -136 | from collections import namedtuple -137 | -138 | _P = ParamSpec("_P") +147 | from collections import namedtuple +148 | +149 | _P = ParamSpec("_P") | ^^ RUF052 -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) | = help: Remove leading underscores ℹ Unsafe fix -135 135 | from enum import Enum -136 136 | from collections import namedtuple -137 137 | -138 |- _P = ParamSpec("_P") - 138 |+ P = ParamSpec("P") -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) +146 146 | from enum import Enum +147 147 | from collections import namedtuple +148 148 | +149 |- _P = ParamSpec("_P") + 149 |+ P = ParamSpec("P") +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) -------------------------------------------------------------------------------- -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:139:5: RUF052 [*] Local dummy variable `_T` is accessed +RUF052.py:150:5: RUF052 [*] Local dummy variable `_T` is accessed | -138 | _P = ParamSpec("_P") -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +149 | _P = ParamSpec("_P") +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) | ^^ RUF052 -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) | = help: Remove leading underscores ℹ Unsafe fix -136 136 | from collections import namedtuple -137 137 | -138 138 | _P = ParamSpec("_P") -139 |- _T = TypeVar(name="_T", covariant=True, bound=int|str) - 139 |+ T = TypeVar(name="T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +147 147 | from collections import namedtuple +148 148 | +149 149 | _P = ParamSpec("_P") +150 |- _T = TypeVar(name="_T", covariant=True, bound=int|str) + 150 |+ T = TypeVar(name="T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -------------------------------------------------------------------------------- -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:140:5: RUF052 [*] Local dummy variable `_NT` is accessed +RUF052.py:151:5: RUF052 [*] Local dummy variable `_NT` is accessed | -138 | _P = ParamSpec("_P") -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) +149 | _P = ParamSpec("_P") +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) | ^^^ RUF052 -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) | = help: Remove leading underscores ℹ Unsafe fix -137 137 | -138 138 | _P = ParamSpec("_P") -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 |- _NT = NamedTuple("_NT", [("foo", int)]) - 140 |+ NT = NamedTuple("NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +148 148 | +149 149 | _P = ParamSpec("_P") +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 |- _NT = NamedTuple("_NT", [("foo", int)]) + 151 |+ NT = NamedTuple("NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:141:5: RUF052 [*] Local dummy variable `_E` is accessed +RUF052.py:152:5: RUF052 [*] Local dummy variable `_E` is accessed | -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) | ^^ RUF052 -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) | = help: Remove leading underscores ℹ Unsafe fix -138 138 | _P = ParamSpec("_P") -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 |- _E = Enum("_E", ["a", "b", "c"]) - 141 |+ E = Enum("E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +149 149 | _P = ParamSpec("_P") +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 |- _E = Enum("_E", ["a", "b", "c"]) + 152 |+ E = Enum("E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:142:5: RUF052 [*] Local dummy variable `_NT2` is accessed +RUF052.py:153:5: RUF052 [*] Local dummy variable `_NT2` is accessed | -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) | ^^^^ RUF052 -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) | = help: Remove leading underscores ℹ Unsafe fix -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 |- _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) - 142 |+ NT2 = namedtuple("NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, NT2, _NT3, _DynamicClass, _NotADynamicClass) +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 |- _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) + 153 |+ NT2 = namedtuple("NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:143:5: RUF052 [*] Local dummy variable `_NT3` is accessed +RUF052.py:154:5: RUF052 [*] Local dummy variable `_NT3` is accessed | -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) | ^^^^ RUF052 -144 | _DynamicClass = type("_DynamicClass", (), {}) -145 | _NotADynamicClass = type("_NotADynamicClass") +155 | _DynamicClass = type("_DynamicClass", (), {}) +156 | _NotADynamicClass = type("_NotADynamicClass") | = help: Remove leading underscores ℹ Unsafe fix -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 |- _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) - 143 |+ NT3 = namedtuple(typename="NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, _NT2, NT3, _DynamicClass, _NotADynamicClass) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 |- _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) + 154 |+ NT3 = namedtuple(typename="NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, _NT2, NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:144:5: RUF052 [*] Local dummy variable `_DynamicClass` is accessed +RUF052.py:155:5: RUF052 [*] Local dummy variable `_DynamicClass` is accessed | -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) | ^^^^^^^^^^^^^ RUF052 -145 | _NotADynamicClass = type("_NotADynamicClass") +156 | _NotADynamicClass = type("_NotADynamicClass") | = help: Remove leading underscores ℹ Unsafe fix -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 |- _DynamicClass = type("_DynamicClass", (), {}) - 144 |+ DynamicClass = type("DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, _NT2, _NT3, DynamicClass, _NotADynamicClass) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 |- _DynamicClass = type("_DynamicClass", (), {}) + 155 |+ DynamicClass = type("DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, _NT2, _NT3, DynamicClass, _NotADynamicClass) -RUF052.py:145:5: RUF052 [*] Local dummy variable `_NotADynamicClass` is accessed +RUF052.py:156:5: RUF052 [*] Local dummy variable `_NotADynamicClass` is accessed | -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) -145 | _NotADynamicClass = type("_NotADynamicClass") +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) +156 | _NotADynamicClass = type("_NotADynamicClass") | ^^^^^^^^^^^^^^^^^ RUF052 -146 | -147 | print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +157 | +158 | print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) | = help: Remove leading underscores ℹ Unsafe fix -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 |- _NotADynamicClass = type("_NotADynamicClass") - 145 |+ NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, NotADynamicClass) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 |- _NotADynamicClass = type("_NotADynamicClass") + 156 |+ NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, NotADynamicClass) diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_1.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_1.snap index 06ecf0da87a10..912b45d8e95c7 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_1.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_1.snap @@ -1,343 +1,344 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs +snapshot_kind: text --- -RUF052.py:77:9: RUF052 [*] Local dummy variable `_var` is accessed +RUF052.py:88:9: RUF052 [*] Local dummy variable `_var` is accessed | -75 | class Class_: -76 | def fun(self): -77 | _var = "method variable" # [RUF052] +86 | class Class_: +87 | def fun(self): +88 | _var = "method variable" # [RUF052] | ^^^^ RUF052 -78 | return _var +89 | return _var | = help: Remove leading underscores ℹ Unsafe fix -74 74 | -75 75 | class Class_: -76 76 | def fun(self): -77 |- _var = "method variable" # [RUF052] -78 |- return _var - 77 |+ var = "method variable" # [RUF052] - 78 |+ return var -79 79 | -80 80 | def fun(_var): # parameters are ignored -81 81 | return _var +85 85 | +86 86 | class Class_: +87 87 | def fun(self): +88 |- _var = "method variable" # [RUF052] +89 |- return _var + 88 |+ var = "method variable" # [RUF052] + 89 |+ return var +90 90 | +91 91 | def fun(_var): # parameters are ignored +92 92 | return _var -RUF052.py:84:5: RUF052 [*] Local dummy variable `_list` is accessed +RUF052.py:95:5: RUF052 [*] Local dummy variable `_list` is accessed | -83 | def fun(): -84 | _list = "built-in" # [RUF052] +94 | def fun(): +95 | _list = "built-in" # [RUF052] | ^^^^^ RUF052 -85 | return _list +96 | return _list | = help: Prefer using trailing underscores to avoid shadowing a built-in ℹ Unsafe fix -81 81 | return _var -82 82 | -83 83 | def fun(): -84 |- _list = "built-in" # [RUF052] -85 |- return _list - 84 |+ list_ = "built-in" # [RUF052] - 85 |+ return list_ -86 86 | -87 87 | x = "global" -88 88 | +92 92 | return _var +93 93 | +94 94 | def fun(): +95 |- _list = "built-in" # [RUF052] +96 |- return _list + 95 |+ list_ = "built-in" # [RUF052] + 96 |+ return list_ +97 97 | +98 98 | x = "global" +99 99 | -RUF052.py:91:5: RUF052 [*] Local dummy variable `_x` is accessed - | -89 | def fun(): -90 | global x -91 | _x = "shadows global" # [RUF052] - | ^^ RUF052 -92 | return _x - | - = help: Prefer using trailing underscores to avoid shadowing a variable +RUF052.py:102:5: RUF052 [*] Local dummy variable `_x` is accessed + | +100 | def fun(): +101 | global x +102 | _x = "shadows global" # [RUF052] + | ^^ RUF052 +103 | return _x + | + = help: Prefer using trailing underscores to avoid shadowing a variable ℹ Unsafe fix -88 88 | -89 89 | def fun(): -90 90 | global x -91 |- _x = "shadows global" # [RUF052] -92 |- return _x - 91 |+ x_ = "shadows global" # [RUF052] - 92 |+ return x_ -93 93 | -94 94 | def foo(): -95 95 | x = "outer" +99 99 | +100 100 | def fun(): +101 101 | global x +102 |- _x = "shadows global" # [RUF052] +103 |- return _x + 102 |+ x_ = "shadows global" # [RUF052] + 103 |+ return x_ +104 104 | +105 105 | def foo(): +106 106 | x = "outer" -RUF052.py:98:5: RUF052 [*] Local dummy variable `_x` is accessed +RUF052.py:109:5: RUF052 [*] Local dummy variable `_x` is accessed | - 96 | def bar(): - 97 | nonlocal x - 98 | _x = "shadows nonlocal" # [RUF052] +107 | def bar(): +108 | nonlocal x +109 | _x = "shadows nonlocal" # [RUF052] | ^^ RUF052 - 99 | return _x -100 | bar() +110 | return _x +111 | bar() | = help: Prefer using trailing underscores to avoid shadowing a variable ℹ Unsafe fix -95 95 | x = "outer" -96 96 | def bar(): -97 97 | nonlocal x -98 |- _x = "shadows nonlocal" # [RUF052] -99 |- return _x - 98 |+ x_ = "shadows nonlocal" # [RUF052] - 99 |+ return x_ -100 100 | bar() -101 101 | return x -102 102 | +106 106 | x = "outer" +107 107 | def bar(): +108 108 | nonlocal x +109 |- _x = "shadows nonlocal" # [RUF052] +110 |- return _x + 109 |+ x_ = "shadows nonlocal" # [RUF052] + 110 |+ return x_ +111 111 | bar() +112 112 | return x +113 113 | -RUF052.py:105:5: RUF052 [*] Local dummy variable `_x` is accessed +RUF052.py:116:5: RUF052 [*] Local dummy variable `_x` is accessed | -103 | def fun(): -104 | x = "local" -105 | _x = "shadows local" # [RUF052] +114 | def fun(): +115 | x = "local" +116 | _x = "shadows local" # [RUF052] | ^^ RUF052 -106 | return _x +117 | return _x | = help: Prefer using trailing underscores to avoid shadowing a variable ℹ Unsafe fix -102 102 | -103 103 | def fun(): -104 104 | x = "local" -105 |- _x = "shadows local" # [RUF052] -106 |- return _x - 105 |+ x_ = "shadows local" # [RUF052] - 106 |+ return x_ -107 107 | -108 108 | -109 109 | GLOBAL_1 = "global 1" +113 113 | +114 114 | def fun(): +115 115 | x = "local" +116 |- _x = "shadows local" # [RUF052] +117 |- return _x + 116 |+ x_ = "shadows local" # [RUF052] + 117 |+ return x_ +118 118 | +119 119 | +120 120 | GLOBAL_1 = "global 1" -RUF052.py:113:5: RUF052 Local dummy variable `_GLOBAL_1` is accessed +RUF052.py:124:5: RUF052 Local dummy variable `_GLOBAL_1` is accessed | -112 | def unfixables(): -113 | _GLOBAL_1 = "foo" +123 | def unfixables(): +124 | _GLOBAL_1 = "foo" | ^^^^^^^^^ RUF052 -114 | # unfixable because the rename would shadow a global variable -115 | print(_GLOBAL_1) # [RUF052] +125 | # unfixable because the rename would shadow a global variable +126 | print(_GLOBAL_1) # [RUF052] | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:121:5: RUF052 Local dummy variable `_local` is accessed +RUF052.py:132:5: RUF052 Local dummy variable `_local` is accessed | -120 | # unfixable because the rename would shadow a local variable -121 | _local = "local3" # [RUF052] +131 | # unfixable because the rename would shadow a local variable +132 | _local = "local3" # [RUF052] | ^^^^^^ RUF052 -122 | print(_local) +133 | print(_local) | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:125:9: RUF052 Local dummy variable `_GLOBAL_1` is accessed +RUF052.py:136:9: RUF052 Local dummy variable `_GLOBAL_1` is accessed | -124 | def nested(): -125 | _GLOBAL_1 = "foo" +135 | def nested(): +136 | _GLOBAL_1 = "foo" | ^^^^^^^^^ RUF052 -126 | # unfixable because the rename would shadow a global variable -127 | print(_GLOBAL_1) # [RUF052] +137 | # unfixable because the rename would shadow a global variable +138 | print(_GLOBAL_1) # [RUF052] | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:130:9: RUF052 Local dummy variable `_local` is accessed +RUF052.py:141:9: RUF052 Local dummy variable `_local` is accessed | -129 | # unfixable because the rename would shadow a variable from the outer function -130 | _local = "local4" +140 | # unfixable because the rename would shadow a variable from the outer function +141 | _local = "local4" | ^^^^^^ RUF052 -131 | print(_local) +142 | print(_local) | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:138:5: RUF052 [*] Local dummy variable `_P` is accessed +RUF052.py:149:5: RUF052 [*] Local dummy variable `_P` is accessed | -136 | from collections import namedtuple -137 | -138 | _P = ParamSpec("_P") +147 | from collections import namedtuple +148 | +149 | _P = ParamSpec("_P") | ^^ RUF052 -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) | = help: Remove leading underscores ℹ Unsafe fix -135 135 | from enum import Enum -136 136 | from collections import namedtuple -137 137 | -138 |- _P = ParamSpec("_P") - 138 |+ P = ParamSpec("P") -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) +146 146 | from enum import Enum +147 147 | from collections import namedtuple +148 148 | +149 |- _P = ParamSpec("_P") + 149 |+ P = ParamSpec("P") +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) -------------------------------------------------------------------------------- -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:139:5: RUF052 [*] Local dummy variable `_T` is accessed +RUF052.py:150:5: RUF052 [*] Local dummy variable `_T` is accessed | -138 | _P = ParamSpec("_P") -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +149 | _P = ParamSpec("_P") +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) | ^^ RUF052 -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) | = help: Remove leading underscores ℹ Unsafe fix -136 136 | from collections import namedtuple -137 137 | -138 138 | _P = ParamSpec("_P") -139 |- _T = TypeVar(name="_T", covariant=True, bound=int|str) - 139 |+ T = TypeVar(name="T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +147 147 | from collections import namedtuple +148 148 | +149 149 | _P = ParamSpec("_P") +150 |- _T = TypeVar(name="_T", covariant=True, bound=int|str) + 150 |+ T = TypeVar(name="T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -------------------------------------------------------------------------------- -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:140:5: RUF052 [*] Local dummy variable `_NT` is accessed +RUF052.py:151:5: RUF052 [*] Local dummy variable `_NT` is accessed | -138 | _P = ParamSpec("_P") -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) +149 | _P = ParamSpec("_P") +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) | ^^^ RUF052 -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) | = help: Remove leading underscores ℹ Unsafe fix -137 137 | -138 138 | _P = ParamSpec("_P") -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 |- _NT = NamedTuple("_NT", [("foo", int)]) - 140 |+ NT = NamedTuple("NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +148 148 | +149 149 | _P = ParamSpec("_P") +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 |- _NT = NamedTuple("_NT", [("foo", int)]) + 151 |+ NT = NamedTuple("NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:141:5: RUF052 [*] Local dummy variable `_E` is accessed +RUF052.py:152:5: RUF052 [*] Local dummy variable `_E` is accessed | -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) | ^^ RUF052 -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) | = help: Remove leading underscores ℹ Unsafe fix -138 138 | _P = ParamSpec("_P") -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 |- _E = Enum("_E", ["a", "b", "c"]) - 141 |+ E = Enum("E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +149 149 | _P = ParamSpec("_P") +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 |- _E = Enum("_E", ["a", "b", "c"]) + 152 |+ E = Enum("E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:142:5: RUF052 [*] Local dummy variable `_NT2` is accessed +RUF052.py:153:5: RUF052 [*] Local dummy variable `_NT2` is accessed | -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) | ^^^^ RUF052 -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) | = help: Remove leading underscores ℹ Unsafe fix -139 139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 |- _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) - 142 |+ NT2 = namedtuple("NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, NT2, _NT3, _DynamicClass, _NotADynamicClass) +150 150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 |- _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) + 153 |+ NT2 = namedtuple("NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, NT2, _NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:143:5: RUF052 [*] Local dummy variable `_NT3` is accessed +RUF052.py:154:5: RUF052 [*] Local dummy variable `_NT3` is accessed | -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) | ^^^^ RUF052 -144 | _DynamicClass = type("_DynamicClass", (), {}) -145 | _NotADynamicClass = type("_NotADynamicClass") +155 | _DynamicClass = type("_DynamicClass", (), {}) +156 | _NotADynamicClass = type("_NotADynamicClass") | = help: Remove leading underscores ℹ Unsafe fix -140 140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 |- _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) - 143 |+ NT3 = namedtuple(typename="NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, _NT2, NT3, _DynamicClass, _NotADynamicClass) +151 151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 |- _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) + 154 |+ NT3 = namedtuple(typename="NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, _NT2, NT3, _DynamicClass, _NotADynamicClass) -RUF052.py:144:5: RUF052 [*] Local dummy variable `_DynamicClass` is accessed +RUF052.py:155:5: RUF052 [*] Local dummy variable `_DynamicClass` is accessed | -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) | ^^^^^^^^^^^^^ RUF052 -145 | _NotADynamicClass = type("_NotADynamicClass") +156 | _NotADynamicClass = type("_NotADynamicClass") | = help: Remove leading underscores ℹ Unsafe fix -141 141 | _E = Enum("_E", ["a", "b", "c"]) -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 |- _DynamicClass = type("_DynamicClass", (), {}) - 144 |+ DynamicClass = type("DynamicClass", (), {}) -145 145 | _NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, _NT2, _NT3, DynamicClass, _NotADynamicClass) +152 152 | _E = Enum("_E", ["a", "b", "c"]) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 |- _DynamicClass = type("_DynamicClass", (), {}) + 155 |+ DynamicClass = type("DynamicClass", (), {}) +156 156 | _NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, _NT2, _NT3, DynamicClass, _NotADynamicClass) -RUF052.py:145:5: RUF052 [*] Local dummy variable `_NotADynamicClass` is accessed +RUF052.py:156:5: RUF052 [*] Local dummy variable `_NotADynamicClass` is accessed | -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) -145 | _NotADynamicClass = type("_NotADynamicClass") +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) +156 | _NotADynamicClass = type("_NotADynamicClass") | ^^^^^^^^^^^^^^^^^ RUF052 -146 | -147 | print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +157 | +158 | print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) | = help: Remove leading underscores ℹ Unsafe fix -142 142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 144 | _DynamicClass = type("_DynamicClass", (), {}) -145 |- _NotADynamicClass = type("_NotADynamicClass") - 145 |+ NotADynamicClass = type("_NotADynamicClass") -146 146 | -147 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) - 147 |+ print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, NotADynamicClass) +153 153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 155 | _DynamicClass = type("_DynamicClass", (), {}) +156 |- _NotADynamicClass = type("_NotADynamicClass") + 156 |+ NotADynamicClass = type("_NotADynamicClass") +157 157 | +158 |- print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) + 158 |+ print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, NotADynamicClass) diff --git a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_2.snap b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_2.snap index 8974885f86459..78e5f22d5305c 100644 --- a/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_2.snap +++ b/crates/ruff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__custom_dummy_var_regexp_preset__RUF052_RUF052.py_2.snap @@ -1,185 +1,186 @@ --- source: crates/ruff_linter/src/rules/ruff/mod.rs +snapshot_kind: text --- -RUF052.py:77:9: RUF052 Local dummy variable `_var` is accessed +RUF052.py:88:9: RUF052 Local dummy variable `_var` is accessed | -75 | class Class_: -76 | def fun(self): -77 | _var = "method variable" # [RUF052] +86 | class Class_: +87 | def fun(self): +88 | _var = "method variable" # [RUF052] | ^^^^ RUF052 -78 | return _var +89 | return _var | = help: Remove leading underscores -RUF052.py:84:5: RUF052 Local dummy variable `_list` is accessed +RUF052.py:95:5: RUF052 Local dummy variable `_list` is accessed | -83 | def fun(): -84 | _list = "built-in" # [RUF052] +94 | def fun(): +95 | _list = "built-in" # [RUF052] | ^^^^^ RUF052 -85 | return _list +96 | return _list | = help: Prefer using trailing underscores to avoid shadowing a built-in -RUF052.py:91:5: RUF052 Local dummy variable `_x` is accessed - | -89 | def fun(): -90 | global x -91 | _x = "shadows global" # [RUF052] - | ^^ RUF052 -92 | return _x - | - = help: Prefer using trailing underscores to avoid shadowing a variable +RUF052.py:102:5: RUF052 Local dummy variable `_x` is accessed + | +100 | def fun(): +101 | global x +102 | _x = "shadows global" # [RUF052] + | ^^ RUF052 +103 | return _x + | + = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:95:3: RUF052 Local dummy variable `x` is accessed - | -94 | def foo(): -95 | x = "outer" - | ^ RUF052 -96 | def bar(): -97 | nonlocal x - | +RUF052.py:106:3: RUF052 Local dummy variable `x` is accessed + | +105 | def foo(): +106 | x = "outer" + | ^ RUF052 +107 | def bar(): +108 | nonlocal x + | -RUF052.py:98:5: RUF052 Local dummy variable `_x` is accessed +RUF052.py:109:5: RUF052 Local dummy variable `_x` is accessed | - 96 | def bar(): - 97 | nonlocal x - 98 | _x = "shadows nonlocal" # [RUF052] +107 | def bar(): +108 | nonlocal x +109 | _x = "shadows nonlocal" # [RUF052] | ^^ RUF052 - 99 | return _x -100 | bar() +110 | return _x +111 | bar() | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:105:5: RUF052 Local dummy variable `_x` is accessed +RUF052.py:116:5: RUF052 Local dummy variable `_x` is accessed | -103 | def fun(): -104 | x = "local" -105 | _x = "shadows local" # [RUF052] +114 | def fun(): +115 | x = "local" +116 | _x = "shadows local" # [RUF052] | ^^ RUF052 -106 | return _x +117 | return _x | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:113:5: RUF052 Local dummy variable `_GLOBAL_1` is accessed +RUF052.py:124:5: RUF052 Local dummy variable `_GLOBAL_1` is accessed | -112 | def unfixables(): -113 | _GLOBAL_1 = "foo" +123 | def unfixables(): +124 | _GLOBAL_1 = "foo" | ^^^^^^^^^ RUF052 -114 | # unfixable because the rename would shadow a global variable -115 | print(_GLOBAL_1) # [RUF052] +125 | # unfixable because the rename would shadow a global variable +126 | print(_GLOBAL_1) # [RUF052] | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:121:5: RUF052 Local dummy variable `_local` is accessed +RUF052.py:132:5: RUF052 Local dummy variable `_local` is accessed | -120 | # unfixable because the rename would shadow a local variable -121 | _local = "local3" # [RUF052] +131 | # unfixable because the rename would shadow a local variable +132 | _local = "local3" # [RUF052] | ^^^^^^ RUF052 -122 | print(_local) +133 | print(_local) | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:125:9: RUF052 Local dummy variable `_GLOBAL_1` is accessed +RUF052.py:136:9: RUF052 Local dummy variable `_GLOBAL_1` is accessed | -124 | def nested(): -125 | _GLOBAL_1 = "foo" +135 | def nested(): +136 | _GLOBAL_1 = "foo" | ^^^^^^^^^ RUF052 -126 | # unfixable because the rename would shadow a global variable -127 | print(_GLOBAL_1) # [RUF052] +137 | # unfixable because the rename would shadow a global variable +138 | print(_GLOBAL_1) # [RUF052] | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:130:9: RUF052 Local dummy variable `_local` is accessed +RUF052.py:141:9: RUF052 Local dummy variable `_local` is accessed | -129 | # unfixable because the rename would shadow a variable from the outer function -130 | _local = "local4" +140 | # unfixable because the rename would shadow a variable from the outer function +141 | _local = "local4" | ^^^^^^ RUF052 -131 | print(_local) +142 | print(_local) | = help: Prefer using trailing underscores to avoid shadowing a variable -RUF052.py:138:5: RUF052 Local dummy variable `_P` is accessed +RUF052.py:149:5: RUF052 Local dummy variable `_P` is accessed | -136 | from collections import namedtuple -137 | -138 | _P = ParamSpec("_P") +147 | from collections import namedtuple +148 | +149 | _P = ParamSpec("_P") | ^^ RUF052 -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) | = help: Remove leading underscores -RUF052.py:139:5: RUF052 Local dummy variable `_T` is accessed +RUF052.py:150:5: RUF052 Local dummy variable `_T` is accessed | -138 | _P = ParamSpec("_P") -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +149 | _P = ParamSpec("_P") +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) | ^^ RUF052 -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) | = help: Remove leading underscores -RUF052.py:140:5: RUF052 Local dummy variable `_NT` is accessed +RUF052.py:151:5: RUF052 Local dummy variable `_NT` is accessed | -138 | _P = ParamSpec("_P") -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) +149 | _P = ParamSpec("_P") +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) | ^^^ RUF052 -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) | = help: Remove leading underscores -RUF052.py:141:5: RUF052 Local dummy variable `_E` is accessed +RUF052.py:152:5: RUF052 Local dummy variable `_E` is accessed | -139 | _T = TypeVar(name="_T", covariant=True, bound=int|str) -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) +150 | _T = TypeVar(name="_T", covariant=True, bound=int|str) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) | ^^ RUF052 -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) | = help: Remove leading underscores -RUF052.py:142:5: RUF052 Local dummy variable `_NT2` is accessed +RUF052.py:153:5: RUF052 Local dummy variable `_NT2` is accessed | -140 | _NT = NamedTuple("_NT", [("foo", int)]) -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +151 | _NT = NamedTuple("_NT", [("foo", int)]) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) | ^^^^ RUF052 -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) | = help: Remove leading underscores -RUF052.py:143:5: RUF052 Local dummy variable `_NT3` is accessed +RUF052.py:154:5: RUF052 Local dummy variable `_NT3` is accessed | -141 | _E = Enum("_E", ["a", "b", "c"]) -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +152 | _E = Enum("_E", ["a", "b", "c"]) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) | ^^^^ RUF052 -144 | _DynamicClass = type("_DynamicClass", (), {}) -145 | _NotADynamicClass = type("_NotADynamicClass") +155 | _DynamicClass = type("_DynamicClass", (), {}) +156 | _NotADynamicClass = type("_NotADynamicClass") | = help: Remove leading underscores -RUF052.py:144:5: RUF052 Local dummy variable `_DynamicClass` is accessed +RUF052.py:155:5: RUF052 Local dummy variable `_DynamicClass` is accessed | -142 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) +153 | _NT2 = namedtuple("_NT2", ['x', 'y', 'z']) +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) | ^^^^^^^^^^^^^ RUF052 -145 | _NotADynamicClass = type("_NotADynamicClass") +156 | _NotADynamicClass = type("_NotADynamicClass") | = help: Remove leading underscores -RUF052.py:145:5: RUF052 Local dummy variable `_NotADynamicClass` is accessed +RUF052.py:156:5: RUF052 Local dummy variable `_NotADynamicClass` is accessed | -143 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) -144 | _DynamicClass = type("_DynamicClass", (), {}) -145 | _NotADynamicClass = type("_NotADynamicClass") +154 | _NT3 = namedtuple(typename="_NT3", field_names=['x', 'y', 'z']) +155 | _DynamicClass = type("_DynamicClass", (), {}) +156 | _NotADynamicClass = type("_NotADynamicClass") | ^^^^^^^^^^^^^^^^^ RUF052 -146 | -147 | print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) +157 | +158 | print(_T, _P, _NT, _E, _NT2, _NT3, _DynamicClass, _NotADynamicClass) | = help: Remove leading underscores diff --git a/crates/ruff_python_semantic/src/binding.rs b/crates/ruff_python_semantic/src/binding.rs index 6d7e568d5ddbe..a2f9c8ee34668 100644 --- a/crates/ruff_python_semantic/src/binding.rs +++ b/crates/ruff_python_semantic/src/binding.rs @@ -445,7 +445,7 @@ impl Ranged for Binding<'_> { /// ID uniquely identifying a [Binding] in a program. /// -/// Using a `u32` to identify [Binding]s should is sufficient because Ruff only supports documents with a +/// Using a `u32` to identify [Binding]s should be sufficient because Ruff only supports documents with a /// size smaller than or equal to `u32::max`. A document with the size of `u32::max` must have fewer than `u32::max` /// bindings because bindings must be separated by whitespace (and have an assignment). #[newtype_index] diff --git a/crates/ruff_python_semantic/src/model.rs b/crates/ruff_python_semantic/src/model.rs index f43cf24de1ec5..d96f771cf1350 100644 --- a/crates/ruff_python_semantic/src/model.rs +++ b/crates/ruff_python_semantic/src/model.rs @@ -1498,7 +1498,7 @@ impl<'a> SemanticModel<'a> { source: self.node_id, context: self.execution_context(), exceptions: self.exceptions(), - flags: BindingFlags::empty(), + flags: BindingFlags::GLOBAL, }); self.global_scope_mut().add(name, id); }