From 18e1a7157562b00655561ee25ce0b41d383b2ed1 Mon Sep 17 00:00:00 2001 From: Jake Lishman Date: Fri, 22 Sep 2023 14:53:08 +0100 Subject: [PATCH] Reduce use of deprecated `distutils` module Per PEP 632, `distutils` is deprecated and most uses of it can be replaced by `setuptools`. The only use within `testtools` is to support the deprecated `TestCommand` that's due to be removed in the next major, but in the meantime, adding a runtime dependency on `setuptools` for Python 3.12+ (which removed `distutils` entirely) allows it to continue working. --- setup.cfg | 5 +++++ testtools/distutilscmd.py | 12 ++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index b988065c..b73fa3e1 100644 --- a/setup.cfg +++ b/setup.cfg @@ -25,6 +25,11 @@ classifier = Topic :: Software Development :: Libraries :: Python Modules Topic :: Software Development :: Testing +[options] +install_requires = + # Purely to support the deprecated `TestCommand` until 3.0. + setuptools; python_version>='3.12' + [extras] test = testscenarios diff --git a/testtools/distutilscmd.py b/testtools/distutilscmd.py index eedc4ff2..daf761fa 100644 --- a/testtools/distutilscmd.py +++ b/testtools/distutilscmd.py @@ -5,8 +5,12 @@ import sys import warnings -from distutils.core import Command -from distutils.errors import DistutilsOptionError +try: + from setuptools import Command + from setuptools.errors import OptionError +except ModuleNotFoundError: + from distutils.core import Command + from distutils.errors import DistutilsOptionError as OptionError from testtools.run import TestProgram, TestToolsTestRunner @@ -47,12 +51,12 @@ def initialize_options(self): def finalize_options(self): if self.test_suite is None: if self.test_module is None: - raise DistutilsOptionError( + raise OptionError( "You must specify a module or a suite to run tests from") else: self.test_suite = self.test_module+".test_suite" elif self.test_module: - raise DistutilsOptionError( + raise OptionError( "You may specify a module or a suite, but not both") self.test_args = [self.test_suite] if self.verbose: