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

initial impl for normalized pep440 check #219

Open
wants to merge 10 commits 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
25 changes: 22 additions & 3 deletions src/validate_pyproject/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from contextlib import contextmanager
from itertools import chain
from textwrap import dedent, wrap
from types import MappingProxyType
from typing import (
Callable,
Dict,
Expand All @@ -26,9 +27,9 @@
TypeVar,
)

from . import __version__
from . import __version__, formats
from . import _tomllib as tomllib
from .api import Validator
from .api import FORMAT_FUNCTIONS, Validator
from .errors import ValidationError
from .plugins import PluginWrapper
from .plugins import list_from_entry_points as list_plugins_from_entry_points
Expand Down Expand Up @@ -122,6 +123,7 @@ class CliParams(NamedTuple):
store: str
loglevel: int = logging.WARNING
dump_json: bool = False
normalized_pep440: bool = False


def __meta__(plugins: Sequence[PluginWrapper]) -> Dict[str, dict]:
Expand Down Expand Up @@ -157,13 +159,22 @@ def parse_args(
for cli_opts in get_parser_spec(plugins).values():
parser.add_argument(*cli_opts.pop("flags", ()), **cli_opts)

parser.add_argument(
"--normalized-pep440",
default=False,
action="store_true",
dest="normalized_pep440",
help="require all pep440 to be normalized",
)

parser.set_defaults(loglevel=logging.WARNING)
params = vars(parser.parse_args(args))
enabled = params.pop("enable", ())
disabled = params.pop("disable", ())
params["tool"] = params["tool"] or []
params["store"] = params["store"] or ""
params["plugins"] = select_plugins(plugins, enabled, disabled)
params["normalized_pep440"] = params.pop("normalized_pep440", False)
return params_class(**params) # type: ignore[call-overload, no-any-return]


Expand Down Expand Up @@ -225,7 +236,15 @@ def run(args: Sequence[str] = ()) -> int:
tool_plugins = [RemotePlugin.from_str(t) for t in params.tool]
if params.store:
tool_plugins.extend(load_store(params.store))
validator = Validator(params.plugins, extra_plugins=tool_plugins)
format_validators = FORMAT_FUNCTIONS
if params.normalized_pep440:
format_validators = MappingProxyType(
FORMAT_FUNCTIONS | {"pep440": formats.normalized_pep440}
)

validator = Validator(
params.plugins, extra_plugins=tool_plugins, format_validators=format_validators
)

exceptions = _ExceptionGroup()
for file in params.input_file:
Expand Down
16 changes: 16 additions & 0 deletions src/validate_pyproject/formats.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
import typing
from itertools import chain as _chain

try:
from packaging import version as _version
except ImportError: # pragma: no cover
_version = None # type: ignore[assignment]

if typing.TYPE_CHECKING:
from typing_extensions import Literal

Expand Down Expand Up @@ -57,6 +62,17 @@
VERSION_REGEX = re.compile(r"^\s*" + VERSION_PATTERN + r"\s*$", re.X | re.I)


def normalized_pep440(version: str) -> bool:
"""See :ref:`PyPA's version specification <pypa:version-specifiers>`
(initially introduced in :pep:`440`).
"""
if _version is None:
raise ValueError("--normalized-pep440 flags require packaging installed")
if str(_version.parse(version)) != version:
raise ValueError(f"{version} is not normalized pep440 version")
return True


def pep440(version: str) -> bool:
"""See :ref:`PyPA's version specification <pypa:version-specifiers>`
(initially introduced in :pep:`440`).
Expand Down
1 change: 1 addition & 0 deletions src/validate_pyproject/pre_compile/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class CliParams(NamedTuple):
loglevel: int = logging.WARNING
tool: Sequence[str] = ()
store: str = ""
normalized_pep440: bool = False


def parser_spec(plugins: Sequence[PluginWrapper]) -> Dict[str, dict]:
Expand Down
8 changes: 8 additions & 0 deletions tests/invalid-examples/normalized_pep440/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[build-system]
# AVOID CHANGING REQUIRES: IT WILL BE UPDATED BY PYSCAFFOLD!
requires = ["setuptools>=46.1.0", "setuptools_scm[toml]>=7.1"]
build-backend = "setuptools.build_meta"

[project]
name = 'normalized_pep440'
version = '0.0.0.a0'