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

Fix all mypy issues #3979

Merged
merged 44 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
aaea47d
Fix all mypy issues
Avasam Jul 9, 2023
11fd06c
Ran black
Avasam Jul 9, 2023
8050560
Exclude tox from mypy check
Avasam Oct 1, 2023
765881a
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Oct 1, 2023
ddbf028
Fix all mypy issues again
Avasam Oct 1, 2023
5f287b8
Merge branch 'main' into fix-all-mypy-issues
Avasam Oct 21, 2023
37a2bb1
Address PR comments
Avasam Oct 22, 2023
f0e6bc5
Merge branch 'fix-all-mypy-issues' of https://github.com/Avasam/setup…
Avasam Oct 22, 2023
03afa45
Fix accidental line ending changes
Avasam Oct 22, 2023
dc946aa
Update .gitignore
Avasam Oct 27, 2023
f69eaa1
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Nov 22, 2023
f0697b4
No unused type: ignore
Avasam Nov 22, 2023
baa555b
TypeError: 'ABCMeta' object is not subscriptable
Avasam Nov 22, 2023
c367b9f
Fix RuffError
Avasam Nov 22, 2023
c7ed433
Merge branch 'main' into fix-all-mypy-issues
Avasam Dec 24, 2023
ebdabe4
Merge branch 'main' into fix-all-mypy-issues
Avasam Jan 12, 2024
82265ca
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Jan 24, 2024
7ddd2b2
Merge branch 'fix-all-mypy-issues' of https://github.com/Avasam/setup…
Avasam Jan 24, 2024
84bd5e3
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Jan 24, 2024
f76def7
Fix post-merge mypy issues
Avasam Jan 24, 2024
ace61b4
Merge branch 'main' into fix-all-mypy-issues
Avasam Feb 5, 2024
d114d18
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Feb 9, 2024
818b206
Merge branch 'fix-all-mypy-issues' of https://github.com/Avasam/setup…
Avasam Feb 9, 2024
3ed0dda
Merge branch 'main' into fix-all-mypy-issues
Avasam Feb 14, 2024
024e30e
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Feb 14, 2024
8fbe1bf
RUff format
Avasam Feb 14, 2024
c4dd3c9
Merge branch 'fix-all-mypy-issues' of https://github.com/Avasam/setup…
Avasam Feb 14, 2024
83c2b3d
Ignore more generated files
Avasam Feb 15, 2024
f4e1d21
Disable more mypy errors
Avasam Feb 19, 2024
6b5bbca
Globally ignore attr-defined for now
Avasam Feb 19, 2024
ecac670
Update more comments
Avasam Feb 19, 2024
f3779b1
Address PR comments and fix new exposed typing issues
Avasam Feb 22, 2024
379a041
Comments updates and don't touch vendored
Avasam Feb 22, 2024
555e9ff
Accidentally removed noqa
Avasam Feb 22, 2024
8aa568a
Update setuptools/tests/integration/test_pip_install_sdist.py
Avasam Feb 22, 2024
62a8099
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Feb 22, 2024
b3fde45
Post merge comments
Avasam Feb 22, 2024
8981ed4
Merge branch 'fix-all-mypy-issues' of https://github.com/Avasam/setup…
Avasam Feb 22, 2024
55eeabd
Document that usage of _config_vars is very purposeful Closes #4228
Avasam Feb 23, 2024
e9d79f0
sort nitpick_ignore
Avasam Feb 23, 2024
232bcd4
Make only comment on newline like others
Avasam Feb 23, 2024
7ba7133
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Feb 25, 2024
708fff7
Forgot to re-ignore
Avasam Feb 25, 2024
e1c064f
Merge branch 'main' of https://github.com/pypa/setuptools into fix-al…
Avasam Mar 5, 2024
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
20 changes: 18 additions & 2 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
[mypy]
# CI should test for all versions, local development gets hints for oldest supported
python_version = 3.8
strict = False
warn_unused_ignores = True
# TODO: Not all dependencies are typed. setuptools itself should be typed too
# TODO: Test environment is not yet properly configured to install all imported packages
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
ignore_missing_imports = True
# required to support namespace packages
# https://github.com/python/mypy/issues/14057
# required to support namespace packages: https://github.com/python/mypy/issues/14057
explicit_package_bases = True
exclude = (?x)(
^build/
| ^.tox/
| ^pkg_resources/tests/data/my-test-package-source/setup.py$ # Duplicate module name
| ^.+?/(_vendor|extern)/ # Vendored
| ^setuptools/_distutils/ # Vendored
)

