From 556ae16f3a856c3e7382bc195fe52152b4e62fc0 Mon Sep 17 00:00:00 2001 From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Mon, 30 Dec 2024 00:51:21 -0800 Subject: [PATCH] Some improvements to linting (#18381) --- misc/upload-pypi.py | 2 +- mypy/build.py | 14 ++------------ mypy/checker.py | 15 ++------------- mypy/dmypy_server.py | 4 ++-- mypy/fswatcher.py | 4 ++-- mypy/graph_utils.py | 4 ++-- mypy/semanal_shared.py | 2 +- mypy/stubgenc.py | 2 +- mypy/stubtest.py | 4 ++-- mypy/test/testgraph.py | 2 +- mypyc/codegen/emit.py | 2 +- pyproject.toml | 11 +++++++++-- 12 files changed, 26 insertions(+), 40 deletions(-) diff --git a/misc/upload-pypi.py b/misc/upload-pypi.py index 90ae80da643f..c0ff1b2a075e 100644 --- a/misc/upload-pypi.py +++ b/misc/upload-pypi.py @@ -27,7 +27,7 @@ def is_whl_or_tar(name: str) -> bool: - return name.endswith(".tar.gz") or name.endswith(".whl") + return name.endswith((".tar.gz", ".whl")) def item_ok_for_pypi(name: str) -> bool: diff --git a/mypy/build.py b/mypy/build.py index 884862dcf568..a1a9206367af 100644 --- a/mypy/build.py +++ b/mypy/build.py @@ -25,18 +25,8 @@ import sys import time import types -from collections.abc import Iterator, Mapping, Sequence -from typing import ( - TYPE_CHECKING, - AbstractSet, - Any, - Callable, - ClassVar, - Final, - NamedTuple, - NoReturn, - TextIO, -) +from collections.abc import Iterator, Mapping, Sequence, Set as AbstractSet +from typing import TYPE_CHECKING, Any, Callable, ClassVar, Final, NamedTuple, NoReturn, TextIO from typing_extensions import TypeAlias as _TypeAlias, TypedDict import mypy.semanal_main diff --git a/mypy/checker.py b/mypy/checker.py index 6adf8fe26a0d..2b078f721736 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -4,20 +4,9 @@ import itertools from collections import defaultdict -from collections.abc import Iterable, Iterator, Mapping, Sequence +from collections.abc import Iterable, Iterator, Mapping, Sequence, Set as AbstractSet from contextlib import ExitStack, contextmanager -from typing import ( - AbstractSet, - Callable, - Final, - Generic, - NamedTuple, - Optional, - TypeVar, - Union, - cast, - overload, -) +from typing import Callable, Final, Generic, NamedTuple, Optional, TypeVar, Union, cast, overload from typing_extensions import TypeAlias as _TypeAlias import mypy.checkexpr diff --git a/mypy/dmypy_server.py b/mypy/dmypy_server.py index ee1590a25141..d73487efe3bc 100644 --- a/mypy/dmypy_server.py +++ b/mypy/dmypy_server.py @@ -16,9 +16,9 @@ import sys import time import traceback -from collections.abc import Sequence +from collections.abc import Sequence, Set as AbstractSet from contextlib import redirect_stderr, redirect_stdout -from typing import AbstractSet, Any, Callable, Final +from typing import Any, Callable, Final from typing_extensions import TypeAlias as _TypeAlias import mypy.build diff --git a/mypy/fswatcher.py b/mypy/fswatcher.py index a51b1fa95337..d5873f3a0a99 100644 --- a/mypy/fswatcher.py +++ b/mypy/fswatcher.py @@ -3,8 +3,8 @@ from __future__ import annotations import os -from collections.abc import Iterable -from typing import AbstractSet, NamedTuple +from collections.abc import Iterable, Set as AbstractSet +from typing import NamedTuple from mypy.fscache import FileSystemCache diff --git a/mypy/graph_utils.py b/mypy/graph_utils.py index 9083ed6a12f7..154efcef48a9 100644 --- a/mypy/graph_utils.py +++ b/mypy/graph_utils.py @@ -2,8 +2,8 @@ from __future__ import annotations -from collections.abc import Iterable, Iterator -from typing import AbstractSet, TypeVar +from collections.abc import Iterable, Iterator, Set as AbstractSet +from typing import TypeVar T = TypeVar("T") diff --git a/mypy/semanal_shared.py b/mypy/semanal_shared.py index b7d50e411016..40af5ce81d9e 100644 --- a/mypy/semanal_shared.py +++ b/mypy/semanal_shared.py @@ -453,7 +453,7 @@ def require_bool_literal_argument( api: SemanticAnalyzerInterface | SemanticAnalyzerPluginInterface, expression: Expression, name: str, - default: Literal[True] | Literal[False], + default: Literal[True, False], ) -> bool: ... diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 9895d23ffaab..694be8e4beda 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -203,7 +203,7 @@ def _from_sigs(cls, sigs: list[FunctionSig], is_abstract: bool = False) -> CFunc sigs[0].name, "\n".join(sig.format_sig()[:-4] for sig in sigs), is_abstract ) - def __get__(self) -> None: + def __get__(self) -> None: # noqa: PLE0302 """ This exists to make this object look like a method descriptor and thus return true for CStubGenerator.ismethod() diff --git a/mypy/stubtest.py b/mypy/stubtest.py index 48dc565bfe14..21e8736ff6a7 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -25,11 +25,11 @@ import typing_extensions import warnings from collections import defaultdict -from collections.abc import Iterator +from collections.abc import Iterator, Set as AbstractSet from contextlib import redirect_stderr, redirect_stdout from functools import singledispatch from pathlib import Path -from typing import AbstractSet, Any, Generic, TypeVar, Union +from typing import Any, Generic, TypeVar, Union from typing_extensions import get_origin, is_typeddict import mypy.build diff --git a/mypy/test/testgraph.py b/mypy/test/testgraph.py index 0355e75e8c34..238869f36fdf 100644 --- a/mypy/test/testgraph.py +++ b/mypy/test/testgraph.py @@ -3,7 +3,7 @@ from __future__ import annotations import sys -from typing import AbstractSet +from collections.abc import Set as AbstractSet from mypy.build import BuildManager, BuildSourceSet, State, order_ascc, sorted_components from mypy.errors import Errors diff --git a/mypyc/codegen/emit.py b/mypyc/codegen/emit.py index 97302805fd3b..f6663e6194dc 100644 --- a/mypyc/codegen/emit.py +++ b/mypyc/codegen/emit.py @@ -1034,7 +1034,7 @@ def emit_box( self.emit_line(f"if (unlikely({dest} == NULL))") self.emit_line(" CPyError_OutOfMemory();") # TODO: Fail if dest is None - for i in range(0, len(typ.types)): + for i in range(len(typ.types)): if not typ.is_unboxed: self.emit_line(f"PyTuple_SET_ITEM({dest}, {i}, {src}.f{i}") else: diff --git a/pyproject.toml b/pyproject.toml index 24f13921eaf8..5edbc8a75224 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -102,7 +102,7 @@ force-exclude = ''' [tool.ruff] line-length = 99 -target-version = "py38" +target-version = "py39" fix = true extend-exclude = [ @@ -126,11 +126,13 @@ select = [ "B", # flake8-bugbear "I", # isort "N", # pep8-naming + "PIE", # flake8-pie + "PLE", # pylint error "RUF100", # Unused noqa comments "PGH004", # blanket noqa comments "UP", # pyupgrade "C4", # flake8-comprehensions - "SIM201", "SIM202", # simplify comparisons involving not + "SIM201", "SIM202", "SIM222", "SIM223", # flake8-simplify "ISC001", # implicitly concatenated string "RET501", "RET502", # better return None handling ] @@ -149,7 +151,10 @@ ignore = [ "N806", # UPPER_CASE used for constant local variables "UP031", # Use format specifiers instead of percent format "UP032", # 'f-string always preferable to format' is controversial + "C409", # https://github.com/astral-sh/ruff/issues/12912 + "C420", # reads a little worse. fromkeys predates dict comprehensions "C416", # There are a few cases where it's nice to have names for the dict items + "PIE790", # there's nothing wrong with pass ] unfixable = [ @@ -158,6 +163,8 @@ unfixable = [ "F602", # automatic fix might obscure issue "B018", # automatic fix might obscure issue "UP036", # sometimes it's better to just noqa this + "SIM222", # automatic fix might obscure issue + "SIM223", # automatic fix might obscure issue ] [tool.ruff.lint.per-file-ignores]