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

Change --min-py-version behavior & always set python_requires #179

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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,14 @@ 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`)
- the default for the `--min-py-version` argument (currently `3.7`)

### adds python version classifiers

Expand Down
42 changes: 17 additions & 25 deletions setup_cfg_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def _v(x: Version) -> str:

def _parse_python_requires(
python_requires: str | None,
) -> tuple[Version | None, set[Version]]:
) -> tuple[Version, set[Version]]:
minimum = None
excluded = set()

Expand All @@ -155,7 +155,7 @@ def _parse_python_requires(
else:
raise UnknownVersionError()

return minimum, excluded
return minimum or (3, 7), excluded


def _tox_envlist(setup_cfg: str) -> Generator[str, None, None]:
Expand All @@ -175,8 +175,8 @@ def _tox_envlist(setup_cfg: str) -> Generator[str, None, None]:


def _python_requires(
setup_cfg: str, *, min_py_version: tuple[int, int],
) -> str | None:
setup_cfg: str, *, min_py_version: tuple[int, int] | None,
) -> str:
cfg = NoTransformConfigParser()
cfg.read(setup_cfg)
current_value = cfg.get('options', 'python_requires', fallback='')
Expand All @@ -187,11 +187,14 @@ 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:
version = _to_ver(f'3.{match[1]}')
if minimum is None or version < minimum[:2]:
if version < minimum[:2]:
minimum = version

for classifier in classifiers.strip().splitlines():
Expand All @@ -200,15 +203,10 @@ def _python_requires(
if '.' not in version_part:
continue
version = _to_ver(version_part)
if minimum is None or version < minimum[:2]:
if version < minimum[:2]:
minimum = version

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)
mxr marked this conversation as resolved.
Show resolved Hide resolved
return _format_python_requires(minimum, excluded)


def _requires(
Expand Down Expand Up @@ -274,11 +272,8 @@ def _py_classifiers(
except UnknownVersionError:
return None

if minimum is None: # don't have a sequence of versions to iterate over
return None
else:
# classifiers only use the first two segments of version
minimum = minimum[:2]
# classifiers only use the first two segments of version
minimum = minimum[:2]

versions: set[Version] = set()
while minimum <= max_py_version:
Expand Down Expand Up @@ -310,8 +305,6 @@ def _trim_py_classifiers(
def _is_ok_classifier(s: str) -> bool:
parts = s.split(' :: ')
if (
# can't know if it applies without a minimum
minimum is None or
# handle Python :: 3 :: Only
len(parts) != 3 or
not s.startswith('Programming Language :: Python :: ')
Expand Down Expand Up @@ -357,7 +350,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 @@ -405,10 +398,9 @@ def format_file(
)

requires = _python_requires(filename, min_py_version=min_py_version)
if requires is not None:
if not cfg.has_section('options'):
cfg.add_section('options')
cfg['options']['python_requires'] = requires
if not cfg.has_section('options'):
cfg.add_section('options')
cfg['options']['python_requires'] = requires

install_requires = _requires(cfg, 'install_requires')
if install_requires:
Expand Down Expand Up @@ -514,7 +506,7 @@ def main(argv: Sequence[str] | None = None) -> int:
'--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)

Expand Down
Loading