Pyproject Toml Validator
Validate Python project pyproject.toml files against PEP 517/621 rules for project metadata, build system, and tool configurations with detailed reports.
Documentation & Preview
#!/usr/bin/env python3 """Validate pyproject.toml files for Python projects (PEP 517/621)."""
import sys import json import re import os
try: import tomllib except ImportError: tomllib = None
SEVERITIES = {"error": 3, "warning": 2, "info": 1}
VALID_BUILD_BACKENDS = [ "setuptools.build_meta", "flit_core.buildapi", "hatchling.build", "pdm.backend", "poetry.core.masonry.api", "maturin", "scikit_build_core.build", "mesonpy", "whey", ]
SPDX_LICENSES = [ "MIT", "Apache-2.0", "GPL-2.0-only", "GPL-2.0-or-later", "GPL-3.0-only", "GPL-3.0-or-later", "BSD-2-Clause", "BSD-3-Clause", "ISC", "MPL-2.0", "LGPL-2.1-only", "LGPL-2.1-or-later", "LGPL-3.0-only", "LGPL-3.0-or-later", "AGPL-3.0-only", "AGPL-3.0-or-later", "Unlicense", "CC0-1.0", "0BSD", "Artistic-2.0", "BSL-1.0", "ECL-2.0", "PSF-2.0", "Zlib", ]
TROVE_CLASSIFIER_PREFIXES = [ "Development Status", "Environment", "Framework", "Intended Audience", "License", "Natural Language", "Operating System", "Programming Language", "Topic", "Typing", ]
KNOWN_TOOL_SECTIONS = [ "ruff", "mypy", "pytest", "black", "isort", "pylint", "flake8", "coverage", "tox", "bandit", "pyright", "pydocstyle", "yapf", "autopep8", "setuptools", "hatch", "pdm", "poetry", "flit", "cibuildwheel", "towncrier", "bumpversion", "bump2version", "semantic_release", "commitizen", "numpydoc", ]
PROJECT_FIELDS = { "name", "version", "description", "readme", "requires-python", "license", "license-files", "authors", "maintainers", "keywords", "classifiers", "urls", "scripts", "gui-scripts", "entry-points", "dependencies", "optional-dependencies", "dynamic", }
BUILD_SYSTEM_FIELDS = {"requires", "build-backend", "backend-path"}
def parse_toml(path): if tomllib: with open(path, "rb") as f: return tomllib.load(f) with open(path, "r") as f: content = f.read() return simple_toml_parse(content)
def simple_toml_parse(text): result = {} current = result stack = [result] current_key_path = []
for line in text.split("\n"): stripped = line.strip() if not stripped or stripped.startswith("#"): continue
header = re.match(r'^\[([^\[\]]+)\]$', stripped) array_header = re.match(r'^\[\[([^\[\]]+)\]\]$', stripped)
if array_header: parts = [p.strip() for p in array_header.group(1).split(".")] current = result for i, part in enumerate(parts[:-1]): current = current.setdefault(part, {}) key = parts[-1] if key not in current: current[key] = [] entry = {} current[key].append(entry) current = entry current_key_path = parts elif header: parts = [p.strip().strip('"') for p in header.group(1).split(".")] current = result for part in parts: current = current.setdefault(part, {}) current_key_path = parts else: m = re.match(r'^([A-Za-z0-9_\-."]+)\s*=\s*(.+)$', stripped) if m: key = m.group(1).strip().strip('"') val = parse_toml_value(m.group(2).strip()) current[key] = val
return result
def parse_toml_value(val): if val.startswith('"') and val.endswith('"'): return val[1:-1] if val.startswith("'") and val.endswith("'"): return val[1:-1] if val == "true": return True if val == "false": return False if val.startswith("["): inner = val[1:-1].strip() if not inner: return [] items = [] for item in smart_split(inner, ","): item = item.strip() if item: items.append(parse_toml_value(item)) return items if val.startswith("{"): inner = val[1:-1].strip() if not inner: return {} d = {} for pair in smart_split(inner, ","): pair = pair.strip() if "=" in pair: k, v = pair.split("=", 1) d[k.strip().strip('"')] = parse_toml_value(v.strip()) return d try: return int(val) except ValueError: pass try: return float(val) except ValueError: pass return val
def smart_split(text, sep): parts = [] depth = 0 current = [] in_str = None for ch in text: if ch in ('"', "'") and in_str is None: in_str = ch elif ch == in_str: in_str = None elif in_str is None: if ch in ("[", "{"): depth += 1 elif ch in ("]", "}"): depth -= 1 elif ch == sep and depth == 0: parts.append("".join(current)) current = [] continue current.append(ch) if current: parts.append("".join(current)) return parts
def validate_project(data, issues): project = data.get("project", {}) if not project: issues.append(("warning", "missing-project-table", "No [project] table found")) return
if "name" not in project and "name" not in project.get("dynamic", []): issues.append(("error", "missing-name", "[project] must have 'name' field")) elif "name" in project: name = project["name"] if not re.match(r'^a-zA-Z0-9?$', str(name)): issues.append(("error", "invalid-name", f"Project name '{name}' doesn't match PEP 508 naming"))
if "version" not in project and "version" not in project.get("dynamic", []): issues.append(("warning", "missing-version", "[project] should have 'version' or list it in 'dynamic'"))
if "description" not in project: issues.append(("info", "missing-description", "[project] should have 'description'"))
if "requires-python" in project: rp = str(project["requires-python"]) if not re.match(r'^[><=!~]+\s*\d+(\.\d+)*(\s*,\s*[><=!~]+\s*\d+(\.\d+)*)*$', rp): issues.append(("warning", "invalid-requires-python", f"'requires-python' value '{rp}' may be malformed"))
if "license" in project: lic = project["license"] if isinstance(lic, str): if lic not in SPDX_LICENSES and not lic.startswith("LicenseRef-"): issues.append(("info", "unknown-license", f"License '{lic}' not in common SPDX list")) elif isinstance(lic, dict): if "text" not in lic and "file" not in lic: issues.append(("warning", "invalid-license-table", "License table should have 'text' or 'file'"))
if "classifiers" in project: classifiers = project["classifiers"] if isinstance(classifiers, list): for clf in classifiers: if isinstance(clf, str): prefix = clf.split(" :: ")[0] if " :: " in clf else "" if prefix and prefix not in TROVE_CLASSIFIER_PREFIXES: issues.append(("info", "unknown-classifier-prefix", f"Classifier prefix '{prefix}' not recognized"))
if "keywords" in project: kw = project["keywords"] if isinstance(kw, list) and len(kw) > 20: issues.append(("info", "too-many-keywords", f"Found {len(kw)} keywords — consider limiting to ~10-15"))
if "authors" in project: authors = project["authors"] if isinstance(authors, list): for i, author in enumerate(authors): if isinstance(author, dict) and "name" not in author and "email" not in author: issues.append(("warning", "empty-author", f"Author #{i+1} has no 'name' or 'email'"))
if "dependencies" in project: deps = project["dependencies"] if isinstance(deps, list): validate_dependency_list(deps, "dependencies", issues)
if "optional-dependencies" in project: opt = project["optional-dependencies"] if isinstance(opt, dict): for group, deps in opt.items(): if isinstance(deps, list): validate_dependency_list(deps, f"optional-dependencies.{group}", issues)
for key in project: if key not in PROJECT_FIELDS: issues.append(("warning", "unknown-project-field", f"Unknown field '{key}' in [project]"))
if "dynamic" in project: dynamic = project["dynamic"] if isinstance(dynamic, list): for field in dynamic: if field == "name": issues.append(("error", "name-in-dynamic", "'name' cannot be listed in 'dynamic'")) if field not in PROJECT_FIELDS: issues.append(("warning", "unknown-dynamic-field", f"Unknown dynamic field '{field}'")) if field in project and field != "name": issues.append(("warning", "static-and-dynamic", f"Field '{field}' is both static and listed in 'dynamic'"))
def validate_dependency_list(deps, section, issues): seen = {} for dep in deps: if not isinstance(dep, str): continue pkg = re.split(r'[><=!~\[;@\s]', dep)[0].strip().lower() pkg_normalized = re.sub(r'[-_.]+', '-', pkg) if pkg_normalized in seen: issues.append(("warning", "duplicate-dependency", f"Duplicate dependency '{pkg}' in {section} (also at index {seen[pkg_normalized]})")) seen[pkg_normalized] = deps.index(dep)
if dep.strip() == pkg and not re.search(r'[><=!~@]', dep): issues.append(("info", "unpinned-dependency", f"Dependency '{pkg}' in {section} has no version constraint"))
def validate_build_system(data, issues): bs = data.get("build-system", {}) if not bs: issues.append(("warning", "missing-build-system", "No [build-system] table — needed for PEP 517")) return
if "requires" not in bs: issues.append(("error", "missing-build-requires", "[build-system] must have 'requires'")) elif isinstance(bs["requires"], list) and len(bs["requires"]) == 0: issues.append(("error", "empty-build-requires", "[build-system].requires is empty"))
if "build-backend" not in bs: issues.append(("warning", "missing-build-backend", "[build-system] should specify 'build-backend'")) elif isinstance(bs["build-backend"], str): backend = bs["build-backend"] if backend not in VALID_BUILD_BACKENDS: issues.append(("info", "unusual-build-backend", f"Build backend '{backend}' is not a common choice"))
for key in bs: if key not in BUILD_SYSTEM_FIELDS: issues.append(("warning", "unknown-build-system-field", f"Unknown field '{key}' in [build-system]"))
def validate_tool_sections(data, issues): tool = data.get("tool", {}) if not tool: return
for section in tool: if section not in KNOWN_TOOL_SECTIONS: issues.append(("info", "unknown-tool-section", f"Tool section [tool.{section}] not in common tools list"))
if "ruff" in tool: validate_ruff(tool["ruff"], issues)
if "mypy" in tool: validate_mypy(tool["mypy"], issues)
if "pytest" in tool: validate_pytest(tool["pytest"], issues)
if "black" in tool: validate_black(tool["black"], issues)
if "isort" in tool: validate_isort(tool["isort"], issues)
if "black" in tool and "ruff" in tool: ruff_conf = tool["ruff"] if isinstance(ruff_conf, dict): format_conf = ruff_conf.get("format", {}) if isinstance(format_conf, dict) and format_conf: issues.append(("info", "ruff-and-black", "[tool.ruff.format] and [tool.black] both present — may conflict"))
if "isort" in tool and "ruff" in tool: ruff_conf = tool["ruff"] if isinstance(ruff_conf, dict): lint = ruff_conf.get("lint", ruff_co