Skip to content

Commit

Permalink
Clean up after monkeytype
Browse files Browse the repository at this point in the history
  • Loading branch information
bhrutledge committed Apr 26, 2020
1 parent 3d58211 commit fa5ba3b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 29 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,twine.wheel,twine.wininst]
[mypy-twine.auth,twine.cli,twine.exceptions,twine.package,twine.repository,twine.utils,twine.wheel,twine.wininst,twine.commands,twine.commands.check,twine.commands.upload]
; Enabling this will fail on subclasses of untype imports, e.g. tqdm
; disallow_subclassing_any = True
disallow_any_generics = True
Expand Down
8 changes: 4 additions & 4 deletions tests/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_check_no_distributions(monkeypatch):

monkeypatch.setattr(commands, "_find_dists", lambda a: [])

assert not check.check("dist/*", output_stream=stream)
assert not check.check(["dist/*"], output_stream=stream)
assert stream.getvalue() == "No files to check.\n"


Expand All @@ -72,7 +72,7 @@ def test_check_passing_distribution(monkeypatch):
)
monkeypatch.setattr(check, "_WarningStream", lambda: warning_stream)

assert not check.check("dist/*", output_stream=output_stream)
assert not check.check(["dist/*"], output_stream=output_stream)
assert output_stream.getvalue() == "Checking dist/dist.tar.gz: PASSED\n"
assert renderer.render.calls == [pretend.call("blah", stream=warning_stream)]

Expand All @@ -94,7 +94,7 @@ def test_check_no_description(monkeypatch, capsys):

# used to crash with `AttributeError`
output_stream = io.StringIO()
check.check("dist/*", output_stream=output_stream)
check.check(["dist/*"], output_stream=output_stream)
assert output_stream.getvalue() == (
"Checking dist/dist.tar.gz: PASSED, with warnings\n"
" warning: `long_description_content_type` missing. "
Expand Down Expand Up @@ -123,7 +123,7 @@ def test_check_failing_distribution(monkeypatch):
)
monkeypatch.setattr(check, "_WarningStream", lambda: warning_stream)

assert check.check("dist/*", output_stream=output_stream)
assert check.check(["dist/*"], output_stream=output_stream)
assert output_stream.getvalue() == (
"Checking dist/dist.tar.gz: FAILED\n"
" `long_description` has syntax errors in markup and would not be "
Expand Down
36 changes: 12 additions & 24 deletions twine/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
import io
import re
import sys
from io import StringIO
from typing import Any
import textwrap
from typing import IO
from typing import List
from typing import Tuple
from typing import Union
from typing import cast

import readme_renderer.rst
from pretend import stub

from twine import commands
from twine import package as package_file
Expand Down Expand Up @@ -72,8 +71,8 @@ def __str__(self) -> str:


def _check_file(
filename: str, render_warning_stream: Union[str, _WarningStream]
) -> Union[Tuple[List[Any], bool], Tuple[List[str], bool]]:
filename: str, render_warning_stream: _WarningStream
) -> Tuple[List[str], bool]:
"""Check given distribution."""
warnings = []
is_ok = True
Expand All @@ -90,7 +89,7 @@ def _check_file(
)
description_content_type = "text/x-rst"

content_type, params = cgi.parse_header(description_content_type)
content_type, params = cgi.parse_header(cast(str, description_content_type))
renderer = _RENDERERS.get(content_type, _RENDERERS[None])

if description in {None, "UNKNOWN\n\n\n"}:
Expand All @@ -105,18 +104,7 @@ def _check_file(
return warnings, is_ok


# TODO: Replace with textwrap.indent when Python 2 support is dropped
def _indented(text: str, prefix: str) -> str:
"""Adds 'prefix' to all non-empty lines on 'text'."""

def prefixed_lines():
for line in text.splitlines(True):
yield (prefix + line if line.strip() else line)

return "".join(prefixed_lines())


def check(dists: str, output_stream: StringIO = sys.stdout) -> bool:
def check(dists: List[str], output_stream: IO[str] = sys.stdout) -> bool:
uploads = [i for i in commands._find_dists(dists) if not i.endswith(".asc")]
if not uploads: # Return early, if there are no files to check.
output_stream.write("No files to check.\n")
Expand All @@ -138,8 +126,8 @@ def check(dists: str, output_stream: StringIO = sys.stdout) -> bool:
"`long_description` has syntax errors in markup and "
"would not be rendered on PyPI.\n"
)
output_stream.write(_indented(error_text, " "))
output_stream.write(_indented(str(render_warning_stream), " "))
output_stream.write(textwrap.indent(error_text, " "))
output_stream.write(textwrap.indent(str(render_warning_stream), " "))
elif warnings:
output_stream.write("PASSED, with warnings\n")
else:
Expand All @@ -152,7 +140,7 @@ def check(dists: str, output_stream: StringIO = sys.stdout) -> bool:
return failure


def main(args: List[str]) -> stub:
def main(args: List[str]) -> bool:
parser = argparse.ArgumentParser(prog="twine check")
parser.add_argument(
"dists",
Expand All @@ -161,7 +149,7 @@ def main(args: List[str]) -> stub:
help="The distribution files to check, usually dist/*",
)

args = parser.parse_args(args)
parsed_args = parser.parse_args(args)

# Call the check function with the arguments from the command line
return check(args.dists)
return check(parsed_args.dists)

0 comments on commit fa5ba3b

Please sign in to comment.