Skip to content

Commit 7a9bbd5

Browse files
authored
Merge pull request #320 from Avasam/type-script_args
Coerce Distribution.script_args to list
2 parents 22b61d9 + 2a01f31 commit 7a9bbd5

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

distutils/core.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
really defined in distutils.dist and distutils.cmd.
77
"""
88

9+
from __future__ import annotations
10+
911
import os
1012
import sys
1113
import tokenize
14+
from collections.abc import Iterable
1215

1316
from .cmd import Command
1417
from .debug import DEBUG
@@ -215,7 +218,7 @@ def run_commands(dist):
215218
return dist
216219

217220

218-
def run_setup(script_name, script_args=None, stop_after="run"):
221+
def run_setup(script_name, script_args: Iterable[str] | None = None, stop_after="run"):
219222
"""Run a setup script in a somewhat controlled environment, and
220223
return the Distribution instance that drives things. This is useful
221224
if you need to find out the distribution meta-data (passed as

distutils/dist.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def __init__(self, attrs=None): # noqa: C901
177177
# and sys.argv[1:], but they can be overridden when the caller is
178178
# not necessarily a setup script run from the command-line.
179179
self.script_name = None
180-
self.script_args = None
180+
self.script_args: list[str] | None = None
181181

182182
# 'command_options' is where we store command options between
183183
# parsing them (from config files, the command-line, etc.) and when
@@ -277,6 +277,8 @@ def __init__(self, attrs=None): # noqa: C901
277277
self.want_user_cfg = True
278278

279279
if self.script_args is not None:
280+
# Coerce any possible iterable from attrs into a list
281+
self.script_args = list(self.script_args)
280282
for arg in self.script_args:
281283
if not arg.startswith('-'):
282284
break

distutils/fancy_getopt.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* options set attributes of a passed-in object
99
"""
1010

11+
from __future__ import annotations
12+
1113
import getopt
1214
import re
1315
import string
@@ -219,7 +221,7 @@ def _grok_option_table(self): # noqa: C901
219221
self.short_opts.append(short)
220222
self.short2long[short[0]] = long
221223

222-
def getopt(self, args=None, object=None): # noqa: C901
224+
def getopt(self, args: Sequence[str] | None = None, object=None): # noqa: C901
223225
"""Parse command-line options in args. Store as attributes on object.
224226
225227
If 'args' is None or not supplied, uses 'sys.argv[1:]'. If
@@ -375,7 +377,7 @@ def print_help(self, header=None, file=None):
375377
file.write(line + "\n")
376378

377379

378-
def fancy_getopt(options, negative_opt, object, args):
380+
def fancy_getopt(options, negative_opt, object, args: Sequence[str] | None):
379381
parser = FancyGetopt(options)
380382
parser.set_negative_aliases(negative_opt)
381383
return parser.getopt(args, object)

distutils/tests/test_dist.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,12 @@ def test_find_config_files_disable(self, temp_home):
246246
# make sure --no-user-cfg disables the user cfg file
247247
assert len(all_files) - 1 == len(files)
248248

249+
def test_script_args_list_coercion(self):
250+
d = Distribution(attrs={'script_args': ('build', '--no-user-cfg')})
251+
252+
# make sure script_args is a list even if it started as a different iterable
253+
assert d.script_args == ['build', '--no-user-cfg']
254+
249255
@pytest.mark.skipif(
250256
'platform.system() == "Windows"',
251257
reason='Windows does not honor chmod 000',

0 commit comments

Comments
 (0)