From 94d4d756b69f7e55540ea95944bddc4fd13c1e34 Mon Sep 17 00:00:00 2001 From: Avasam Date: Tue, 30 Jul 2024 13:38:48 -0400 Subject: [PATCH] Disallow blanket suppressions --- mypy.ini | 4 +++- pkg_resources/__init__.py | 2 +- ruff.toml | 2 ++ setuptools/_distutils/compat/py38.py | 2 +- setuptools/command/build_ext.py | 2 +- setuptools/config/expand.py | 16 ++++++++++------ setuptools/config/pyprojecttoml.py | 9 +++++---- setuptools/dist.py | 2 +- .../tests/config/test_apply_pyprojecttoml.py | 2 +- setuptools/tests/config/test_pyprojecttoml.py | 2 +- setuptools/tests/test_config_discovery.py | 2 +- setuptools/tests/test_editable_install.py | 8 ++++---- 12 files changed, 31 insertions(+), 22 deletions(-) diff --git a/mypy.ini b/mypy.ini index 4fba13c286..7d0dad3b38 100644 --- a/mypy.ini +++ b/mypy.ini @@ -19,6 +19,7 @@ exclude = (?x)( ) # Too many false-positives disable_error_code = overload-overlap +enable_error_code = ignore-without-code # Ignoring attr-defined because setuptools wraps a lot of distutils classes, adding new attributes, # w/o updating all the attributes and return types from the base classes for type-checkers to understand @@ -35,7 +36,8 @@ disable_error_code = import-not-found # - All jaraco modules are still untyped # - _validate_project sometimes complains about trove_classifiers (#4296) # - wheel appears to be untyped -[mypy-distutils._modified,jaraco.*,trove_classifiers,wheel.*] +# - _aix_support is untyped +[mypy-distutils._modified,jaraco.*,trove_classifiers,wheel.*,_aix_support] ignore_missing_imports = True # Even when excluding a module, import issues can show up due to following import diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py index 3c88d8d3d7..73d22043bd 100644 --- a/pkg_resources/__init__.py +++ b/pkg_resources/__init__.py @@ -2767,7 +2767,7 @@ def load( if require: # We could pass `env` and `installer` directly, # but keeping `*args` and `**kwargs` for backwards compatibility - self.require(*args, **kwargs) # type: ignore + self.require(*args, **kwargs) # type: ignore[arg-type] return self.resolve() def resolve(self) -> _ResolvedEntryPoint: diff --git a/ruff.toml b/ruff.toml index 09f256d5cb..f926777df1 100644 --- a/ruff.toml +++ b/ruff.toml @@ -18,6 +18,8 @@ extend-select = [ "UP", # pyupgrade "TRY", "YTT", # flake8-2020 + "PGH", # pygrep-hooks (blanket-* rules) + "RUF100", # unused-noqa ] ignore = [ "TRY003", # raise-vanilla-args, avoid multitude of exception classes diff --git a/setuptools/_distutils/compat/py38.py b/setuptools/_distutils/compat/py38.py index 2d44211147..79afc3b295 100644 --- a/setuptools/_distutils/compat/py38.py +++ b/setuptools/_distutils/compat/py38.py @@ -25,7 +25,7 @@ def removeprefix(self, prefix): def aix_platform(osname, version, release): try: - import _aix_support # type: ignore + import _aix_support return _aix_support.aix_platform() except ImportError: diff --git a/setuptools/command/build_ext.py b/setuptools/command/build_ext.py index 508704f3c0..2b62cdc8c8 100644 --- a/setuptools/command/build_ext.py +++ b/setuptools/command/build_ext.py @@ -30,7 +30,7 @@ get_config_var("LDSHARED") # Not publicly exposed in typeshed distutils stubs, but this is done on purpose # See https://github.com/pypa/setuptools/pull/4228#issuecomment-1959856400 -from distutils.sysconfig import _config_vars as _CONFIG_VARS # type: ignore # noqa +from distutils.sysconfig import _config_vars as _CONFIG_VARS # noqa: E402 def _customize_compiler_for_shlib(compiler): diff --git a/setuptools/config/expand.py b/setuptools/config/expand.py index de6339fa42..6ba0847adb 100644 --- a/setuptools/config/expand.py +++ b/setuptools/config/expand.py @@ -209,7 +209,8 @@ def _load_spec(spec: ModuleSpec, module_name: str) -> ModuleType: return sys.modules[name] module = importlib.util.module_from_spec(spec) sys.modules[name] = module # cache (it also ensures `==` works on loaded items) - spec.loader.exec_module(module) # type: ignore + assert spec.loader is not None + spec.loader.exec_module(module) return module @@ -290,10 +291,10 @@ def find_packages( from setuptools.discovery import construct_package_dir from more_itertools import unique_everseen, always_iterable - if namespaces: - from setuptools.discovery import PEP420PackageFinder as PackageFinder + if not namespaces: + from setuptools.discovery import PackageFinder else: - from setuptools.discovery import PackageFinder # type: ignore + from setuptools.discovery import PEP420PackageFinder as PackageFinder root_dir = root_dir or os.curdir where = kwargs.pop('where', ['.']) @@ -357,14 +358,17 @@ def canonic_data_files( ] -def entry_points(text: str, text_source="entry-points") -> dict[str, dict]: +def entry_points( + text: str, text_source: str = "entry-points" +) -> dict[str, dict[str, str]]: """Given the contents of entry-points file, process it into a 2-level dictionary (``dict[str, dict[str, str]]``). The first level keys are entry-point groups, the second level keys are entry-point names, and the second level values are references to objects (that correspond to the entry-point value). """ - parser = ConfigParser(default_section=None, delimiters=("=",)) # type: ignore + # TODO: Explain why passing None is fine here when ConfigParser.default_section expects to be str + parser = ConfigParser(default_section=None, delimiters=("=",)) # type: ignore[call-overload] parser.optionxform = str # case sensitive parser.read_string(text, text_source) groups = {k: dict(v.items()) for k, v in parser.items()} diff --git a/setuptools/config/pyprojecttoml.py b/setuptools/config/pyprojecttoml.py index a83e43bb35..d3bade258c 100644 --- a/setuptools/config/pyprojecttoml.py +++ b/setuptools/config/pyprojecttoml.py @@ -43,8 +43,8 @@ def validate(config: dict, filepath: StrPath) -> bool: trove_classifier = validator.FORMAT_FUNCTIONS.get("trove-classifier") if hasattr(trove_classifier, "_disable_download"): - # Improve reproducibility by default. See issue 31 for validate-pyproject. - trove_classifier._disable_download() # type: ignore + # Improve reproducibility by default. See https://github.com/abravalheri/validate-pyproject/issues/31 + trove_classifier._disable_download() # type: ignore[union-attr] try: return validator.validate(config) @@ -322,7 +322,7 @@ def _obtain_readme(self, dist: Distribution) -> dict[str, str] | None: def _obtain_entry_points( self, dist: Distribution, package_dir: Mapping[str, str] - ) -> dict[str, dict] | None: + ) -> dict[str, dict[str, Any]] | None: fields = ("entry-points", "scripts", "gui-scripts") if not any(field in self.dynamic for field in fields): return None @@ -332,7 +332,8 @@ def _obtain_entry_points( return None groups = _expand.entry_points(text) - expanded = {"entry-points": groups} + # Any is str | dict[str, str], but causes variance issues + expanded: dict[str, dict[str, Any]] = {"entry-points": groups} def _set_scripts(field: str, group: str): if group in groups: diff --git a/setuptools/dist.py b/setuptools/dist.py index 6a29b2b2b7..4d94d3b23d 100644 --- a/setuptools/dist.py +++ b/setuptools/dist.py @@ -28,7 +28,7 @@ from . import _entry_points from . import _reqs -from . import command as _ # noqa -- imported for side-effects +from . import command as _ # noqa: F401 -- imported for side-effects from ._importlib import metadata from .config import setupcfg, pyprojecttoml from .discovery import ConfigDiscovery diff --git a/setuptools/tests/config/test_apply_pyprojecttoml.py b/setuptools/tests/config/test_apply_pyprojecttoml.py index 6650a657d3..4900c5db97 100644 --- a/setuptools/tests/config/test_apply_pyprojecttoml.py +++ b/setuptools/tests/config/test_apply_pyprojecttoml.py @@ -18,7 +18,7 @@ from packaging.metadata import Metadata -import setuptools # noqa ensure monkey patch to metadata +import setuptools # noqa: F401 -- ensure monkey patch to metadata from setuptools.dist import Distribution from setuptools.config import setupcfg, pyprojecttoml from setuptools.config import expand diff --git a/setuptools/tests/config/test_pyprojecttoml.py b/setuptools/tests/config/test_pyprojecttoml.py index bf8cae5a24..23a545c9a7 100644 --- a/setuptools/tests/config/test_pyprojecttoml.py +++ b/setuptools/tests/config/test_pyprojecttoml.py @@ -18,7 +18,7 @@ from setuptools.errors import OptionError -import setuptools # noqa -- force distutils.core to be patched +import setuptools # noqa: F401 -- force distutils.core to be patched import distutils.core EXAMPLE = """ diff --git a/setuptools/tests/test_config_discovery.py b/setuptools/tests/test_config_discovery.py index ff9e672b68..7d1e7151f8 100644 --- a/setuptools/tests/test_config_discovery.py +++ b/setuptools/tests/test_config_discovery.py @@ -8,7 +8,7 @@ from setuptools.discovery import find_package_path, find_parent_package from setuptools.errors import PackageDiscoveryError -import setuptools # noqa -- force distutils.core to be patched +import setuptools # noqa: F401 -- force distutils.core to be patched import distutils.core import pytest diff --git a/setuptools/tests/test_editable_install.py b/setuptools/tests/test_editable_install.py index 24c10a5054..7e56de8a64 100644 --- a/setuptools/tests/test_editable_install.py +++ b/setuptools/tests/test_editable_install.py @@ -878,9 +878,9 @@ class TestOverallBehaviour: "otherfile.py": "", "mypkg": { "__init__.py": "", - "mod1.py": FLAT_LAYOUT["mypkg"]["mod1.py"], # type: ignore + "mod1.py": FLAT_LAYOUT["mypkg"]["mod1.py"], # type: ignore[index] }, - "other": FLAT_LAYOUT["mypkg"]["subpackage"], # type: ignore + "other": FLAT_LAYOUT["mypkg"]["subpackage"], # type: ignore[index] }, "namespace": { "pyproject.toml": dedent(PYPROJECT), @@ -888,8 +888,8 @@ class TestOverallBehaviour: "otherfile.py": "", "src": { "mypkg": { - "mod1.py": FLAT_LAYOUT["mypkg"]["mod1.py"], # type: ignore - "subpackage": FLAT_LAYOUT["mypkg"]["subpackage"], # type: ignore + "mod1.py": FLAT_LAYOUT["mypkg"]["mod1.py"], # type: ignore[index] + "subpackage": FLAT_LAYOUT["mypkg"]["subpackage"], # type: ignore[index] }, }, },