Skip to content

Commit

Permalink
Merge branch 'main' into cflags
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Dec 27, 2024
2 parents b6e88a0 + a8eec20 commit 482877b
Show file tree
Hide file tree
Showing 42 changed files with 276 additions and 241 deletions.
2 changes: 1 addition & 1 deletion conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def _save_cwd():

@pytest.fixture
def distutils_managed_tempdir(request):
from distutils.tests.compat import py38 as os_helper
from distutils.tests.compat import py39 as os_helper

self = request.instance
self.tempdirs = []
Expand Down
27 changes: 25 additions & 2 deletions distutils/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@
in the distutils.command package.
"""

from __future__ import annotations

import logging
import os
import re
import sys
from collections.abc import Callable
from typing import Any, ClassVar, TypeVar, overload

from . import _modified, archive_util, dir_util, file_util, util
from ._log import log
from .errors import DistutilsOptionError

_CommandT = TypeVar("_CommandT", bound="Command")


class Command:
"""Abstract base class for defining command classes, the "worker bees"
Expand Down Expand Up @@ -44,7 +50,14 @@ class Command:
# 'sub_commands' is usually defined at the *end* of a class, because
# predicates can be unbound methods, so they must already have been
# defined. The canonical example is the "install" command.
sub_commands = []
sub_commands: ClassVar[ # Any to work around variance issues
list[tuple[str, Callable[[Any], bool] | None]]
] = []

user_options: ClassVar[
# Specifying both because list is invariant. Avoids mypy override assignment issues
list[tuple[str, str, str]] | list[tuple[str, str | None, str]]
] = []

# -- Creation/initialization methods -------------------------------

Expand Down Expand Up @@ -305,7 +318,17 @@ def get_finalized_command(self, command, create=True):

# XXX rename to 'get_reinitialized_command()'? (should do the
# same in dist.py, if so)
def reinitialize_command(self, command, reinit_subcommands=False):
@overload
def reinitialize_command(
self, command: str, reinit_subcommands: bool = False
) -> Command: ...
@overload
def reinitialize_command(
self, command: _CommandT, reinit_subcommands: bool = False
) -> _CommandT: ...
def reinitialize_command(
self, command: str | Command, reinit_subcommands=False
) -> Command:
return self.distribution.reinitialize_command(command, reinit_subcommands)

def run_command(self, command):
Expand Down
5 changes: 3 additions & 2 deletions distutils/command/bdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import os
import warnings
from typing import ClassVar

from ..core import Command
from ..errors import DistutilsOptionError, DistutilsPlatformError
Expand All @@ -23,7 +24,7 @@ def show_formats():
pretty_printer.print_help("List of available distribution formats:")


class ListCompat(dict):
class ListCompat(dict[str, tuple[str, str]]):
# adapter to allow for Setuptools compatibility in format_commands
def append(self, item):
warnings.warn(
Expand Down Expand Up @@ -70,7 +71,7 @@ class bdist(Command):
]

# The following commands do not take a format option from bdist
no_format_option = ('bdist_rpm',)
no_format_option: ClassVar[tuple[str, ...]] = ('bdist_rpm',)

# This won't do in reality: will need to distinguish RPM-ish Linux,
# Debian-ish Linux, Solaris, FreeBSD, ..., Windows, Mac OS.
Expand Down
3 changes: 2 additions & 1 deletion distutils/command/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def finalize_options(self): # noqa: C901
self.build_temp = os.path.join(self.build_base, 'temp' + plat_specifier)
if self.build_scripts is None:
self.build_scripts = os.path.join(
self.build_base, 'scripts-%d.%d' % sys.version_info[:2]
self.build_base,
f'scripts-{sys.version_info.major}.{sys.version_info.minor}',
)

if self.executable is None and sys.executable:
Expand Down
6 changes: 3 additions & 3 deletions distutils/command/build_clib.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import os
from distutils._log import log
from typing import ClassVar

from ..core import Command
from ..errors import DistutilsSetupError
Expand All @@ -31,7 +32,7 @@ def show_compilers():
class build_clib(Command):
description = "build C/C++ libraries used by Python extensions"

