Skip to content

Commit

Permalink
Merge pull request #320 from Avasam/type-script_args
Browse files Browse the repository at this point in the history
Coerce Distribution.script_args to list
  • Loading branch information
jaraco authored Dec 27, 2024
2 parents 22b61d9 + 2a01f31 commit 7a9bbd5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
5 changes: 4 additions & 1 deletion distutils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
really defined in distutils.dist and distutils.cmd.
"""

from __future__ import annotations

import os
import sys
import tokenize
from collections.abc import Iterable

from .cmd import Command
from .debug import DEBUG
Expand Down Expand Up @@ -215,7 +218,7 @@ def run_commands(dist):
return dist


def run_setup(script_name, script_args=None, stop_after="run"):
def run_setup(script_name, script_args: Iterable[str] | None = None, stop_after="run"):
"""Run a setup script in a somewhat controlled environment, and
return the Distribution instance that drives things. This is useful
if you need to find out the distribution meta-data (passed as
Expand Down
4 changes: 3 additions & 1 deletion distutils/dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def __init__(self, attrs=None): # noqa: C901
# and sys.argv[1:], but they can be overridden when the caller is
# not necessarily a setup script run from the command-line.
self.script_name = None
self.script_args = None
self.script_args: list[str] | None = None

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

if self.script_args is not None:
# Coerce any possible iterable from attrs into a list
self.script_args = list(self.script_args)
for arg in self.script_args:
if not arg.startswith('-'):
break
Expand Down
6 changes: 4 additions & 2 deletions distutils/fancy_getopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
* options set attributes of a passed-in object
"""

from __future__ import annotations

import getopt
import re
import string
Expand Down Expand Up @@ -219,7 +221,7 @@ def _grok_option_table(self): # noqa: C901
self.short_opts.append(short)
self.short2long[short[0]] = long

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


def fancy_getopt(options, negative_opt, object, args):
def fancy_getopt(options, negative_opt, object, args: Sequence[str] | None):
parser = FancyGetopt(options)
parser.set_negative_aliases(negative_opt)
return parser.getopt(args, object)
Expand Down
6 changes: 6 additions & 0 deletions distutils/tests/test_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ def test_find_config_files_disable(self, temp_home):
# make sure --no-user-cfg disables the user cfg file
assert len(all_files) - 1 == len(files)

def test_script_args_list_coercion(self):
d = Distribution(attrs={'script_args': ('build', '--no-user-cfg')})

# make sure script_args is a list even if it started as a different iterable
assert d.script_args == ['build', '--no-user-cfg']

@pytest.mark.skipif(
'platform.system() == "Windows"',
reason='Windows does not honor chmod 000',
Expand Down

0 comments on commit 7a9bbd5

Please sign in to comment.