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

feat: pyproject-metadata #847

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
tests: tests from pyproject-metadata
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Oct 25, 2024
commit b75d7736f65b35c7af5d5cb810f874663e7e872b
6 changes: 1 addition & 5 deletions src/packaging/_pyproject.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from .errors import ErrorCollector

if typing.TYPE_CHECKING:
if typing.TYPE_CHECKING: # pragma: no cover
from collections.abc import Generator, Iterable, Sequence

from .project_table import ContactTable, Dynamic, ProjectTable
Expand All @@ -26,10 +26,6 @@
]


def __dir__() -> list[str]:
return __all__


@dataclasses.dataclass(frozen=True)
class License:
"""
Expand Down
7 changes: 4 additions & 3 deletions src/packaging/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import dataclasses
import sys
from collections.abc import Generator
from typing import Any, NoReturn
from typing import Any

__all__ = ["ExceptionGroup", "ConfigurationError", "ConfigurationWarning"]

Expand Down Expand Up @@ -75,9 +75,10 @@ def config_error(

self.errors.append(ConfigurationError(msg, key=key))

def finalize(self, msg: str) -> NoReturn:
def finalize(self, msg: str) -> None:
"""Raise a group exception if there are any errors."""
raise ExceptionGroup(msg, self.errors)
if self.errors:
raise ExceptionGroup(msg, self.errors)

@contextlib.contextmanager
def collect(self) -> Generator[None, None, None]:
Expand Down
13 changes: 7 additions & 6 deletions src/packaging/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import sys
import typing
import warnings
from collections.abc import Sequence

from . import markers, specifiers, utils
from . import metadata as packaging_metadata
from . import version as packaging_version
from ._pyproject import License, PyProjectReader, Readme
from .errors import ConfigurationError, ConfigurationWarning, ErrorCollector

if typing.TYPE_CHECKING:
if typing.TYPE_CHECKING: # pragma: no cover
from collections.abc import Mapping
from typing import Any

Expand Down Expand Up @@ -143,7 +144,7 @@ def from_pyproject(
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_keys`` to control what happens when extra keys are
present in the pyproject table, and ``all_errors``, to raise all errors
present in the pyproject table, and ``all_errors``, to raise all errors
in an ExceptionGroup instead of raising the first one.
"""
pyproject = PyProjectReader()
Expand Down Expand Up @@ -368,7 +369,7 @@ def validate(self) -> None:
errors.finalize("[project] table validation failed")

