diff --git a/twine/package.py b/twine/package.py index 0845105b..5018d370 100644 --- a/twine/package.py +++ b/twine/package.py @@ -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: @@ -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 @@ -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( diff --git a/twine/repository.py b/twine/repository.py index 32c54702..5b2e288e 100644 --- a/twine/repository.py +++ b/twine/repository.py @@ -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) @@ -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: