From 6aa16f7b370ef618f824e9ef02fd908fe7a99199 Mon Sep 17 00:00:00 2001 From: Manuel Barkhau Date: Tue, 4 Sep 2018 20:16:46 +0200 Subject: [PATCH] mypy pacification --- src/pycalver/__main__.py | 19 +++++++++---------- src/pycalver/lex_id.py | 2 +- src/pycalver/vcs.py | 35 +++++++++++++++++++++++------------ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/src/pycalver/__main__.py b/src/pycalver/__main__.py index 74d0a98..a8acb95 100644 --- a/src/pycalver/__main__.py +++ b/src/pycalver/__main__.py @@ -83,7 +83,7 @@ def incr(old_version: str, release: str = None) -> None: new_version_nfo = parse.parse_version_info(new_version) print("PyCalVer Version:", new_version) - print("PEP440 Version:", new_version_nfo["pep440_version"]) + print("PEP440 Version:", new_version_nfo.pep440_version) @cli.command() @@ -196,15 +196,14 @@ def bump( filepaths = set(file_patterns.keys()) _vcs = vcs.get_vcs() - _vcs.assert_not_dirty(filepaths, allow_dirty) - rewrite.rewrite(new_version, file_patterns, dry, verbose) - - if dry: - return - - if not commit: - return - if _vcs is None: log.warn("Version Control System not found, aborting commit.") + else: + _vcs.assert_not_dirty(filepaths, allow_dirty) + + rewrite.rewrite(new_version, file_patterns, dry, verbose) + + if dry or not commit or _vcs is None: return + + # TODO (mb 2018-09-04): add files and commit diff --git a/src/pycalver/lex_id.py b/src/pycalver/lex_id.py index 3fcc3c6..b75d1d0 100644 --- a/src/pycalver/lex_id.py +++ b/src/pycalver/lex_id.py @@ -102,7 +102,7 @@ def ord_val(lex_id: str) -> int: return int(lex_id[1:], 10) -def main(): +def main() -> None: _curr_id = "01" print(f"{'lexical':<13} {'numerical':>12}") diff --git a/src/pycalver/vcs.py b/src/pycalver/vcs.py index 78aec0c..f2292f4 100644 --- a/src/pycalver/vcs.py +++ b/src/pycalver/vcs.py @@ -21,18 +21,29 @@ log = logging.getLogger("pycalver.vcs") class BaseVCS: - @classmethod - def commit(cls, message): - f = tempfile.NamedTemporaryFile("wb", delete=False) - f.write(message.encode("utf-8")) - f.close() - cmd = cls._COMMIT_COMMAND + [f.name] - env_items = list(os.environ.items()) + [(b"HGENCODING", b"utf-8")] - sp.check_output(cmd, env=dict(env_items)) - os.unlink(f.name) + _TEST_USABLE_COMMAND: typ.List[str] + _COMMIT_COMMAND: typ.List[str] + _STATUS_COMMAND: typ.List[str] @classmethod - def is_usable(cls): + def commit(cls, message: str) -> None: + message_data = message.encode("utf-8") + + tmp_file = tempfile.NamedTemporaryFile("wb", delete=False) + + with tmp_file as fh: + fh.write(message_data) + + cmd = cls._COMMIT_COMMAND + [tmp_file.name] + env = os.environ.copy() + # TODO (mb 2018-09-04): check that this works on py27, + # might need to be bytes there, idk. + env["HGENCODING"] = "utf-8" + sp.check_output(cmd, env=env) + os.unlink(tmp_file.name) + + @classmethod + def is_usable(cls) -> bool: try: return sp.call( cls._TEST_USABLE_COMMAND, @@ -46,7 +57,7 @@ class BaseVCS: raise @classmethod - def dirty_files(cls): + def dirty_files(cls) -> typ.List[str]: status_output = sp.check_output(cls._STATUS_COMMAND) return [ line.decode("utf-8")[2:].strip() @@ -107,7 +118,7 @@ class Mercurial(BaseVCS): VCS = [Git, Mercurial] -def get_vcs(): +def get_vcs() -> typ.Optional[typ.Type[BaseVCS]]: for vcs in VCS: if vcs.is_usable(): return vcs