Skip to content

Commit

Permalink
Annotate exceptions, commands, and commands.upload (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrutledge authored Apr 25, 2020
1 parent 8765d14 commit 2cec216
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 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.package,twine.repository,twine.utils]
[mypy-twine.auth,twine.cli,twine.exceptions,twine.package,twine.repository,twine.utils,twine.commands]
; Enabling this will fail on subclasses of untype imports, e.g. tqdm
; disallow_subclassing_any = True
disallow_any_generics = True
Expand Down
4 changes: 2 additions & 2 deletions twine/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
__all__: List[str] = []


def _group_wheel_files_first(files):
def _group_wheel_files_first(files: List[str]) -> List[str]:
if not any(fname for fname in files if fname.endswith(".whl")):
# Return early if there's no wheel files
return files
Expand All @@ -30,7 +30,7 @@ def _group_wheel_files_first(files):
return files


def _find_dists(dists):
def _find_dists(dists: List[str]) -> List[str]:
uploads = []
for filename in dists:
if os.path.exists(filename):
Expand Down
20 changes: 13 additions & 7 deletions twine/commands/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
# limitations under the License.
import argparse
import os.path
from typing import List
from typing import cast

import requests

from twine import commands
from twine import exceptions
Expand All @@ -21,7 +25,9 @@
from twine import utils


def skip_upload(response, skip_existing, package):
def skip_upload(
response: requests.Response, skip_existing: bool, package: package_file.PackageFile
) -> bool:
if not skip_existing:
return False

Expand All @@ -44,14 +50,14 @@ def skip_upload(response, skip_existing, package):
)


def upload(upload_settings, dists):
def upload(upload_settings: settings.Settings, dists: List[str]) -> None:
dists = commands._find_dists(dists)

# Determine if the user has passed in pre-signed distributions
signatures = {os.path.basename(d): d for d in dists if d.endswith(".asc")}
uploads = [i for i in dists if not i.endswith(".asc")]
upload_settings.check_repository_url()
repository_url = upload_settings.repository_config["repository"]
repository_url = cast(str, upload_settings.repository_config["repository"])

print(f"Uploading distributions to {repository_url}")

Expand Down Expand Up @@ -109,7 +115,7 @@ def upload(upload_settings, dists):
repository.close()


def main(args):
def main(args: List[str]) -> None:
parser = argparse.ArgumentParser(prog="twine upload")
settings.Settings.register_argparse_arguments(parser)
parser.add_argument(
Expand All @@ -122,8 +128,8 @@ def main(args):
"file upload.",
)

args = parser.parse_args(args)
upload_settings = settings.Settings.from_argparse(args)
parsed_args = parser.parse_args(args)
upload_settings = settings.Settings.from_argparse(parsed_args)

# Call the upload function with the arguments from the command line
return upload(upload_settings, args.dists)
return upload(upload_settings, parsed_args.dists)
6 changes: 4 additions & 2 deletions twine/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RedirectDetected(TwineException):
"""

@classmethod
def from_args(cls, repository_url, redirect_url):
def from_args(cls, repository_url: str, redirect_url: str) -> "RedirectDetected":
msg = "\n".join(
[
"{} attempted to redirect to {}.".format(repository_url, redirect_url),
Expand Down Expand Up @@ -60,7 +60,9 @@ class UploadToDeprecatedPyPIDetected(TwineException):
"""

@classmethod
def from_args(cls, target_url, default_url, test_url):
def from_args(
cls, target_url: str, default_url: str, test_url: str
) -> "UploadToDeprecatedPyPIDetected":
"""Return an UploadToDeprecatedPyPIDetected instance."""
return cls(
"You're trying to upload to the legacy PyPI site '{}'. "
Expand Down
2 changes: 1 addition & 1 deletion twine/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(
self.blake2_256_digest = hexdigest.blake2

@classmethod
def from_filename(cls, filename: str, comment: None) -> "PackageFile":
def from_filename(cls, filename: str, comment: Optional[str]) -> "PackageFile":
# Extract the metadata from the package
for ext, dtype in DIST_EXTENSIONS.items():
if filename.endswith(ext):
Expand Down
4 changes: 2 additions & 2 deletions twine/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def __init__(
self,
*,
sign: bool = False,
sign_with: Optional[str] = "gpg",
sign_with: str = "gpg",
identity: Optional[str] = None,
username: Optional[str] = None,
password: Optional[str] = None,
Expand Down Expand Up @@ -272,7 +272,7 @@ def from_argparse(cls, args: argparse.Namespace) -> "Settings":
return cls(**settings)

def _handle_package_signing(
self, sign: bool, sign_with: Optional[str], identity: Optional[str]
self, sign: bool, sign_with: str, identity: Optional[str]
) -> None:
if not sign and identity:
raise exceptions.InvalidSigningConfiguration(
Expand Down

0 comments on commit 2cec216

Please sign in to comment.