def metadata(
self, metadata_version: str, dynamic_metadata: list[str]
self, *, metadata_version: str, dynamic_metadata: Sequence[str] = ()
) -> packaging_metadata.Metadata:
"""
Return an Message with the metadata.
Expand Down Expand Up @@ -431,8 +432,8 @@ def metadata(
message["requires_dist"] = [str(d) for d in self.dependencies]
for extra, requirements in self.optional_dependencies.items():
norm_extra = extra.replace(".", "-").replace("_", "-").lower()
message.get("provides_extra", []).append(norm_extra)
message.get("requires_dist", []).extend(
message.setdefault("provides_extra", []).append(norm_extra)
message.setdefault("requires_dist", []).extend(
str(_build_extra_req(norm_extra, requirement))
for requirement in requirements
)
Expand All @@ -449,7 +450,7 @@ def metadata(
if field.lower() not in packaging_metadata.ALL_FIELDS:
msg = f"Field is not known: {field}"
raise ConfigurationError(msg)
message["dynamic"] = dynamic_metadata
message["dynamic"] = list(dynamic_metadata)

return packaging_metadata.Metadata.from_raw(message)

Expand Down
Empty file.
6 changes: 6 additions & 0 deletions tests/project/dynamic-description/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = 'dynamic-description'
version = '1.0.0'
dynamic = [
'description',
]
1 change: 1 addition & 0 deletions tests/project/full-metadata/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some readme 👋
Empty file.
49 changes: 49 additions & 0 deletions tests/project/full-metadata/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[project]
name = 'full_metadata'
version = '3.2.1'
description = 'A package with all the metadata :)'
readme = 'README.md'
license = { text = 'some license text' }
keywords = ['trampolim', 'is', 'interesting']
authors = [
{ email = 'example@example.com' },
{ name = 'Example!' },
]
maintainers = [
{ name = 'Other Example', email = 'other@example.com' },
]
classifiers = [
'Development Status :: 4 - Beta',
'Programming Language :: Python',
]

requires-python = '>=3.8'
dependencies = [
'dependency1',
'dependency2>1.0.0',
'dependency3[extra]',
'dependency4; os_name != "nt"',
'dependency5[other-extra]>1.0; os_name == "nt"',
]

[project.optional-dependencies]
test = [
'test_dependency',
'test_dependency[test_extra]',
'test_dependency[test_extra2] > 3.0; os_name == "nt"',
]

[project.urls]
homepage = 'example.com'
documentation = 'readthedocs.org'
repository = 'github.com/some/repo'
changelog = 'github.com/some/repo/blob/master/CHANGELOG.rst'

[project.scripts]
full-metadata = 'full_metadata:main_cli'

[project.gui-scripts]
full-metadata-gui = 'full_metadata:main_gui'

[project.entry-points.custom]
full-metadata = 'full_metadata:main_custom'
1 change: 1 addition & 0 deletions tests/project/full-metadata2/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Some license! 👋
1 change: 1 addition & 0 deletions tests/project/full-metadata2/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some readme 👋
Empty file.
49 changes: 49 additions & 0 deletions tests/project/full-metadata2/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
[project]
name = 'full-metadata2'
version = '3.2.1'
description = 'A package with all the metadata :)'
readme = 'README.rst'
license = { file = 'LICENSE' }
keywords = ['trampolim', 'is', 'interesting']
authors = [
{ email = 'example@example.com' },
{ name = 'Example!' },
]
maintainers = [
{ name = 'Other Example', email = 'other@example.com' },
]
classifiers = [
'Development Status :: 4 - Beta',
'Programming Language :: Python',
]

requires-python = '>=3.8'
dependencies = [
'dependency1',
'dependency2>1.0.0',
'dependency3[extra]',
'dependency4; os_name != "nt"',
'dependency5[other-extra]>1.0; os_name == "nt"',
]

[project.optional-dependencies]
test = [
'test_dependency',
'test_dependency[test_extra]',
'test_dependency[test_extra2] > 3.0; os_name == "nt"',
]

[project.urls]
homepage = 'example.com'
documentation = 'readthedocs.org'
repository = 'github.com/some/repo'
changelog = 'github.com/some/repo/blob/master/CHANGELOG.rst'

[project.scripts]
full-metadata = 'full_metadata:main_cli'

[project.gui-scripts]
full-metadata-gui = 'full_metadata:main_gui'

[project.entry-points.custom]
full-metadata = 'full_metadata:main_custom'
20 changes: 20 additions & 0 deletions tests/project/fulltext_license/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright © 2019 Filipe Laíns <filipe.lains@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
Empty file added tests/project/spdx/AUTHORS.txt
Empty file.
Empty file added tests/project/spdx/LICENSE.md
Empty file.
Empty file added tests/project/spdx/LICENSE.txt
Empty file.
Empty file.
5 changes: 5 additions & 0 deletions tests/project/spdx/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[project]
name = "example"
version = "1.2.3"
license = "MIT OR GPL-2.0-or-later OR (FSFUL AND BSD-2-Clause)"
license-files = ["LICEN[CS]E*", "AUTHORS*", "licenses/LICENSE.MIT"]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
some readme
4 changes: 4 additions & 0 deletions tests/project/unknown-readme-type/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[project]
name = 'unknown-readme-type'
version = '1.0.0'
readme = 'README.just-made-this-up-now'
Empty file.
1 change: 1 addition & 0 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ coverage[toml]>=5.0.0
pip>=21.1
pretend
pytest>=6.2.0
tomli>=1.0.0; python_version < "3.11"
Loading
Loading