Skip to content
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

Enable Ruff PLR (Pylint Refactor) #13307

Merged
merged 14 commits into from
Mar 3, 2025
Merged
2 changes: 1 addition & 1 deletion lib/ts_utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def read_metadata(distribution: str) -> StubMetadata:
f"Invalid upstream_repository for {distribution!r}: "
"URLs for GitHub repositories always have two parts in their paths"
)
assert num_url_path_parts == 2, bad_github_url_msg
assert num_url_path_parts == 2, bad_github_url_msg # noqa: PLR2004 # astral-sh/ruff#10009

obsolete_since: object = data.get("obsolete_since")
assert isinstance(obsolete_since, (str, type(None)))
Expand Down
6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ select = [
"FA", # flake8-future-annotations
"I", # isort
"PGH", # pygrep-hooks
"PLR", # Pylint Refactor
"RUF", # Ruff-specific and unused-noqa
"UP", # pyupgrade
# Flake8 base rules
Expand Down Expand Up @@ -87,11 +88,12 @@ ignore = [
###
# Rules we don't want or don't agree with
###
# Slower and more verbose https://github.com/astral-sh/ruff/issues/7871
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
# Used for direct, non-subclass type comparison, for example: `type(val) is str`
# see https://github.com/astral-sh/ruff/issues/6465
"E721", # Do not compare types, use `isinstance()`
"PLR09", # Too many ...
# Slower and more verbose https://github.com/astral-sh/ruff/issues/7871
"UP038", # Use `X | Y` in `isinstance` call instead of `(X, Y)`
###
# False-positives, but already checked by type-checkers
###
Expand Down
5 changes: 3 additions & 2 deletions scripts/create_baseline_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import subprocess
import sys
import urllib.parse
from http import HTTPStatus
from importlib.metadata import distribution

import aiohttp
Expand Down Expand Up @@ -72,7 +73,7 @@ def run_ruff(stub_dir: str) -> None:
async def get_project_urls_from_pypi(project: str, session: aiohttp.ClientSession) -> dict[str, str]:
pypi_root = f"https://pypi.org/pypi/{urllib.parse.quote(project)}"
async with session.get(f"{pypi_root}/json") as response:
if response.status != 200:
if response.status != HTTPStatus.OK:
return {}
j: dict[str, dict[str, dict[str, str]]]
j = await response.json()
Expand Down Expand Up @@ -107,7 +108,7 @@ async def get_upstream_repo_url(project: str) -> str | None:
# truncate to https://site.com/user/repo
upstream_repo_url = "/".join(url.split("/")[:5])
async with session.get(upstream_repo_url) as response:
if response.status == 200:
if response.status == HTTPStatus.OK:
return upstream_repo_url
return None

Expand Down
4 changes: 2 additions & 2 deletions scripts/stubsabot.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,10 @@ async def get_github_repo_info(session: aiohttp.ClientSession, stub_info: StubMe
split_url = urllib.parse.urlsplit(stub_info.upstream_repository)
if split_url.netloc == "github.com":
url_path = split_url.path.strip("/")
assert len(Path(url_path).parts) == 2
assert len(Path(url_path).parts) == 2 # noqa: PLR2004 # astral-sh/ruff#10009
github_tags_info_url = f"https://api.github.com/repos/{url_path}/tags"
async with session.get(github_tags_info_url, headers=get_github_api_headers()) as response:
if response.status == 200:
if response.status == HTTPStatus.OK:
tags: list[dict[str, Any]] = await response.json()
assert isinstance(tags, list)
return GitHubInfo(repo_path=url_path, tags=tags)
Expand Down
4 changes: 2 additions & 2 deletions stdlib/importlib/readers.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ from typing import Literal, NoReturn, TypeVar
from typing_extensions import Never

if sys.version_info >= (3, 11):
import importlib.resources.abc as abc
from importlib.resources import abc
else:
import importlib.abc as abc
from importlib import abc

if sys.version_info >= (3, 10):
if sys.version_info >= (3, 11):
Expand Down
31 changes: 15 additions & 16 deletions stdlib/mmap.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,22 @@ PAGESIZE: int
class mmap:
if sys.platform == "win32":
def __init__(self, fileno: int, length: int, tagname: str | None = ..., access: int = ..., offset: int = ...) -> None: ...
elif sys.version_info >= (3, 13):
def __new__(
cls,
fileno: int,
length: int,
flags: int = ...,
prot: int = ...,
access: int = ...,
offset: int = ...,
*,
trackfd: bool = True,
) -> Self: ...
else:
if sys.version_info >= (3, 13):
def __new__(
cls,
fileno: int,
length: int,
flags: int = ...,
prot: int = ...,
access: int = ...,
offset: int = ...,
*,
trackfd: bool = True,
) -> Self: ...
else:
def __new__(
cls, fileno: int, length: int, flags: int = ..., prot: int = ..., access: int = ..., offset: int = ...
) -> Self: ...
def __new__(
cls, fileno: int, length: int, flags: int = ..., prot: int = ..., access: int = ..., offset: int = ...
) -> Self: ...

def close(self) -> None: ...
def flush(self, offset: int = ..., size: int = ...) -> None: ...
Expand Down
2 changes: 1 addition & 1 deletion stubs/gevent/gevent/libev/corecext.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ from types import TracebackType
from typing import Any
from typing_extensions import ParamSpec

import gevent.libev.watcher as watcher
from gevent._ffi.loop import _ErrorHandler
from gevent._types import _Callback
from gevent.libev import watcher

# this c extension is only available on posix
if sys.platform != "win32":
Expand Down
2 changes: 1 addition & 1 deletion stubs/gevent/gevent/libev/corecffi.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import sys
from _typeshed import FileDescriptor
from collections.abc import Sequence

import gevent.libev.watcher as watcher
from gevent._ffi.loop import AbstractLoop
from gevent.libev import watcher

def get_version() -> str: ...
def get_header_version() -> str: ...
Expand Down
2 changes: 1 addition & 1 deletion stubs/gevent/gevent/libuv/loop.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import sys
from _typeshed import FileDescriptor
from typing import NamedTuple

import gevent.libuv.watcher as watcher
from gevent._ffi.loop import AbstractLoop
from gevent._types import _IoWatcher
from gevent.libuv import watcher

def get_version() -> str: ...
def get_header_version() -> str: ...
Expand Down
2 changes: 1 addition & 1 deletion tests/mypy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class TestConfig:


def log(args: TestConfig, *varargs: object) -> None:
if args.verbose >= 2:
if args.verbose >= 2: # noqa: PLR2004 # astral-sh/ruff#10009
print(colored(" ".join(map(str, varargs)), "blue"))


Expand Down
23 changes: 11 additions & 12 deletions tests/runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def main() -> None:
python_version: str = args.python_version

path_tokens = Path(path).parts
if len(path_tokens) != 2:
if len(path_tokens) != 2: # noqa: PLR2004 # astral-sh/ruff#10009
parser.error("'path' argument should be in format <folder>/<stub>.")
folder, stub = path_tokens
if folder not in {"stdlib", "stubs"}:
Expand Down Expand Up @@ -104,19 +104,18 @@ def main() -> None:
if folder == "stdlib":
print("\nRunning stubtest...")
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_stdlib.py", stub])
elif run_stubtest:
print("\nRunning stubtest...")
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_third_party.py", stub])
else:
if run_stubtest:
print("\nRunning stubtest...")
stubtest_result = subprocess.run([sys.executable, "tests/stubtest_third_party.py", stub])
else:
print(
colored(
f"\nSkipping stubtest for {stub!r}..."
+ "\nNOTE: Running third-party stubtest involves downloading and executing arbitrary code from PyPI."
+ f"\nOnly run stubtest if you trust the {stub!r} package.",
"yellow",
)
print(
colored(
f"\nSkipping stubtest for {stub!r}..."
+ "\nNOTE: Running third-party stubtest involves downloading and executing arbitrary code from PyPI."
+ f"\nOnly run stubtest if you trust the {stub!r} package.",
"yellow",
)
)
else:
print(colored("\nSkipping stubtest since mypy failed.", "yellow"))

Expand Down
Loading