Skip to content

Commit 2268e88

Browse files
committed
Add logic capturing when the declared metadata version is not supported.
1 parent ecaced0 commit 2268e88

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

twine/package.py

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,19 @@
1818
import os
1919
import re
2020
import subprocess
21-
from typing import Any, Dict, List, NamedTuple, Optional, Sequence, Tuple, Union, cast
21+
import warnings
22+
from typing import (
23+
Any,
24+
Dict,
25+
Iterable,
26+
List,
27+
NamedTuple,
28+
Optional,
29+
Sequence,
30+
Tuple,
31+
Union,
32+
cast,
33+
)
2234

2335
import importlib_metadata
2436
import pkginfo
@@ -95,7 +107,8 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
95107
for ext, dtype in DIST_EXTENSIONS.items():
96108
if filename.endswith(ext):
97109
try:
98-
meta = DIST_TYPES[dtype](filename)
110+
with warnings.catch_warnings(record=True) as captured:
111+
meta = DIST_TYPES[dtype](filename)
99112
except EOFError:
100113
raise exceptions.InvalidDistribution(
101114
"Invalid distribution file: '%s'" % os.path.basename(filename)
@@ -107,19 +120,25 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
107120
"Unknown distribution format: '%s'" % os.path.basename(filename)
108121
)
109122

110-
# If pkginfo encounters a metadata version it doesn't support, it may give us
123+
supported_metadata = list(pkginfo.distribution.HEADER_ATTRS)
124+
if cls._is_unknown_metadata_version(captured):
125+
raise exceptions.InvalidDistribution(
126+
"Make sure the distribution is using a supported Metadata-Version: "
127+
f"{', '.join(supported_metadata)}."
128+
)
129+
# If pkginfo <1.11 encounters a metadata version it doesn't support, it may give
111130
# back empty metadata. At the very least, we should have a name and version,
112131
# which could also be empty if, for example, a MANIFEST.in doesn't include
113132
# setup.cfg.
114133
missing_fields = [
115134
f.capitalize() for f in ["name", "version"] if not getattr(meta, f)
116135
]
117136
if missing_fields:
118-
supported_metadata = list(pkginfo.distribution.HEADER_ATTRS)
119137
raise exceptions.InvalidDistribution(
120138
"Metadata is missing required fields: "
121-
f"{', '.join(missing_fields)}.\n"
122-
"Make sure the distribution includes the files where those fields "
139+
f"{', '.join(missing_fields)}."
140+
# TODO: Remove this section after requiring pkginfo>=1.11
141+
"\nMake sure the distribution includes the files where those fields "
123142
"are specified, and is using a supported Metadata-Version: "
124143
f"{', '.join(supported_metadata)}."
125144
)
@@ -137,6 +156,13 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
137156

138157
return cls(filename, comment, meta, py_version, dtype)
139158

159+
@staticmethod
160+
def _is_unknown_metadata_version(
161+
captured: Iterable[warnings.WarningMessage],
162+
) -> bool:
163+
NMV = getattr(pkginfo.distribution, "NewMetadataVersion", None)
164+
return any(warning.category is NMV for warning in captured)
165+
140166
def metadata_dictionary(self) -> Dict[str, MetadataValue]:
141167
"""Merge multiple sources of metadata into a single dictionary.
142168

0 commit comments

Comments
 (0)