Skip to content

Commit

Permalink
Add type annotations to twine.wheel and twine.wininst (#607)
Browse files Browse the repository at this point in the history
* Add type annotations to twine.wheel and twine.wininst

* Add unit test for pyversion not found in wheel
  • Loading branch information
deveshks authored Apr 26, 2020
1 parent 2cec216 commit 7111240
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ txt_report = mypy
; TODO: Adopt --strict settings, iterating towards something like:
; https://github.com/pypa/packaging/blob/master/setup.cfg
; Starting with modules that have annotations applied via MonkeyType
[mypy-twine.auth,twine.cli,twine.exceptions,twine.package,twine.repository,twine.utils,twine.commands]
[mypy-twine.auth,twine.cli,twine.exceptions,twine.package,twine.repository,twine.utils,twine.commands,twine.wheel,twine.wininst]
; Enabling this will fail on subclasses of untype imports, e.g. tqdm
; disallow_subclassing_any = True
disallow_any_generics = True
Expand Down
7 changes: 7 additions & 0 deletions tests/test_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import os
import zipfile

import pretend
import pytest

import helpers
Expand All @@ -36,6 +37,12 @@ def test_version_parsing(example_wheel):
assert example_wheel.py_version == "py2.py3"


def test_version_parsing_missing_pyver(monkeypatch, example_wheel):

wheel.wheel_file_re = pretend.stub(match=lambda a: None)
assert example_wheel.py_version == "any"


def test_find_metadata_files():
names = [
"package/lib/__init__.py",
Expand Down
19 changes: 12 additions & 7 deletions twine/wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import os
import re
import zipfile
from typing import List
from typing import Optional

from pkginfo import distribution

Expand All @@ -34,24 +36,27 @@


class Wheel(distribution.Distribution):
def __init__(self, filename, metadata_version=None):
def __init__(self, filename: str, metadata_version: Optional[str] = None) -> None:
self.filename = filename
self.basefilename = os.path.basename(self.filename)
self.metadata_version = metadata_version
self.extractMetadata()

@property
def py_version(self):
def py_version(self) -> str:
wheel_info = wheel_file_re.match(self.basefilename)
return wheel_info.group("pyver")
if wheel_info is None:
return "any"
else:
return wheel_info.group("pyver")

@staticmethod
def find_candidate_metadata_files(names):
def find_candidate_metadata_files(names: List[str]) -> List[List[str]]:
"""Filter files that may be METADATA files."""
tuples = [x.split("/") for x in names if "METADATA" in x]
return [x[1] for x in sorted([(len(x), x) for x in tuples])]

def read(self):
def read(self) -> bytes:
fqn = os.path.abspath(os.path.normpath(self.filename))
if not os.path.exists(fqn):
raise exceptions.InvalidDistribution("No such file: %s" % fqn)
Expand All @@ -60,7 +65,7 @@ def read(self):
archive = zipfile.ZipFile(fqn)
names = archive.namelist()

def read_file(name):
def read_file(name: str) -> bytes:
return archive.read(name)

else:
Expand All @@ -79,7 +84,7 @@ def read_file(name):

raise exceptions.InvalidDistribution("No METADATA in archive: %s" % fqn)

def parse(self, data):
def parse(self, data: bytes) -> None:
super().parse(data)

fp = io.StringIO(distribution.must_decode(data))
Expand Down
9 changes: 5 additions & 4 deletions twine/wininst.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import zipfile
from typing import Optional

from pkginfo import distribution

Expand All @@ -10,20 +11,20 @@


class WinInst(distribution.Distribution):
def __init__(self, filename, metadata_version=None):
def __init__(self, filename: str, metadata_version: Optional[str] = None) -> None:
self.filename = filename
self.metadata_version = metadata_version
self.extractMetadata()

@property
def py_version(self):
def py_version(self) -> str:
m = wininst_file_re.match(self.filename)
if m is None:
return "any"
else:
return m.group("pyver")

def read(self):
def read(self) -> bytes:
fqn = os.path.abspath(os.path.normpath(self.filename))
if not os.path.exists(fqn):
raise exceptions.InvalidDistribution("No such file: %s" % fqn)
Expand All @@ -32,7 +33,7 @@ def read(self):
archive = zipfile.ZipFile(fqn)
names = archive.namelist()

def read_file(name):
def read_file(name: str) -> bytes:
return archive.read(name)

else:
Expand Down

0 comments on commit 7111240

Please sign in to comment.