From 9d9887db963e8f3e8e6758e1a3d3d2238a7d1f23 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 26 Dec 2024 19:49:58 -0500 Subject: [PATCH] ClassVar classvar mutables and tuple from typeshed --- distutils/cmd.py | 12 ++++++++++-- distutils/command/bdist.py | 5 +++-- distutils/command/build_clib.py | 3 ++- distutils/command/build_scripts.py | 3 ++- distutils/command/check.py | 3 ++- distutils/command/command_template | 8 ++++---- distutils/command/install_egg_info.py | 3 ++- distutils/command/install_headers.py | 4 +++- distutils/command/sdist.py | 3 ++- distutils/tests/test_dist.py | 3 ++- 10 files changed, 32 insertions(+), 15 deletions(-) diff --git a/distutils/cmd.py b/distutils/cmd.py index 6ffe7bd4..9c6fa656 100644 --- a/distutils/cmd.py +++ b/distutils/cmd.py @@ -10,7 +10,8 @@ import os import re import sys -from typing import TypeVar, overload +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 @@ -49,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 ------------------------------- diff --git a/distutils/command/bdist.py b/distutils/command/bdist.py index f3340751..1ec3c35f 100644 --- a/distutils/command/bdist.py +++ b/distutils/command/bdist.py @@ -5,6 +5,7 @@ import os import warnings +from typing import ClassVar from ..core import Command from ..errors import DistutilsOptionError, DistutilsPlatformError @@ -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( @@ -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. diff --git a/distutils/command/build_clib.py b/distutils/command/build_clib.py index 1305d5bb..3e183276 100644 --- a/distutils/command/build_clib.py +++ b/distutils/command/build_clib.py @@ -16,6 +16,7 @@ import os from distutils._log import log +from typing import ClassVar from ..core import Command from ..errors import DistutilsSetupError @@ -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"), diff --git a/distutils/command/build_scripts.py b/distutils/command/build_scripts.py index 9e5963c2..1c6fd3ca 100644 --- a/distutils/command/build_scripts.py +++ b/distutils/command/build_scripts.py @@ -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 @@ -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"), diff --git a/distutils/command/check.py b/distutils/command/check.py index 1375028e..078c1ce8 100644 --- a/distutils/command/check.py +++ b/distutils/command/check.py @@ -4,6 +4,7 @@ """ import contextlib +from typing import ClassVar from ..core import Command from ..errors import DistutilsSetupError @@ -41,7 +42,7 @@ 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', diff --git a/distutils/command/command_template b/distutils/command/command_template index 6106819d..a4a751ad 100644 --- a/distutils/command/command_template +++ b/distutils/command/command_template @@ -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 diff --git a/distutils/command/install_egg_info.py b/distutils/command/install_egg_info.py index 0baeee7b..230e94ab 100644 --- a/distutils/command/install_egg_info.py +++ b/distutils/command/install_egg_info.py @@ -8,6 +8,7 @@ import os import re import sys +from typing import ClassVar from .. import dir_util from .._log import log @@ -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"), ] diff --git a/distutils/command/install_headers.py b/distutils/command/install_headers.py index fbb3b242..586121e0 100644 --- a/distutils/command/install_headers.py +++ b/distutils/command/install_headers.py @@ -3,6 +3,8 @@ 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 @@ -10,7 +12,7 @@ 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)"), ] diff --git a/distutils/command/sdist.py b/distutils/command/sdist.py index 003e0bf8..acb3a416 100644 --- a/distutils/command/sdist.py +++ b/distutils/command/sdist.py @@ -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 @@ -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 diff --git a/distutils/tests/test_dist.py b/distutils/tests/test_dist.py index 4d78a198..cd07bcd0 100644 --- a/distutils/tests/test_dist.py +++ b/distutils/tests/test_dist.py @@ -13,6 +13,7 @@ from distutils.cmd import Command from distutils.dist import Distribution, fix_help_options from distutils.tests import support +from typing import ClassVar import jaraco.path import pytest @@ -23,7 +24,7 @@ class test_dist(Command): """Sample distutils extension command.""" - user_options = [ + user_options: ClassVar[list[tuple[str, str, str]]] = [ ("sample-option=", "S", "help text"), ]