API Reference¶
This is pyproject_metadata, a library for working with metadata in the pyproject.toml project table.
Example usage:
from pyproject_metadata import StandardMetadata
metadata = StandardMetadata.from_pyproject(
parsed_pyproject, allow_extra_keys=False, all_errors=True, metadata_version="2.3"
)
pkg_info = metadata.as_rfc822()
with open("METADATA", "wb") as f:
f.write(pkg_info.as_bytes())
ep = metadata.entrypoints.copy()
ep["console_scripts"] = metadata.scripts
ep["gui_scripts"] = metadata.gui_scripts
with open("entry_points.txt", "w", encoding="utf-8") as f:
for group, entries in ep.items():
if entries:
print(f"[{group}]", file=f)
for name, target in entries.items():
print(f"{name} = {target}", file=f)
print(file=f)
- class pyproject_metadata.License(text, file)[source]¶
Bases:
objectThis represents a classic license, which contains text, and optionally a file path. Modern licenses are just SPDX identifiers, which are strings.
- class pyproject_metadata.RFC822Message[source]¶
Bases:
EmailMessageThis is
email.message.EmailMessagewith two small changes: it defaults to our RFC822Policy, and it correctly writes unicode when being called with bytes().Create a new message with RFC822Policy.
- class pyproject_metadata.RFC822Policy(**kw)[source]¶
Bases:
EmailPolicyThis is
email.policy.EmailPolicy, but with a simpleheader_store_parseimplementation that handles multiline values, and some nice defaults.Create new Policy, possibly overriding some defaults.
See class docstring for a list of overridable attributes.
- utf8 = True¶
- mangle_from_ = False¶
- max_line_length = 0¶
- class pyproject_metadata.Readme(text, file, content_type)[source]¶
Bases:
objectThis represents a readme, which contains text and a content type, and optionally a file path.
- class pyproject_metadata.StandardMetadata(name, version=None, description=None, license=None, license_files=None, readme=None, requires_python=None, dependencies=<factory>, optional_dependencies=<factory>, entrypoints=<factory>, authors=<factory>, maintainers=<factory>, urls=<factory>, classifiers=<factory>, keywords=<factory>, scripts=<factory>, gui_scripts=<factory>, import_names=None, import_namespaces=None, dynamic=<factory>, dual_dynamic=<factory>, dynamic_metadata=<factory>, metadata_version=None, all_errors=False)[source]¶
Bases:
objectThis class represents the standard metadata fields for a project. It can be used to read metadata from a pyproject.toml table, validate it, and write it to an RFC822 message or JSON.
- requires_python: SpecifierSet | None = None¶
- dependencies: list[Requirement]¶
- optional_dependencies: dict[str, list[Requirement]]¶
- dynamic: list[Literal['authors', 'classifiers', 'dependencies', 'description', 'entry-points', 'gui-scripts', 'import-names', 'import-namespaces', 'keywords', 'license', 'license-files', 'maintainers', 'optional-dependencies', 'readme', 'requires-python', 'scripts', 'urls', 'version']]¶
This field contains the list of fields declared dynamic in
project.dynamic. A field that is both declared dynamic and explicitly set causes a parsing error.
- dual_dynamic: set[str]¶
Fields that are both declared in
project.dynamicand given a static value in[project](PEP 808). Emitting these as dynamic metadata requires metadata_version 2.6+.
- dynamic_metadata: list[str]¶
This is a list of METADATA fields that can change in between SDist and wheel. Requires metadata_version 2.2+.
- metadata_version: str | None = None¶
This is the target metadata version. If None, it will be computed as a minimum based on the fields set.
- property auto_metadata_version: str¶
This computes the metadata version based on the fields set in the object if
metadata_versionis None.
- classmethod from_pyproject(data, project_dir='.', metadata_version=None, dynamic_metadata=None, *, allow_extra_keys=None, all_errors=False)[source]¶
Read metadata from a pyproject.toml table. This is the main method for creating an instance of this class. It also supports two additional fields:
allow_extra_keysto control what happens when extra keys are present in the pyproject table, andall_errors, to raise all errors in an ExceptionGroup instead of raising the first one.- Return type:
Self
- validate(*, warn=True)[source]¶
Validate metadata for consistency and correctness.
Will also produce warnings if
warnis given. Respectsall_errors. This is called when loading a pyproject.toml, and when making metadata. Checks:metadata_versionis a known version or Nonenameis a valid project namelicense_filescan’t be used with classiclicenseLicense classifiers can’t be used with SPDX license
descriptionis a single line (warning)Extra names in “project.optional-dependencies” should be valid (warning)
licenseis not an SPDX license expression if metadata_version >= 2.4 (warning)License classifiers deprecated for metadata_version >= 2.4 (warning)
licenseis an SPDX license expression if metadata_version >= 2.4license_filesis supported only for metadata_version >= 2.4project_urlcan’t contain keys over 32 charactersimport-name(paces)sis only supported on metadata_version >= 2.5import-name(space)smust be valid names, optionally with; privateimport-namesandimport-namespacescannot overlap.A field that is both static and dynamic metadata requires metadata_version >= 2.6
- Return type:
- pyproject_metadata.extras_build_system(pyproject_table)[source]¶
Return any extra keys in the build-system table.
- pyproject_metadata.extras_project(pyproject_table)[source]¶
Return any extra keys in the project table.
- pyproject_metadata.extras_top_level(pyproject_table)[source]¶
Return any extra keys in the top-level of the pyproject table.
- pyproject_metadata.field_to_metadata(field)[source]¶
Return the METADATA fields that correspond to a project field.
Submodules¶
pyproject_metadata.constants module¶
Constants for the pyproject_metadata package, collected here to make them easy to update. These should be considered mostly private.
pyproject_metadata.errors module¶
This module defines exceptions and error handling utilities. It is the
recommend path to access ConfiguratonError, ConfigurationWarning, and
ExceptionGroup. For backward compatibility, ConfigurationError is
re-exported in the top-level package.
- exception pyproject_metadata.errors.ConfigurationError(msg, *, key=None)[source]¶
Bases:
ExceptionError in the backend metadata.
Has an optional key attribute, which will be non-None if the error is related to a single key in the pyproject.toml file.
Create a new error with a key (can be None).
- exception pyproject_metadata.errors.ConfigurationWarning[source]¶
Bases:
UserWarningWarnings about backend metadata.
- exception pyproject_metadata.errors.ExceptionGroup¶
Bases:
BaseExceptionGroup,Exception
pyproject_metadata.project_table module¶
This module contains type definitions for the tables used in the
pyproject.toml. You should either import this at type-check time only, or
make sure typing_extensions is available for Python 3.10 and below.
Documentation notice: the fields with hyphens are not shown due to a sphinx-autodoc bug.
- class pyproject_metadata.project_table.ContactTable[source]¶
Bases:
TypedDictCan have either name or email.
- class pyproject_metadata.project_table.LicenseTable[source]¶
Bases:
TypedDictCan have either text or file. Legacy.
- class pyproject_metadata.project_table.ProjectTable¶
Bases:
TypedDict- license: LicenseTable | str¶
- readme: str | ReadmeTable¶
- authors: List[ContactTable]¶
- maintainers: List[ContactTable]¶
- class pyproject_metadata.project_table.PyProjectTable¶
Bases:
TypedDict- project: ProjectTable¶
- pyproject_metadata.project_table.to_project_table(data, /, *, collect_errors)[source]¶
Convert a dict to a PyProjectTable, validating types at runtime.
Note that only the types that are affected by a TypedDict are validated; extra keys are ignored. If it throws, it will be an ExceptionGroup with all the ConfigurationTypeError found.
- Return type: