extra_validations.py 1.1 KB

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