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 PLC (Pylint Convention) #13306

Open
wants to merge 5 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
5 changes: 3 additions & 2 deletions .vscode/settings.default.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,14 @@
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.codeActionsOnSave": {
// Let Ruff lint fixes handle imports
"source.organizeImports": "never"
"source.organizeImports": "never",
"source.unusedImports": "never"
}
},
// python.analysis is Pylance (pyright) configurations
"python.analysis.fixAll": [
"source.unusedImports"
// Explicitly omiting "source.convertImportFormat", some stubs use relative imports
// Explicitly omiting "source.unusedImports", Let Ruff lint fixes handle imports
],
"python.analysis.typeshedPaths": [
"${workspaceFolder}"
Expand Down
6 changes: 6 additions & 0 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
"PLC", # Pylint Convention
"RUF", # Ruff-specific and unused-noqa
"UP", # pyupgrade
# Flake8 base rules
Expand Down Expand Up @@ -106,10 +107,15 @@ ignore = [
"B", # flake8-bugbear
# Rules that are out of the control of stub authors:
"F403", # `from . import *` used; unable to detect undefined names
"PLC2701", # Private name import from external module
# Stubs can sometimes re-export entire modules.
# Issues with using a star-imported name will be caught by type-checkers.
"F405", # may be undefined, or defined from star imports
]
"lib/ts_utils/**" = [
# Doesn't affect stubs. The only re-exports we have should be in our local lib ts_utils
"PLC0414", # Import alias does not rename original package
]
# See comment on black's force-exclude config above
"*_pb2.pyi" = [
"E501", # Line too long
Expand Down
44 changes: 22 additions & 22 deletions stdlib/builtins.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@ _T2 = TypeVar("_T2")
_T3 = TypeVar("_T3")
_T4 = TypeVar("_T4")
_T5 = TypeVar("_T5")
_SupportsNextT = TypeVar("_SupportsNextT", bound=SupportsNext[Any], covariant=True)
_SupportsAnextT = TypeVar("_SupportsAnextT", bound=SupportsAnext[Any], covariant=True)
_SupportsNextT_co = TypeVar("_SupportsNextT_co", bound=SupportsNext[Any], covariant=True)
_SupportsAnextT_co = TypeVar("_SupportsAnextT_co", bound=SupportsAnext[Any], covariant=True)
_AwaitableT = TypeVar("_AwaitableT", bound=Awaitable[Any])
_AwaitableT_co = TypeVar("_AwaitableT_co", bound=Awaitable[Any], covariant=True)
_P = ParamSpec("_P")
_StartT = TypeVar("_StartT", covariant=True, default=Any)
_StopT = TypeVar("_StopT", covariant=True, default=Any)
_StepT = TypeVar("_StepT", covariant=True, default=Any)
_StartT_co = TypeVar("_StartT_co", covariant=True, default=Any)
_StopT_co = TypeVar("_StopT_co", covariant=True, default=Any)
_StepT_co = TypeVar("_StepT_co", covariant=True, default=Any)

class object:
__doc__: str | None
Expand Down Expand Up @@ -940,13 +940,13 @@ class bool(int):
def __invert__(self) -> int: ...

@final
class slice(Generic[_StartT, _StopT, _StepT]):
class slice(Generic[_StartT_co, _StopT_co, _StepT_co]):
@property
def start(self) -> _StartT: ...
def start(self) -> _StartT_co: ...
@property
def step(self) -> _StepT: ...
def step(self) -> _StepT_co: ...
@property
def stop(self) -> _StopT: ...
def stop(self) -> _StopT_co: ...
@overload
def __new__(cls, stop: int | None, /) -> slice[int | MaybeNone, int | MaybeNone, int | MaybeNone]: ...
@overload
Expand Down Expand Up @@ -1303,7 +1303,7 @@ class _PathLike(Protocol[AnyStr_co]):
def __fspath__(self) -> AnyStr_co: ...

if sys.version_info >= (3, 10):
def aiter(async_iterable: SupportsAiter[_SupportsAnextT], /) -> _SupportsAnextT: ...
def aiter(async_iterable: SupportsAiter[_SupportsAnextT_co], /) -> _SupportsAnextT_co: ...

class _SupportsSynchronousAnext(Protocol[_AwaitableT_co]):
def __anext__(self) -> _AwaitableT_co: ...
Expand Down Expand Up @@ -1465,7 +1465,7 @@ class _GetItemIterable(Protocol[_T_co]):
def __getitem__(self, i: int, /) -> _T_co: ...

@overload
def iter(object: SupportsIter[_SupportsNextT], /) -> _SupportsNextT: ...
def iter(object: SupportsIter[_SupportsNextT_co], /) -> _SupportsNextT_co: ...
@overload
def iter(object: _GetItemIterable[_T], /) -> Iterator[_T]: ...
@overload
Expand Down Expand Up @@ -1672,17 +1672,17 @@ def print(
*values: object, sep: str | None = " ", end: str | None = "\n", file: _SupportsWriteAndFlush[str] | None = None, flush: bool
) -> None: ...

_E = TypeVar("_E", contravariant=True)
_M = TypeVar("_M", contravariant=True)
_E_contra = TypeVar("_E_contra", contravariant=True)
_M_contra = TypeVar("_M_contra", contravariant=True)

class _SupportsPow2(Protocol[_E, _T_co]):
def __pow__(self, other: _E, /) -> _T_co: ...
class _SupportsPow2(Protocol[_E_contra, _T_co]):
def __pow__(self, other: _E_contra, /) -> _T_co: ...

class _SupportsPow3NoneOnly(Protocol[_E, _T_co]):
def __pow__(self, other: _E, modulo: None = None, /) -> _T_co: ...
class _SupportsPow3NoneOnly(Protocol[_E_contra, _T_co]):
def __pow__(self, other: _E_contra, modulo: None = None, /) -> _T_co: ...

class _SupportsPow3(Protocol[_E, _M, _T_co]):
def __pow__(self, other: _E, modulo: _M, /) -> _T_co: ...
class _SupportsPow3(Protocol[_E_contra, _M_contra, _T_co]):
def __pow__(self, other: _E_contra, modulo: _M_contra, /) -> _T_co: ...

_SupportsSomeKindOfPow = ( # noqa: Y026 # TODO: Use TypeAlias once mypy bugs are fixed
_SupportsPow2[Any, Any] | _SupportsPow3NoneOnly[Any, Any] | _SupportsPow3[Any, Any, Any]
Expand Down Expand Up @@ -1718,11 +1718,11 @@ def pow(base: float, exp: complex | _SupportsSomeKindOfPow, mod: None = None) ->
@overload
def pow(base: complex, exp: complex | _SupportsSomeKindOfPow, mod: None = None) -> complex: ...
@overload
def pow(base: _SupportsPow2[_E, _T_co], exp: _E, mod: None = None) -> _T_co: ... # type: ignore[overload-overlap]
def pow(base: _SupportsPow2[_E_contra, _T_co], exp: _E_contra, mod: None = None) -> _T_co: ... # type: ignore[overload-overlap]
@overload
def pow(base: _SupportsPow3NoneOnly[_E, _T_co], exp: _E, mod: None = None) -> _T_co: ... # type: ignore[overload-overlap]
def pow(base: _SupportsPow3NoneOnly[_E_contra, _T_co], exp: _E_contra, mod: None = None) -> _T_co: ... # type: ignore[overload-overlap]
@overload
def pow(base: _SupportsPow3[_E, _M, _T_co], exp: _E, mod: _M) -> _T_co: ...
def pow(base: _SupportsPow3[_E_contra, _M_contra, _T_co], exp: _E_contra, mod: _M_contra) -> _T_co: ...
@overload
def pow(base: _SupportsSomeKindOfPow, exp: float, mod: None = None) -> Any: ...
@overload
Expand Down
12 changes: 6 additions & 6 deletions stdlib/inspect.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ if sys.version_info >= (3, 11):
_P = ParamSpec("_P")
_T = TypeVar("_T")
_F = TypeVar("_F", bound=Callable[..., Any])
_T_cont = TypeVar("_T_cont", contravariant=True)
_V_cont = TypeVar("_V_cont", contravariant=True)
_T_contra = TypeVar("_T_contra", contravariant=True)
_V_contra = TypeVar("_V_contra", contravariant=True)

#
# Types and members
Expand Down Expand Up @@ -228,11 +228,11 @@ def isasyncgenfunction(obj: Callable[_P, Any]) -> TypeGuard[Callable[_P, AsyncGe
@overload
def isasyncgenfunction(obj: object) -> TypeGuard[Callable[..., AsyncGeneratorType[Any, Any]]]: ...

class _SupportsSet(Protocol[_T_cont, _V_cont]):
def __set__(self, instance: _T_cont, value: _V_cont, /) -> None: ...
class _SupportsSet(Protocol[_T_contra, _V_contra]):
def __set__(self, instance: _T_contra, value: _V_contra, /) -> None: ...

class _SupportsDelete(Protocol[_T_cont]):
def __delete__(self, instance: _T_cont, /) -> None: ...
class _SupportsDelete(Protocol[_T_contra]):
def __delete__(self, instance: _T_contra, /) -> None: ...

def isasyncgen(object: object) -> TypeIs[AsyncGeneratorType[Any, Any]]: ...
def istraceback(object: object) -> TypeIs[TracebackType]: ...
Expand Down
18 changes: 9 additions & 9 deletions stdlib/multiprocessing/connection.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ __all__ = ["Client", "Listener", "Pipe", "wait"]
_Address: TypeAlias = str | tuple[str, int]

# Defaulting to Any to avoid forcing generics on a lot of pre-existing code
_SendT = TypeVar("_SendT", contravariant=True, default=Any)
_RecvT = TypeVar("_RecvT", covariant=True, default=Any)
_SendT_contra = TypeVar("_SendT_contra", contravariant=True, default=Any)
_RecvT_co = TypeVar("_RecvT_co", covariant=True, default=Any)

class _ConnectionBase(Generic[_SendT, _RecvT]):
class _ConnectionBase(Generic[_SendT_contra, _RecvT_co]):
def __init__(self, handle: SupportsIndex, readable: bool = True, writable: bool = True) -> None: ...
@property
def closed(self) -> bool: ... # undocumented
Expand All @@ -26,21 +26,21 @@ class _ConnectionBase(Generic[_SendT, _RecvT]):
def fileno(self) -> int: ...
def close(self) -> None: ...
def send_bytes(self, buf: ReadableBuffer, offset: int = 0, size: int | None = None) -> None: ...
def send(self, obj: _SendT) -> None: ...
def send(self, obj: _SendT_contra) -> None: ...
def recv_bytes(self, maxlength: int | None = None) -> bytes: ...
def recv_bytes_into(self, buf: Any, offset: int = 0) -> int: ...
def recv(self) -> _RecvT: ...
def recv(self) -> _RecvT_co: ...
def poll(self, timeout: float | None = 0.0) -> bool: ...
def __enter__(self) -> Self: ...
def __exit__(
self, exc_type: type[BaseException] | None, exc_value: BaseException | None, exc_tb: TracebackType | None
) -> None: ...
def __del__(self) -> None: ...

class Connection(_ConnectionBase[_SendT, _RecvT]): ...
class Connection(_ConnectionBase[_SendT_contra, _RecvT_co]): ...

if sys.platform == "win32":
class PipeConnection(_ConnectionBase[_SendT, _RecvT]): ...
class PipeConnection(_ConnectionBase[_SendT_contra, _RecvT_co]): ...

class Listener:
def __init__(
Expand All @@ -66,8 +66,8 @@ else:

def answer_challenge(connection: Connection[Any, Any], authkey: bytes) -> None: ...
def wait(
object_list: Iterable[Connection[_SendT, _RecvT] | socket.socket | int], timeout: float | None = None
) -> list[Connection[_SendT, _RecvT] | socket.socket | int]: ...
object_list: Iterable[Connection[_SendT_contra, _RecvT_co] | socket.socket | int], timeout: float | None = None
) -> list[Connection[_SendT_contra, _RecvT_co] | socket.socket | int]: ...
def Client(address: _Address, family: str | None = None, authkey: bytes | None = None) -> Connection[Any, Any]: ...

# N.B. Keep this in sync with multiprocessing.context.BaseContext.Pipe.
Expand Down
14 changes: 7 additions & 7 deletions stdlib/typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,10 @@ class Awaitable(Protocol[_T_co]):
def __await__(self) -> Generator[Any, Any, _T_co]: ...

# Non-default variations to accommodate couroutines, and `AwaitableGenerator` having a 4th type parameter.
_SendT_contra_nd = TypeVar("_SendT_contra_nd", contravariant=True)
_ReturnT_co_nd = TypeVar("_ReturnT_co_nd", covariant=True)
_SendT_nd_contra = TypeVar("_SendT_nd_contra", contravariant=True)
_ReturnT_nd_co = TypeVar("_ReturnT_nd_co", covariant=True)

class Coroutine(Awaitable[_ReturnT_co_nd], Generic[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd]):
class Coroutine(Awaitable[_ReturnT_nd_co], Generic[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co]):
__name__: str
__qualname__: str
@property
Expand All @@ -531,7 +531,7 @@ class Coroutine(Awaitable[_ReturnT_co_nd], Generic[_YieldT_co, _SendT_contra_nd,
@property
def cr_running(self) -> bool: ...
@abstractmethod
def send(self, value: _SendT_contra_nd, /) -> _YieldT_co: ...
def send(self, value: _SendT_nd_contra, /) -> _YieldT_co: ...
@overload
@abstractmethod
def throw(
Expand All @@ -547,9 +547,9 @@ class Coroutine(Awaitable[_ReturnT_co_nd], Generic[_YieldT_co, _SendT_contra_nd,
# The parameters correspond to Generator, but the 4th is the original type.
@type_check_only
class AwaitableGenerator(
Awaitable[_ReturnT_co_nd],
Generator[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd],
Generic[_YieldT_co, _SendT_contra_nd, _ReturnT_co_nd, _S],
Awaitable[_ReturnT_nd_co],
Generator[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co],
Generic[_YieldT_co, _SendT_nd_contra, _ReturnT_nd_co, _S],
metaclass=ABCMeta,
): ...

Expand Down
18 changes: 13 additions & 5 deletions stubs/WTForms/wtforms/validators.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ __all__ = (
"Disabled",
)

_ValuesT = TypeVar("_ValuesT", bound=Collection[Any], contravariant=True)
_ValuesT_contra = TypeVar("_ValuesT_contra", bound=Collection[Any], contravariant=True)

class ValidationError(ValueError):
def __init__(self, message: str = "", *args: object) -> None: ...
Expand Down Expand Up @@ -150,9 +150,13 @@ class AnyOf:
@overload
def __init__(self, values: Collection[Any], message: str | None = None, values_formatter: None = None) -> None: ...
@overload
def __init__(self, values: _ValuesT, message: str | None, values_formatter: Callable[[_ValuesT], str]) -> None: ...
def __init__(
self, values: _ValuesT_contra, message: str | None, values_formatter: Callable[[_ValuesT_contra], str]
) -> None: ...
@overload
def __init__(self, values: _ValuesT, message: str | None = None, *, values_formatter: Callable[[_ValuesT], str]) -> None: ...
def __init__(
self, values: _ValuesT_contra, message: str | None = None, *, values_formatter: Callable[[_ValuesT_contra], str]
) -> None: ...
def __call__(self, form: BaseForm, field: Field) -> None: ...
@staticmethod
def default_values_formatter(values: Iterable[object]) -> str: ...
Expand All @@ -164,9 +168,13 @@ class NoneOf:
@overload
def __init__(self, values: Collection[Any], message: str | None = None, values_formatter: None = None) -> None: ...
@overload
def __init__(self, values: _ValuesT, message: str | None, values_formatter: Callable[[_ValuesT], str]) -> None: ...
def __init__(
self, values: _ValuesT_contra, message: str | None, values_formatter: Callable[[_ValuesT_contra], str]
) -> None: ...
@overload
def __init__(self, values: _ValuesT, message: str | None = None, *, values_formatter: Callable[[_ValuesT], str]) -> None: ...
def __init__(
self, values: _ValuesT_contra, message: str | None = None, *, values_formatter: Callable[[_ValuesT_contra], str]
) -> None: ...
def __call__(self, form: BaseForm, field: Field) -> None: ...
@staticmethod
def default_values_formatter(v: Iterable[object]) -> str: ...
Expand Down
8 changes: 4 additions & 4 deletions stubs/networkx/networkx/algorithms/operators/binary.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ def difference(G, H): ...
@_dispatchable
def symmetric_difference(G, H): ...

_X = TypeVar("_X", bound=Hashable, covariant=True)
_Y = TypeVar("_Y", bound=Hashable, covariant=True)
_X_co = TypeVar("_X_co", bound=Hashable, covariant=True)
_Y_co = TypeVar("_Y_co", bound=Hashable, covariant=True)
# GT = TypeVar('GT', bound=Graph[_Node])
# TODO: This does not handle the cases when graphs of different types are passed which is allowed

@_dispatchable
def compose(G: DiGraph[_X], H: DiGraph[_Y]) -> DiGraph[_X | _Y]: ...
def compose(G: DiGraph[_X_co], H: DiGraph[_Y_co]) -> DiGraph[_X_co | _Y_co]: ...
@_dispatchable
def union(G: DiGraph[_X], H: DiGraph[_Y], rename=()) -> DiGraph[_X | _Y]: ...
def union(G: DiGraph[_X_co], H: DiGraph[_Y_co], rename=()) -> DiGraph[_X_co | _Y_co]: ...
8 changes: 4 additions & 4 deletions stubs/pynput/pynput/_util.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ from typing import Any, ClassVar, Generic, TypedDict, TypeVar
from typing_extensions import ParamSpec, Self

_T = TypeVar("_T")
_AbstractListener_T = TypeVar("_AbstractListener_T", bound=AbstractListener)
_AbstractListenerT = TypeVar("_AbstractListenerT", bound=AbstractListener)
_P = ParamSpec("_P")

class _RESOLUTIONS(TypedDict):
Expand Down Expand Up @@ -49,15 +49,15 @@ class AbstractListener(threading.Thread):
def _stop_platform(self) -> None: ... # undocumented
def join(self, timeout: float | None = None, *args: Any) -> None: ...

class Events(Generic[_T, _AbstractListener_T]):
_Listener: type[_AbstractListener_T] | None # undocumented
class Events(Generic[_T, _AbstractListenerT]):
_Listener: type[_AbstractListenerT] | None # undocumented

class Event:
def __eq__(self, other: object) -> bool: ...

_event_queue: Queue[_T] # undocumented
_sentinel: object # undocumented
_listener: _AbstractListener_T # undocumented
_listener: _AbstractListenerT # undocumented
start: Callable[[], None]
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
def __enter__(self) -> Self: ...
Expand Down
4 changes: 2 additions & 2 deletions stubs/pyserial/serial/tools/miniterm.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ from typing_extensions import Self

from serial import Serial

_AnyStr_T = TypeVar("_AnyStr_T", contravariant=True)
_AnyStrT_contra = TypeVar("_AnyStrT_contra", contravariant=True)

@type_check_only
class _SupportsWriteAndFlush(SupportsWrite[_AnyStr_T], SupportsFlush, Protocol): ...
class _SupportsWriteAndFlush(SupportsWrite[_AnyStrT_contra], SupportsFlush, Protocol): ...

@type_check_only
class _SupportsRead(Protocol):
Expand Down
Loading
Loading