• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1"""The purpose of this module is implement PEP 621 validations that are
2difficult to express as a JSON Schema (or that are not supported by the current
3JSON Schema library).
4"""
5
6from typing import Mapping, TypeVar
7
8from .fastjsonschema_exceptions import JsonSchemaValueException
9
10T = TypeVar("T", bound=Mapping)
11
12
13class RedefiningStaticFieldAsDynamic(JsonSchemaValueException):
14    """According to PEP 621:
15
16    Build back-ends MUST raise an error if the metadata specifies a field
17    statically as well as being listed in dynamic.
18    """
19
20
21def validate_project_dynamic(pyproject: T) -> T:
22    project_table = pyproject.get("project", {})
23    dynamic = project_table.get("dynamic", [])
24
25    for field in dynamic:
26        if field in project_table:
27            msg = f"You cannot provide a value for `project.{field}` and "
28            msg += "list it under `project.dynamic` at the same time"
29            name = f"data.project.{field}"
30            value = {field: project_table[field], "...": " # ...", "dynamic": dynamic}
31            raise RedefiningStaticFieldAsDynamic(msg, value, name, rule="PEP 621")
32
33    return pyproject
34
35
36EXTRA_VALIDATIONS = (validate_project_dynamic,)
37