Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coerce Distribution.script_args to list #320

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion distutils/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 +216,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 @@ -169,7 +169,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 @@ -269,6 +269,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
7 changes: 4 additions & 3 deletions distutils/fancy_getopt.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
import re
import string
import sys
from typing import Any, Sequence
from collections.abc import Sequence
from typing import Any

from .errors import DistutilsArgError, DistutilsGetoptError

Expand Down Expand Up @@ -219,7 +220,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 +376,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
Loading