# https://github.com/pypa/setuptools/pull/3979#discussion_r1367968993
[mypy-pkg_resources.extern.*,setuptools.extern.*]
ignore_missing_imports = True
5 changes: 3 additions & 2 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import time
import re
import types
from typing import Optional
import zipfile
import zipimport
import warnings
Expand Down Expand Up @@ -70,8 +71,8 @@
join_continuation,
)

from pkg_resources.extern import platformdirs
from pkg_resources.extern import packaging
from pkg_resources.extern import platformdirs # type: ignore[attr-defined]
from pkg_resources.extern import packaging # type: ignore[attr-defined]

__import__('pkg_resources.extern.packaging.version')
__import__('pkg_resources.extern.packaging.specifiers')
Expand Down
3 changes: 2 additions & 1 deletion pkg_resources/tests/test_pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import stat
import distutils.dist
import distutils.command.install_egg_info
from typing import List

from unittest import mock

Expand All @@ -32,7 +33,7 @@ def __call__(self):


class TestZipProvider:
finalizers = []
finalizers: List[EggRemover] = []

ref_time = datetime.datetime(2013, 5, 12, 13, 25, 0)
"A reference time for a file modification"
Expand Down
2 changes: 1 addition & 1 deletion pkg_resources/tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import itertools

import pytest
from pkg_resources.extern import packaging
from pkg_resources.extern import packaging # type: ignore[attr-defined]

