Skip to content

Commit

Permalink
Cast the Distribution to a CheckedDistribution, capturing the new typ…
Browse files Browse the repository at this point in the history
…e signature.
  • Loading branch information
jaraco committed Jun 26, 2024
1 parent b584973 commit 1ca93e2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
17 changes: 14 additions & 3 deletions twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@ def _safe_name(name: str) -> str:
return re.sub("[^A-Za-z0-9.]+", "-", name)


class CheckedDistribution(pkginfo.Distribution):
"""
A Distribution whose name and version are confirmed to be defined.
"""

name: str
version: str


class PackageFile:
def __init__(
self,
filename: str,
comment: Optional[str],
metadata: pkginfo.Distribution,
metadata: CheckedDistribution,
python_version: Optional[str],
filetype: Optional[str],
) -> None:
Expand All @@ -88,7 +97,7 @@ def __init__(
self.metadata = metadata
self.python_version = python_version
self.filetype = filetype
self.safe_name = _safe_name(cast(str, metadata.name))
self.safe_name = _safe_name(metadata.name)
self.signed_filename = self.filename + ".asc"
self.signed_basefilename = self.basefilename + ".asc"
self.gpg_signature: Optional[Tuple[str, bytes]] = None
Expand Down Expand Up @@ -156,7 +165,9 @@ def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
else:
py_version = None

return cls(filename, comment, meta, py_version, dtype)
return cls(
filename, comment, cast(CheckedDistribution, meta), py_version, dtype
)

@staticmethod
def _is_unknown_metadata_version(
Expand Down
7 changes: 5 additions & 2 deletions twine/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ def _upload(self, package: package_file.PackageFile) -> requests.Response:

with open(package.filename, "rb") as fp:
data_to_send.append(
("content", (package.basefilename, fp, "application/octet-stream"))
(
"content",
(package.basefilename, fp, "application/octet-stream"),
)
)
encoder = requests_toolbelt.MultipartEncoder(data_to_send)

Expand Down Expand Up @@ -223,7 +226,7 @@ def package_is_uploaded(
releases = {}
self._releases_json_data[safe_name] = releases

packages = releases.get(cast(str, package.metadata.version), [])
packages = releases.get(package.metadata.version, [])

for uploaded_package in packages:
if uploaded_package["filename"] == package.basefilename:
Expand Down

0 comments on commit 1ca93e2

Please sign in to comment.