Skip to content

Commit

Permalink
Change --min-py-version behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
mxr committed Dec 16, 2022
1 parent acc732d commit 96bdbc1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,13 @@ This will show up on the pypi project page

### set `python_requires`

A few sources are searched for guessing `python_requires`:
If `--min-py-version` is set, it will be used as the source of truth
for `python_requires`.

Otherwise, a few sources are searched for guessing `python_requires`:
- the existing `python_requires` setting itself
- `envlist` in `tox.ini` if present
- python version `classifiers` that are already set
- the `--min-py-version` argument (currently defaulting to `3.7)

### adds python version classifiers

Expand Down
30 changes: 9 additions & 21 deletions setup_cfg_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def _tox_envlist(setup_cfg: str) -> Generator[str, None, None]:


def _python_requires(
setup_cfg: str, *, min_py_version: tuple[int, int],
setup_cfg: str, *, min_py_version: tuple[int, int] | None,
) -> str | None:
cfg = NoTransformConfigParser()
cfg.read(setup_cfg)
Expand All @@ -186,6 +186,9 @@ def _python_requires(
except UnknownVersionError: # assume they know what's up with weird things
return current_value

if min_py_version is not None:
return _format_python_requires(min_py_version, excluded)

for env in _tox_envlist(setup_cfg):
match = TOX_ENV.match(env)
if match:
Expand All @@ -204,10 +207,8 @@ def _python_requires(

if minimum is None:
return None
elif min_py_version > minimum:
return _format_python_requires(min_py_version, excluded)
else:
return _format_python_requires(minimum, excluded)

return _format_python_requires(minimum, excluded)


def _requires(
Expand Down Expand Up @@ -356,7 +357,7 @@ def _natural_sort(items: Sequence[str]) -> list[str]:
def format_file(
filename: str, *,
include_version_classifiers: bool,
min_py_version: tuple[int, int],
min_py_version: tuple[int, int] | None,
max_py_version: tuple[int, int],
) -> bool:
with open(filename) as f:
Expand Down Expand Up @@ -509,29 +510,16 @@ def main(argv: Sequence[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument('filenames', nargs='*')
parser.add_argument('--include-version-classifiers', action='store_true')
parser.add_argument(
'--min-py3-version', type=_ver_type, default=None,
help=argparse.SUPPRESS,
)
parser.add_argument('--min-py-version', type=_ver_type, default=(3, 7))
parser.add_argument('--min-py-version', type=_ver_type, default=None)
parser.add_argument('--max-py-version', type=_ver_type, default=(3, 11))
args = parser.parse_args(argv)

if args.min_py3_version:
print(
'WARNING: setup-cfg-fmt will replace --min-py3-version '
'with --min-py-version in a future release',
)
min_py_version = args.min_py3_version
else:
min_py_version = args.min_py_version

retv = 0
for filename in args.filenames:
if format_file(
filename,
include_version_classifiers=args.include_version_classifiers,
min_py_version=min_py_version,
min_py_version=args.min_py_version,
max_py_version=args.max_py_version,
):
print(f'Rewriting {filename}')
Expand Down
13 changes: 5 additions & 8 deletions tests/setup_cfg_fmt_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ def test_python_requires_left_alone(tmpdir, s):
assert not main((
str(setup_cfg),
'--include-version-classifiers',
'--min-py3-version=3.2',
'--min-py-version=3.2',
))

assert setup_cfg.read() == (
Expand Down Expand Up @@ -600,7 +600,6 @@ def test_guess_python_requires_python2_tox_ini(tmpdir):
assert main((
str(setup_cfg),
'--include-version-classifiers',
'--min-py-version=3.4',
'--max-py-version=3.7',
))

Expand Down Expand Up @@ -633,7 +632,6 @@ def test_guess_python_requires_tox_ini_dashed_name(tmpdir):
assert main((
str(setup_cfg),
'--include-version-classifiers',
'--min-py-version=3.4',
'--max-py-version=3.7',
))

Expand Down Expand Up @@ -664,7 +662,6 @@ def test_guess_python_requires_tox_ini_py310(tmpdir):
assert main((
str(setup_cfg),
'--include-version-classifiers',
'--min-py3-version=3.4',
))

assert setup_cfg.read() == (
Expand Down Expand Up @@ -695,7 +692,7 @@ def test_guess_python_requires_ignores_insufficient_version_envs(tmpdir):
)

assert not main((
str(setup_cfg), '--min-py-version=3.4', '--max-py-version=3.7',
str(setup_cfg), '--max-py-version=3.7',
))

assert setup_cfg.read() == (
Expand All @@ -721,7 +718,6 @@ def test_guess_python_requires_from_classifiers(tmpdir):
assert main((
str(setup_cfg),
'--include-version-classifiers',
'--min-py-version=3.4',
'--max-py-version=3.7',
))

Expand Down Expand Up @@ -841,7 +837,6 @@ def test_python_requires_with_patch_version(tmpdir):
assert main((
str(setup_cfg),
'--include-version-classifiers',
'--min-py-version=3.4',
'--max-py-version=3.8',
))

Expand Down Expand Up @@ -927,12 +922,14 @@ def test_min_py3_version_less_than_minimum(tmpdir):
'classifiers =\n'
' Programming Language :: Python :: 3\n'
' Programming Language :: Python :: 3 :: Only\n'
' Programming Language :: Python :: 3.4\n'
' Programming Language :: Python :: 3.5\n'
' Programming Language :: Python :: 3.6\n'
' Programming Language :: Python :: 3.7\n'
' Programming Language :: Python :: Implementation :: CPython\n'
'\n'
'[options]\n'
'python_requires = >=3.6\n'
'python_requires = >=3.4\n'
)


Expand Down

0 comments on commit 96bdbc1

Please sign in to comment.