user_options = [
user_options: ClassVar[list[tuple[str, str, str]]] = [
('build-clib=', 'b', "directory to build C/C++ libraries to"),
('build-temp=', 't', "directory to put temporary build by-products"),
('debug', 'g', "compile with debugging information"),
Expand Down Expand Up @@ -138,8 +139,7 @@ def check_library_list(self, libraries):

if '/' in name or (os.sep != '/' and os.sep in name):
raise DistutilsSetupError(
f"bad library name '{lib[0]}': "
"may not contain directory separators"
f"bad library name '{lib[0]}': may not contain directory separators"
)

if not isinstance(build_info, dict):
Expand Down
11 changes: 4 additions & 7 deletions distutils/command/build_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
)
from ..extension import Extension
from ..sysconfig import customize_compiler, get_config_h_filename, get_python_version
from ..util import get_platform, is_mingw, is_freethreaded
from ..util import get_platform, is_freethreaded, is_mingw

# An extension name is just a dot-separated list of Python NAMEs (ie.
# the same as a fully-qualified module name).
Expand Down Expand Up @@ -335,8 +335,7 @@ def run(self): # noqa: C901

# The official Windows free threaded Python installer doesn't set
# Py_GIL_DISABLED because its pyconfig.h is shared with the
# default build, so we need to define it here
# (see pypa/setuptools#4662).
# default build, so define it here (pypa/setuptools#4662).
if os.name == 'nt' and is_freethreaded():
self.compiler.define_macro('Py_GIL_DISABLED', '1')

Expand Down Expand Up @@ -444,8 +443,7 @@ def check_extensions_list(self, extensions): # noqa: C901
for macro in macros:
if not (isinstance(macro, tuple) and len(macro) in (1, 2)):
raise DistutilsSetupError(
"'macros' element of build info dict "
"must be 1- or 2-tuple"
"'macros' element of build info dict must be 1- or 2-tuple"
)
if len(macro) == 1:
ext.undef_macros.append(macro[0])
Expand Down Expand Up @@ -673,8 +671,7 @@ def find_swig(self):
return "swig.exe"
else:
raise DistutilsPlatformError(
"I don't know how to find (much less run) SWIG "
f"on platform '{os.name}'"
f"I don't know how to find (much less run) SWIG on platform '{os.name}'"
)

# -- Name generators -----------------------------------------------
Expand Down
3 changes: 2 additions & 1 deletion distutils/command/build_scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from distutils import sysconfig
from distutils._log import log
from stat import ST_MODE
from typing import ClassVar

from .._modified import newer
from ..core import Command
Expand All @@ -25,7 +26,7 @@
class build_scripts(Command):
description = "\"build\" scripts (copy and fixup #! line)"

user_options = [
user_options: ClassVar[list[tuple[str, str, str]]] = [
('build-dir=', 'd', "directory to \"build\" (copy) to"),
('force', 'f', "forcibly build everything (ignore file timestamps"),
('executable=', 'e', "specify final destination interpreter path"),
Expand Down
8 changes: 3 additions & 5 deletions distutils/command/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import contextlib
from typing import ClassVar

from ..core import Command
from ..errors import DistutilsSetupError
Expand Down Expand Up @@ -41,15 +42,12 @@ class check(Command):
"""This command checks the meta-data of the package."""

description = "perform some checks on the package"
user_options = [
user_options: ClassVar[list[tuple[str, str, str]]] = [
('metadata', 'm', 'Verify meta-data'),
(
'restructuredtext',
'r',
(
'Checks if long string meta-data syntax '
'are reStructuredText-compliant'
),
'Checks if long string meta-data syntax are reStructuredText-compliant',
),
('strict', 's', 'Will exit with an error if a check fails'),
]
Expand Down
8 changes: 4 additions & 4 deletions distutils/command/command_template
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ Implements the Distutils 'x' command.
__revision__ = "$Id$"

from distutils.core import Command
from typing import ClassVar


class x(Command):

# Brief (40-50 characters) description of the command
description = ""

# List of option tuples: long name, short name (None if no short
# name), and help string.
user_options = [('', '',
""),
]
user_options: ClassVar[list[tuple[str, str, str]]] = [
('', '', ""),
]

def initialize_options(self):
self. = None
Expand Down
4 changes: 2 additions & 2 deletions distutils/command/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,8 @@ def finalize_options(self): # noqa: C901
'dist_version': self.distribution.get_version(),
'dist_fullname': self.distribution.get_fullname(),
'py_version': py_version,
'py_version_short': '%d.%d' % sys.version_info[:2],
'py_version_nodot': '%d%d' % sys.version_info[:2],
'py_version_short': f'{sys.version_info.major}.{sys.version_info.minor}',
'py_version_nodot': f'{sys.version_info.major}{sys.version_info.minor}',
'sys_prefix': prefix,
'prefix': prefix,
'sys_exec_prefix': exec_prefix,
Expand Down
5 changes: 2 additions & 3 deletions distutils/command/install_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import functools
import os
from typing import Iterable
from collections.abc import Iterable

from ..core import Command
from ..util import change_root, convert_path
Expand All @@ -22,8 +22,7 @@ class install_data(Command):
(
'install-dir=',
'd',
"base directory for installing data files "
"[default: installation base dir]",
"base directory for installing data files [default: installation base dir]",
),
('root=', None, "install everything relative to this alternate root directory"),
('force', 'f', "force installation (overwrite existing files)"),
Expand Down
11 changes: 5 additions & 6 deletions distutils/command/install_egg_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os
import re
import sys
from typing import ClassVar

from .. import dir_util
from .._log import log
Expand All @@ -18,7 +19,7 @@ class install_egg_info(Command):
"""Install an .egg-info file for the package"""

description = "Install package's PKG-INFO metadata as an .egg-info file"
user_options = [
user_options: ClassVar[list[tuple[str, str, str]]] = [
('install-dir=', 'd', "directory to install to"),
]

Expand All @@ -31,11 +32,9 @@ def basename(self):
Allow basename to be overridden by child class.
Ref pypa/distutils#2.
"""
return "%s-%s-py%d.%d.egg-info" % (
to_filename(safe_name(self.distribution.get_name())),
to_filename(safe_version(self.distribution.get_version())),
*sys.version_info[:2],
)
name = to_filename(safe_name(self.distribution.get_name()))
version = to_filename(safe_version(self.distribution.get_version()))
return f"{name}-{version}-py{sys.version_info.major}.{sys.version_info.minor}.egg-info"

def finalize_options(self):
self.set_undefined_options('install_lib', ('install_dir', 'install_dir'))
Expand Down
4 changes: 3 additions & 1 deletion distutils/command/install_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
Implements the Distutils 'install_headers' command, to install C/C++ header
files to the Python include directory."""

from typing import ClassVar

from ..core import Command


# XXX force is never used
class install_headers(Command):
description = "install C/C++ header files"

user_options = [
user_options: ClassVar[list[tuple[str, str, str]]] = [
('install-dir=', 'd', "directory to install header files to"),
('force', 'f', "force installation (overwrite existing files)"),
]
Expand Down
6 changes: 3 additions & 3 deletions distutils/command/sdist.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from distutils._log import log
from glob import glob
from itertools import filterfalse
from typing import ClassVar

from ..core import Command
from ..errors import DistutilsOptionError, DistutilsTemplateError
Expand Down Expand Up @@ -114,7 +115,7 @@ def checking_metadata(self):

sub_commands = [('check', checking_metadata)]

READMES = ('README', 'README.txt', 'README.rst')
READMES: ClassVar[tuple[str, ...]] = ('README', 'README.txt', 'README.rst')

def initialize_options(self):
# 'template' and 'manifest' are, respectively, the names of
Expand Down Expand Up @@ -362,8 +363,7 @@ def read_template(self):
# convert_path function
except (DistutilsTemplateError, ValueError) as msg:
self.warn(
"%s, line %d: %s"
% (template.filename, template.current_line, msg)
f"{template.filename}, line {int(template.current_line)}: {msg}"
)
finally:
template.close()
Expand Down
4 changes: 1 addition & 3 deletions distutils/compat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

from .py38 import removeprefix


def consolidate_linker_args(args: list[str]) -> list[str] | str:
"""
Expand All @@ -12,4 +10,4 @@ def consolidate_linker_args(args: list[str]) -> list[str] | str:

if not all(arg.startswith('-Wl,') for arg in args):
return args
return '-Wl,' + ','.join(removeprefix(arg, '-Wl,') for arg in args)
return '-Wl,' + ','.join(arg.removeprefix('-Wl,') for arg in args)
34 changes: 0 additions & 34 deletions distutils/compat/py38.py

This file was deleted.

Loading

0 comments on commit 482877b

Please sign in to comment.