import pkg_resources
from pkg_resources import (
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,6 @@ def _restore_install_lib(self):

if __name__ == '__main__':
# allow setup.py to run from another directory
here and os.chdir(here)
# TODO: Use a proper conditonal statement here
here and os.chdir(here) # type: ignore[func-returns-value]
dist = setuptools.setup(**setup_params)
2 changes: 1 addition & 1 deletion setuptools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def setup(**attrs):
_Command = monkey.get_unpatched(distutils.core.Command)


class Command(_Command):
class Command(_Command): # type: ignore[valid-type, misc] # https://github.com/python/mypy/issues/14458
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
"""
Setuptools internal actions are organized using a *command design pattern*.
This means that each action (or group of closely related actions) executed during
Expand Down
4 changes: 2 additions & 2 deletions setuptools/_importlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ def disable_importlib_metadata_finder(metadata):


if sys.version_info < (3, 10):
from setuptools.extern import importlib_metadata as metadata
from setuptools.extern import importlib_metadata as metadata # type: ignore[attr-defined]

disable_importlib_metadata_finder(metadata)
else:
import importlib.metadata as metadata # noqa: F401


if sys.version_info < (3, 9):
from setuptools.extern import importlib_resources as resources
from setuptools.extern import importlib_resources as resources # type: ignore[attr-defined]
else:
import importlib.resources as resources # noqa: F401
5 changes: 1 addition & 4 deletions setuptools/_normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
"""

import re
from pathlib import Path
from typing import Union

from .extern import packaging
from .extern import packaging # type: ignore[attr-defined]

_Path = Union[str, Path]

# https://packaging.python.org/en/latest/specifications/core-metadata/#name
_VALID_NAME = re.compile(r"^([A-Z0-9]|[A-Z0-9][A-Z0-9._-]*[A-Z0-9])$", re.I)
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/_requirestxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def _prepare(


def _convert_extras_requirements(
extras_require: _StrOrIter,
extras_require: Mapping[str, _StrOrIter],
) -> Mapping[str, _Ordered[Requirement]]:
"""
Convert requirements in `extras_require` of the form
Expand Down
38 changes: 21 additions & 17 deletions setuptools/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,33 @@
import itertools
from importlib.machinery import EXTENSION_SUFFIXES
from importlib.util import cache_from_source as _compiled_file_name
from typing import Dict, Iterator, List, Tuple
from typing import TYPE_CHECKING, Dict, Iterator, List, Tuple
from pathlib import Path

from distutils.command.build_ext import build_ext as _du_build_ext
from distutils.ccompiler import new_compiler
from distutils.sysconfig import customize_compiler, get_config_var
from distutils import log

from setuptools.errors import BaseError
from setuptools.extension import Extension, Library

try:
# Attempt to use Cython for building extensions, if available
from Cython.Distutils.build_ext import build_ext as _build_ext
if TYPE_CHECKING:
from setuptools.dist import Distribution
from distutils.command.build_ext import build_ext as _build_ext
else:
try:
# Attempt to use Cython for building extensions, if available
from Cython.Distutils.build_ext import build_ext as _build_ext

# Additionally, assert that the compiler module will load
# also. Ref #1229.
__import__('Cython.Compiler.Main')
except ImportError:
_build_ext = _du_build_ext
# Additionally, assert that the compiler module will load
# also. Ref #1229.
__import__('Cython.Compiler.Main')
except ImportError:
from distutils.command.build_ext import build_ext as _build_ext
Avasam marked this conversation as resolved.
Show resolved Hide resolved

# make sure _config_vars is initialized
get_config_var("LDSHARED")
from distutils.sysconfig import _config_vars as _CONFIG_VARS # noqa
from distutils.sysconfig import _config_vars as _CONFIG_VARS # type: ignore # noqa # Not publicly exposed in distutils stubs


def _customize_compiler_for_shlib(compiler):
Expand Down Expand Up @@ -81,6 +84,7 @@ def get_abi3_suffix():
class build_ext(_build_ext):
editable_mode: bool = False
inplace: bool = False
distribution: "Distribution"

def run(self):
"""Build extensions in build directory, then copy if --inplace"""
Expand Down Expand Up @@ -127,7 +131,7 @@ def _get_output_mapping(self) -> Iterator[Tuple[str, str]]:
return

build_py = self.get_finalized_command('build_py')
opt = self.get_finalized_command('install_lib').optimize or ""
opt = self.get_finalized_command('install_lib').optimize or "" # type: ignore[attr-defined] # TODO: Fix in distutils stubs

for ext in self.extensions:
inplace_file, regular_file = self._get_inplace_equivalent(build_py, ext)
Expand Down Expand Up @@ -338,7 +342,7 @@ def _write_stub_file(self, stub_file: str, ext: Extension, compile=False):
log.info("writing stub loader for %s to %s", ext._full_name, stub_file)
if compile and os.path.exists(stub_file):
raise BaseError(stub_file + " already exists! Please delete.")
if not self.dry_run:
if not self.dry_run: # type: ignore[attr-defined] # TODO: Fix in distutils stubs
f = open(stub_file, 'w')
f.write(
'\n'.join([
Expand Down Expand Up @@ -373,13 +377,13 @@ def _write_stub_file(self, stub_file: str, ext: Extension, compile=False):
def _compile_and_remove_stub(self, stub_file: str):
from distutils.util import byte_compile

byte_compile([stub_file], optimize=0, force=True, dry_run=self.dry_run)
optimize = self.get_finalized_command('install_lib').optimize
byte_compile([stub_file], optimize=0, force=True, dry_run=self.dry_run) # type: ignore[attr-defined] # TODO: Fix in distutils stubs
optimize = self.get_finalized_command('install_lib').optimize # type: ignore[attr-defined] # TODO: Fix in distutils stubs
if optimize > 0:
byte_compile(
[stub_file], optimize=optimize, force=True, dry_run=self.dry_run
[stub_file], optimize=optimize, force=True, dry_run=self.dry_run # type: ignore[attr-defined] # TODO: Fix in distutils stubs
)
if os.path.exists(stub_file) and not self.dry_run:
if os.path.exists(stub_file) and not self.dry_run: # type: ignore[attr-defined] # TODO: Fix in distutils stubs
os.unlink(stub_file)


Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/build_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def _filter_build_files(self, files: Iterable[str], egg_info: str) -> Iterator[s
This function should filter this case of invalid files out.
"""
build = self.get_finalized_command("build")
build_dirs = (egg_info, self.build_lib, build.build_temp, build.build_base)
build_dirs = (egg_info, self.build_lib, build.build_temp, build.build_base) # type: ignore[attr-defined] # TODO: Fix in distutils stubs
norm_dirs = [os.path.normpath(p) for p in build_dirs if p]

for file in files:
Expand Down
18 changes: 14 additions & 4 deletions setuptools/command/easy_install.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from distutils.command import install
import sys
import os
from typing import TYPE_CHECKING, Dict, List, Optional, Union
import zipimport
import shutil
import tempfile
Expand Down Expand Up @@ -78,6 +79,9 @@
from .._path import ensure_directory
from ..extern.jaraco.text import yield_lines

if TYPE_CHECKING:
_FileDescriptorOrPath = Union[int, str, bytes, os.PathLike[str], os.PathLike[bytes]]
abravalheri marked this conversation as resolved.
Show resolved Hide resolved


# Turn on PEP440Warnings
warnings.filterwarnings("default", category=pkg_resources.PEP440Warning)
Expand Down Expand Up @@ -1766,7 +1770,7 @@ def _wrap_lines(cls, lines):


if os.environ.get('SETUPTOOLS_SYS_PATH_TECHNIQUE', 'raw') == 'rewrite':
PthDistributions = RewritePthDistributions
PthDistributions = RewritePthDistributions # type: ignore[misc] # Overwriting type


def _first_line_re():
Expand Down Expand Up @@ -2016,7 +2020,13 @@ def is_python_script(script_text, filename):
from os import chmod as _chmod
except ImportError:
abravalheri marked this conversation as resolved.
Show resolved Hide resolved
# Jython compatibility
def _chmod(*args):
def _chmod(
path: "_FileDescriptorOrPath",
mode: int,
*,
dir_fd: Optional[int] = None,
follow_symlinks: bool = True,
) -> None:
pass


Expand All @@ -2034,8 +2044,8 @@ class CommandSpec(list):
those passed to Popen.
"""

options = []
split_args = dict()
options: List[str] = []
split_args: Dict[str, bool] = dict()

@classmethod
def best(cls):
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def _run_build_commands(
self._run_install("data")
return files, mapping

def _run_build_subcommands(self):
def _run_build_subcommands(self) -> None:
"""
Issue #3501 indicates that some plugins/customizations might rely on:

Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import setuptools.unicode_utils as unicode_utils
from setuptools.glob import glob

from setuptools.extern import packaging
from setuptools.extern import packaging # type: ignore[attr-defined]
from ..warnings import SetuptoolsDeprecationWarning


Expand Down
3 changes: 2 additions & 1 deletion setuptools/command/rotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from distutils.errors import DistutilsOptionError
import os
import shutil
from typing import List

from setuptools import Command

Expand All @@ -17,7 +18,7 @@ class rotate(Command):
('keep=', 'k', "number of matching distributions to keep"),
]

boolean_options = []
boolean_options: List[str] = []

def initialize_options(self):
self.match = None
Expand Down
2 changes: 1 addition & 1 deletion setuptools/command/upload_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def has_sphinx(self):
and metadata.entry_points(group='distutils.commands', name='build_sphinx')
)

sub_commands = [('build_sphinx', has_sphinx)]
sub_commands = [('build_sphinx', has_sphinx)] # type: ignore[list-item] # TODO: Fix in distutils stubs

def initialize_options(self):
upload.initialize_options(self)
Expand Down
2 changes: 1 addition & 1 deletion setuptools/config/_validate_pyproject/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .error_reporting import detailed_errors, ValidationError
from .extra_validations import EXTRA_VALIDATIONS
from .fastjsonschema_exceptions import JsonSchemaException, JsonSchemaValueException
from .fastjsonschema_validations import validate as _validate
from .fastjsonschema_validations import validate as _validate # type: ignore[attr-defined] # mypy false-positive. Pyright is fine here
Avasam marked this conversation as resolved.
Show resolved Hide resolved

__all__ = [
"validate",
Expand Down
2 changes: 1 addition & 1 deletion setuptools/config/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class StaticModule:
"""Proxy to a module object that avoids executing arbitrary code."""

def __init__(self, name: str, spec: ModuleSpec):
module = ast.parse(pathlib.Path(spec.origin).read_bytes())
module = ast.parse(pathlib.Path(spec.origin).read_bytes()) # type: ignore[arg-type] # Let it raise an error on None
vars(self).update(locals())
del self.self

Expand Down
5 changes: 3 additions & 2 deletions setuptools/config/pyprojecttoml.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

if TYPE_CHECKING:
from setuptools.dist import Distribution # noqa
from typing_extensions import Self

_Path = Union[str, os.PathLike]
_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -271,7 +272,7 @@ def _ensure_previously_set(self, dist: "Distribution", field: str):
def _expand_directive(
self, specifier: str, directive, package_dir: Mapping[str, str]
):
from setuptools.extern.more_itertools import always_iterable # type: ignore
from setuptools.extern.more_itertools import always_iterable

with _ignore_errors(self.ignore_option_errors):
root_dir = self.root_dir
Expand Down Expand Up @@ -401,7 +402,7 @@ def __init__(
self._project_cfg = project_cfg
self._setuptools_cfg = setuptools_cfg

def __enter__(self):
def __enter__(self) -> "Self":
"""When entering the context, the values of ``packages``, ``py_modules`` and
``package_dir`` that are missing in ``dist`` are copied from ``setuptools_cfg``.
"""
Expand Down
4 changes: 2 additions & 2 deletions setuptools/config/setupcfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _apply(
filenames = [*other_files, filepath]

try:
_Distribution.parse_config_files(dist, filenames=filenames)
_Distribution.parse_config_files(dist, filenames=filenames) # type: ignore[arg-type] # TODO: fix in disutils stubs
handlers = parse_configuration(
dist, dist.command_options, ignore_option_errors=ignore_option_errors
)
Expand Down Expand Up @@ -475,7 +475,7 @@ def parse_section(self, section_options):
# Keep silent for a new option may appear anytime.
self[name] = value

def parse(self):
def parse(self) -> None:
"""Parses configuration file items from one
or more related sections.

Expand Down
Loading
Loading