Skip to content

Commit eafa274

Browse files
authored
Enable Ruff PLW (Pylint Warning) (#13749)
1 parent df1206f commit eafa274

14 files changed

+43
-37
lines changed

lib/ts_utils/utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,11 @@ def parse_stdlib_versions_file() -> SupportedVersionsDict:
123123
result: dict[str, tuple[VersionTuple, VersionTuple]] = {}
124124
with VERSIONS_PATH.open(encoding="UTF-8") as f:
125125
for line in f:
126-
line = strip_comments(line)
127-
if line == "":
126+
stripped_line = strip_comments(line)
127+
if stripped_line == "":
128128
continue
129-
m = VERSION_LINE_RE.match(line)
130-
assert m, f"invalid VERSIONS line: {line}"
129+
m = VERSION_LINE_RE.match(stripped_line)
130+
assert m, f"invalid VERSIONS line: {stripped_line}"
131131
mod: str = m.group(1)
132132
assert mod not in result, f"Duplicate module {mod} in VERSIONS"
133133
min_version = _parse_version(m.group(2))

pyproject.toml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ select = [
5353
"I", # isort
5454
"N", # pep8-naming
5555
"PGH", # pygrep-hooks
56-
"PLC", # Pylint Convention
57-
"PLE", # Pylint Error
58-
"PLR", # Pylint Refactor
56+
"PL", # Pylint
5957
"RUF", # Ruff-specific and unused-noqa
6058
"TRY", # tryceratops
6159
"UP", # pyupgrade
@@ -186,15 +184,19 @@ ignore = [
186184
# Rules that are out of the control of stub authors:
187185
###
188186
# Names in stubs should match the implementation, even if it's ambiguous.
187+
# https://github.com/astral-sh/ruff/issues/15293
189188
"A", # flake8-builtins
190-
"F403", # `from . import *` used; unable to detect undefined names
191189
# Stubs can sometimes re-export entire modules.
192190
# Issues with using a star-imported name will be caught by type-checkers.
191+
"F403", # `from . import *` used; unable to detect undefined names
193192
"F405", # may be undefined, or defined from star imports
194193
# Most pep8-naming rules don't apply for third-party stubs like typeshed.
195194
# N811 to N814 could apply, but we often use them to disambiguate a name whilst making it look like a more common one
196195
"N8", # pep8-naming
196+
# Stubs are allowed to use private variables (pyright's reportPrivateUsage is also disabled)
197197
"PLC2701", # Private name import from external module
198+
# Names in stubs should match implementation
199+
"PLW0211", # First argument of a static method should not be named `{argument_name}`
198200
]
199201
"lib/ts_utils/**" = [
200202
# Doesn't affect stubs. The only re-exports we have should be in our local lib ts_utils

scripts/create_baseline_stubs.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ def run_stubgen(package: str, output: str) -> None:
5959

6060
def run_stubdefaulter(stub_dir: str) -> None:
6161
print(f"Running stubdefaulter: stubdefaulter --packages {stub_dir}")
62-
subprocess.run(["stubdefaulter", "--packages", stub_dir])
62+
subprocess.run(["stubdefaulter", "--packages", stub_dir], check=False)
6363

6464

6565
def run_black(stub_dir: str) -> None:
6666
print(f"Running Black: black {stub_dir}")
67-
subprocess.run(["pre-commit", "run", "black", "--files", *glob.iglob(f"{stub_dir}/**/*.pyi")])
67+
subprocess.run(["pre-commit", "run", "black", "--files", *glob.iglob(f"{stub_dir}/**/*.pyi")], check=False)
6868

6969

7070
def run_ruff(stub_dir: str) -> None:
7171
print(f"Running Ruff: ruff check {stub_dir} --fix-only")
72-
subprocess.run([sys.executable, "-m", "ruff", "check", stub_dir, "--fix-only"])
72+
subprocess.run([sys.executable, "-m", "ruff", "check", stub_dir, "--fix-only"], check=False)
7373

7474

7575
async def get_project_urls_from_pypi(project: str, session: aiohttp.ClientSession) -> dict[str, str]:
@@ -102,9 +102,9 @@ async def get_upstream_repo_url(project: str) -> str | None:
102102
url for url_name, url in project_urls.items() if url_name not in url_names_probably_pointing_to_source
103103
)
104104

105-
for url in urls_to_check:
105+
for url_to_check in urls_to_check:
106106
# Remove `www.`; replace `http://` with `https://`
107-
url = re.sub(r"^(https?://)?(www\.)?", "https://", url)
107+
url = re.sub(r"^(https?://)?(www\.)?", "https://", url_to_check)
108108
netloc = urllib.parse.urlparse(url).netloc
109109
if netloc in {"gitlab.com", "github.com", "bitbucket.org", "foss.heptapod.net"}:
110110
# truncate to https://site.com/user/repo

scripts/stubsabot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,8 @@ async def main() -> None:
754754
dists_to_update = [path.name for path in STUBS_PATH.iterdir()]
755755

756756
if args.action_level > ActionLevel.nothing:
757-
subprocess.run(["git", "update-index", "--refresh"], capture_output=True)
758-
diff_result = subprocess.run(["git", "diff-index", "HEAD", "--name-only"], text=True, capture_output=True)
757+
subprocess.run(["git", "update-index", "--refresh"], capture_output=True, check=False)
758+
diff_result = subprocess.run(["git", "diff-index", "HEAD", "--name-only"], text=True, capture_output=True, check=False)
759759
if diff_result.returncode:
760760
print("Unexpected exception!")
761761
print(diff_result.stdout)

scripts/sync_protobuf/_utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def run_protoc(
3535
) -> str:
3636
"""TODO: Describe parameters and return."""
3737
protoc_version = (
38-
subprocess.run([sys.executable, "-m", "grpc_tools.protoc", "--version"], capture_output=True).stdout.decode().strip()
38+
subprocess.run([sys.executable, "-m", "grpc_tools.protoc", "--version"], capture_output=True, check=False)
39+
.stdout.decode()
40+
.strip()
3941
)
4042
print()
4143
print(protoc_version)

scripts/sync_protobuf/google_protobuf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def main() -> None:
9090
print("Updated protobuf/METADATA.toml")
9191

9292
# Run pre-commit to cleanup the stubs
93-
subprocess.run((sys.executable, "-m", "pre_commit", "run", "--files", *STUBS_FOLDER.rglob("*_pb2.pyi")))
93+
subprocess.run((sys.executable, "-m", "pre_commit", "run", "--files", *STUBS_FOLDER.rglob("*_pb2.pyi")), check=False)
9494

9595

9696
if __name__ == "__main__":

scripts/sync_protobuf/s2clientprotocol.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def main() -> None:
6969
print("Updated s2clientprotocol/METADATA.toml")
7070

7171
# Run pre-commit to cleanup the stubs
72-
subprocess.run((sys.executable, "-m", "pre_commit", "run", "--files", *STUBS_FOLDER.rglob("*_pb2.pyi")))
72+
subprocess.run((sys.executable, "-m", "pre_commit", "run", "--files", *STUBS_FOLDER.rglob("*_pb2.pyi")), check=False)
7373

7474

7575
if __name__ == "__main__":

scripts/sync_protobuf/tensorflow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,15 @@ def post_creation() -> None:
7272

7373
for path in STUBS_FOLDER.rglob("*_pb2.pyi"):
7474
print(f"Fixing imports in '{path}'")
75-
with open(path) as file:
75+
with open(path, encoding="utf-8") as file:
7676
filedata = file.read()
7777

7878
# Replace the target string
7979
filedata = re.sub(TSL_IMPORT_PATTERN, "\\1tensorflow.tsl.", filedata)
8080
filedata = re.sub(XLA_IMPORT_PATTERN, "\\1tensorflow.compiler.xla.", filedata)
8181

8282
# Write the file out again
83-
with open(path, "w") as file:
83+
with open(path, "w", encoding="utf-8") as file:
8484
file.write(filedata)
8585

8686
print()
@@ -137,7 +137,7 @@ def main() -> None:
137137
print("Updated tensorflow/METADATA.toml")
138138

139139
# Run pre-commit to cleanup the stubs
140-
subprocess.run((sys.executable, "-m", "pre_commit", "run", "--files", *STUBS_FOLDER.rglob("*_pb2.pyi")))
140+
subprocess.run((sys.executable, "-m", "pre_commit", "run", "--files", *STUBS_FOLDER.rglob("*_pb2.pyi")), check=False)
141141

142142

143143
if __name__ == "__main__":

tests/mypy_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def run_mypy(
277277
mypy_command = [python_path, "-m", "mypy", *mypy_args]
278278
if args.verbose:
279279
print(colored(f"running {' '.join(mypy_command)}", "blue"))
280-
result = subprocess.run(mypy_command, capture_output=True, text=True, env=env_vars)
280+
result = subprocess.run(mypy_command, capture_output=True, text=True, env=env_vars, check=False)
281281
if result.returncode:
282282
print_error(f"failure (exit code {result.returncode})\n")
283283
if result.stdout:
@@ -286,7 +286,7 @@ def run_mypy(
286286
print_error(result.stderr)
287287
if non_types_dependencies and args.verbose:
288288
print("Ran with the following environment:")
289-
subprocess.run(["uv", "pip", "freeze"], env={**os.environ, "VIRTUAL_ENV": str(venv_dir)})
289+
subprocess.run(["uv", "pip", "freeze"], env={**os.environ, "VIRTUAL_ENV": str(venv_dir)}, check=False)
290290
print()
291291
else:
292292
print_success_msg()

tests/pyright_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def main() -> None:
2424
sys.exit(1)
2525

2626
try:
27-
subprocess.run([npx, "--version"])
27+
subprocess.run([npx, "--version"], check=False)
2828
except OSError:
2929
print("error running npx; is Node.js installed?", file=sys.stderr)
3030
sys.exit(1)
@@ -40,7 +40,7 @@ def main() -> None:
4040
command = [npx, f"pyright@{pyright_version}"] + sys.argv[1:]
4141
print_command(command)
4242

43-
ret = subprocess.run(command).returncode
43+
ret = subprocess.run(command, check=False).returncode
4444
sys.exit(ret)
4545

4646

tests/regr_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def run_testcases(
224224
msg += f"{description}: MYPYPATH not set"
225225
msg += "\n"
226226
verbose_log(msg)
227-
return subprocess.run(mypy_command, capture_output=True, text=True, env=env_vars)
227+
return subprocess.run(mypy_command, capture_output=True, text=True, env=env_vars, check=False)
228228

229229

230230
@dataclass(frozen=True)

tests/runtests.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,18 @@ def main() -> None:
7676
pytype_result: subprocess.CompletedProcess[bytes] | None = None
7777

7878
print("\nRunning pre-commit...")
79-
pre_commit_result = subprocess.run(["pre-commit", "run", "--files", *Path(path).rglob("*")])
79+
pre_commit_result = subprocess.run(["pre-commit", "run", "--files", *Path(path).rglob("*")], check=False)
8080

8181
print("\nRunning check_typeshed_structure.py...")
82-
check_structure_result = subprocess.run([sys.executable, "tests/check_typeshed_structure.py"])
82+
check_structure_result = subprocess.run([sys.executable, "tests/check_typeshed_structure.py"], check=False)
8383

8484
strict_params = _get_strict_params(path)
8585
print(f"\nRunning Pyright ({'stricter' if strict_params else 'base' } configs) for Python {python_version}...")
8686
pyright_result = subprocess.run(
8787
[sys.executable, "tests/pyright_test.py", path, "--pythonversion", python_version, *strict_params],
8888
stderr=subprocess.PIPE,
8989
text=True,
90+
check=False,
9091
)
9192
if re.match(_NPX_ERROR_PATTERN, pyright_result.stderr):
9293
print(_NPX_ERROR_MESSAGE)
@@ -98,16 +99,16 @@ def main() -> None:
9899
pyright_skipped = False
99100

100101
print(f"\nRunning mypy for Python {python_version}...")
101-
mypy_result = subprocess.run([sys.executable, "tests/mypy_test.py", path, "--python-version", python_version])
102+
mypy_result = subprocess.run([sys.executable, "tests/mypy_test.py", path, "--python-version", python_version], check=False)
102103
# If mypy failed, stubtest will fail without any helpful error
103104
if mypy_result.returncode == 0:
104105
if folder == "stdlib":
105106
print("\nRunning stubtest...")
106-
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_stdlib.py", stub])
107+
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_stdlib.py", stub], check=False)
107108
else:
108109
if run_stubtest:
109110
print("\nRunning stubtest...")
110-
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_third_party.py", stub])
111+
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_third_party.py", stub], check=False)
111112
else:
112113
print(
113114
colored(
@@ -122,7 +123,7 @@ def main() -> None:
122123

123124
if find_spec("pytype"):
124125
print("\nRunning pytype...")
125-
pytype_result = subprocess.run([sys.executable, "tests/pytype_test.py", path])
126+
pytype_result = subprocess.run([sys.executable, "tests/pytype_test.py", path], check=False)
126127
else:
127128
print(
128129
colored(
@@ -149,7 +150,7 @@ def main() -> None:
149150
"-p",
150151
_TESTCASES_CONFIG_FILE,
151152
]
152-
pyright_testcases_result = subprocess.run(command, stderr=subprocess.PIPE, text=True)
153+
pyright_testcases_result = subprocess.run(command, stderr=subprocess.PIPE, text=True, check=False)
153154
if re.match(_NPX_ERROR_PATTERN, pyright_testcases_result.stderr):
154155
print(_NPX_ERROR_MESSAGE)
155156
pyright_testcases_returncode = 0
@@ -164,6 +165,7 @@ def main() -> None:
164165
[sys.executable, "tests/regr_test.py", "stdlib" if folder == "stdlib" else stub, "--python-version", python_version],
165166
stderr=subprocess.PIPE,
166167
text=True,
168+
check=False,
167169
)
168170
# No test means they all ran successfully (0 out of 0). Not all 3rd-party stubs have regression tests.
169171
if "No test cases found" in regr_test_result.stderr:

tests/stubtest_third_party.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ def run_stubtest(
147147

148148
print_divider()
149149
print("Python version: ", end="", flush=True)
150-
ret = subprocess.run([sys.executable, "-VV"], capture_output=True)
150+
ret = subprocess.run([sys.executable, "-VV"], capture_output=True, check=False)
151151
print_command_output(ret)
152152

153153
print("\nRan with the following environment:")
154-
ret = subprocess.run([pip_exe, "freeze", "--all"], capture_output=True)
154+
ret = subprocess.run([pip_exe, "freeze", "--all"], capture_output=True, check=False)
155155
print_command_output(ret)
156156
if keep_tmp_dir:
157157
print("Path to virtual environment:", venv_dir, flush=True)
@@ -163,7 +163,7 @@ def run_stubtest(
163163
print()
164164
else:
165165
print(f"Re-running stubtest with --generate-allowlist.\nAdd the following to {main_allowlist_path}:")
166-
ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True)
166+
ret = subprocess.run([*stubtest_cmd, "--generate-allowlist"], env=stubtest_env, capture_output=True, check=False)
167167
print_command_output(ret)
168168

169169
print_divider()

tests/typecheck_typeshed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def run_mypy_as_subprocess(directory: str, platform: str, version: str) -> Retur
7272
"--custom-typeshed-dir",
7373
".",
7474
]
75-
result = subprocess.run(command, capture_output=True, text=True)
75+
result = subprocess.run(command, capture_output=True, text=True, check=False)
7676
if result.stderr:
7777
print_error(result.stderr)
7878
if result.stdout:

0 commit comments

Comments
 (0)