Skip to content

Commit 0945e75

Browse files
committed
Mark abstract base classes and methods
1 parent 08bd311 commit 0945e75

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

newsfragments/4503.feature.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Mark abstract base classes and methods with `abc.ABC` and `abc.abstract_method` -- by :user:`Avasam`

pkg_resources/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
from __future__ import annotations
2424

25+
from abc import ABC
2526
import sys
2627

2728
if sys.version_info < (3, 8): # noqa: UP036 # Check for unsupported versions
@@ -311,7 +312,7 @@ def get_supported_platform():
311312
]
312313

313314

314-
class ResolutionError(Exception):
315+
class ResolutionError(Exception, ABC):
315316
"""Abstract base for dependency resolution errors"""
316317

317318
def __repr__(self):

setuptools/__init__.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Extensions to the 'distutils' for large or complex distributions"""
22

3+
from abc import ABC, abstractmethod
34
import functools
45
import os
56
import re
@@ -117,7 +118,7 @@ def setup(**attrs):
117118
_Command = monkey.get_unpatched(distutils.core.Command)
118119

119120

120-
class Command(_Command):
121+
class Command(_Command, ABC):
121122
"""
122123
Setuptools internal actions are organized using a *command design pattern*.
123124
This means that each action (or group of closely related actions) executed during
@@ -226,6 +227,33 @@ def reinitialize_command(self, command, reinit_subcommands=False, **kw):
226227
vars(cmd).update(kw)
227228
return cmd
228229

230+
@abstractmethod
231+
def initialize_options(self) -> None:
232+
"""
233+
Set or (reset) all options/attributes/caches used by the command
234+
to their default values. Note that these values may be overwritten during
235+
the build.
236+
"""
237+
raise NotImplementedError
238+
239+
@abstractmethod
240+
def finalize_options(self) -> None:
241+
"""
242+
Set final values for all options/attributes used by the command.
243+
Most of the time, each option/attribute/cache should only be set if it does not
244+
have any value yet (e.g. ``if self.attr is None: self.attr = val``).
245+
"""
246+
raise NotImplementedError
247+
248+
@abstractmethod
249+
def run(self) -> None:
250+
"""
251+
Execute the actions intended by the command.
252+
(Side effects **SHOULD** only take place when ``run`` is executed,
253+
for example, creating new files or writing to the terminal output).
254+
"""
255+
raise NotImplementedError
256+
229257

230258
def _find_all_simple(path):
231259
"""

setuptools/command/setopt.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from abc import ABC, abstractmethod
12
from distutils.util import convert_path
23
from distutils import log
34
from distutils.errors import DistutilsOptionError
@@ -68,7 +69,7 @@ def edit_config(filename, settings, dry_run=False):
6869
opts.write(f)
6970

7071

71-
class option_base(Command):
72+
class option_base(Command, ABC):
7273
"""Abstract base class for commands that mess with config files"""
7374

7475
user_options = [
@@ -103,6 +104,10 @@ def finalize_options(self):
103104
)
104105
(self.filename,) = filenames
105106

107+
@abstractmethod
108+
def run(self) -> None:
109+
raise NotImplementedError
110+
106111

107112
class setopt(option_base):
108113
"""Save command-line options to a file"""

setuptools/sandbox.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from abc import ABC
34
import os
45
import sys
56
import tempfile
@@ -265,7 +266,7 @@ def run_setup(setup_script, args):
265266
# Normal exit, just return
266267

267268

268-
class AbstractSandbox:
269+
class AbstractSandbox(ABC):
269270
"""Wrap 'os' module and 'open()' builtin for virtualizing setup scripts"""
270271

271272
_active = False

0 commit comments

Comments
 (0)