From 033a324488356174f46aaf29bbf046b27a605a3f Mon Sep 17 00:00:00 2001 From: Manuel Barkhau Date: Fri, 18 Sep 2020 17:50:13 +0000 Subject: [PATCH] misc cleanup and linting --- README.md | 1 - pylint-ignore.md | 202 ++++++++++++++------------------------ setup.cfg | 5 +- src/pycalver/__main__.py | 3 + src/pycalver/patterns.py | 31 ------ src/pycalver/vcs.py | 6 +- src/pycalver/version.py | 67 ++++++++----- src/pycalver2/patterns.py | 3 - src/pycalver2/version.py | 89 +++++++++-------- test/test_patterns.py | 2 +- test/test_rewrite.py | 1 - test/test_version.py | 4 +- 12 files changed, 179 insertions(+), 235 deletions(-) diff --git a/README.md b/README.md index 9774893..4294453 100644 --- a/README.md +++ b/README.md @@ -242,7 +242,6 @@ These patterns are closely based on https://calver.org/ | `MAJOR` | 0..9, 10..99, 100.. | `--major` | | `MINOR` | 0..9, 10..99, 100.. | `--minor` | | `PATCH` | 0..9, 10..99, 100.. | `--patch` | -| `MICRO` | 0..9, 10..99, 100.. | Synonym for `PATCH` | ### Week Numbering diff --git a/pylint-ignore.md b/pylint-ignore.md index 8cc8b32..789183b 100644 --- a/pylint-ignore.md +++ b/pylint-ignore.md @@ -23,197 +23,145 @@ The recommended approach to using `pylint-ignore` is: # Overview - - [W0511: fixme (5x)](#w0511-fixme) - - [W0603: global-statement (1x)](#w0603-global-statement) - - [W0613: unused-argument (1x)](#w0613-unused-argument) - - [W0703: broad-except (2x)](#w0703-broad-except) - - [C0412: ungrouped-imports (2x)](#c0412-ungrouped-imports) + - [W0511: fixme (7x)](#w0511-fixme) + - [W0703: broad-except (1x)](#w0703-broad-except) # W0511: fixme +## File test/test_patterns.py - Line 14 - W0511 (fixme) + +- `message: TODO (mb 2020-09-06): test for v2patterns` +- `author : Manuel Barkhau ` +- `date : 2020-09-18T17:01:05` + +``` + 12: import pycalver2.patterns as v2patterns + 13: +> 14: # TODO (mb 2020-09-06): test for v2patterns + 15: + 16: V2_PART_PATTERN_CASES = [ +``` + + ## File src/pycalver/vcs.py - Line 78 - W0511 (fixme) -- `message: TODO (mb 2018-11-15): Detect encoding of output?` +- `message: TODO (mb 2018-11-15): Detect encoding of output? Use chardet?` - `author : Manuel Barkhau ` -- `date : 2020-07-19T18:50:33` +- `date : 2020-09-18T17:24:49` ``` 68: def __call__(self, cmd_name: str, env: Env = None, **kwargs: str) -> str: ... 76: output_data: bytes = sp.check_output(cmd_str.split(), env=env, stderr=sp.STDOUT) 77: -> 78: # TODO (mb 2018-11-15): Detect encoding of output? +> 78: # TODO (mb 2018-11-15): Detect encoding of output? Use chardet? 79: _encoding = "utf-8" 80: return output_data.decode(_encoding) ``` -## File src/pycalver2/version.py - Line 184 - W0511 (fixme) +## File test/test_version.py - Line 167 - W0511 (fixme) -- `message: TODO (mb 2020-09-05): pytag` +- `message: TODO (mb 2020-09-06): add tests for new style patterns` - `author : Manuel Barkhau ` -- `date : 2020-09-05T14:30:17` +- `date : 2020-09-18T17:01:05` ``` - 176: def _parse_field_values(field_values: FieldValues) -> VersionInfo: + 162: def vnfo(**field_values): ... - 182: tag = TAG_ALIASES.get(tag, tag) - 183: assert tag is not None -> 184: # TODO (mb 2020-09-05): pytag - 185: pytag = "" - 186: + 165: + 166: PARSE_VERSION_TEST_CASES = [ +> 167: # TODO (mb 2020-09-06): add tests for new style patterns + 168: # ["YYYY.MM.DD" , "2017.06.07", vnfo(year="2017", month="06", dom="07")], + 169: ["{year}.{month}.{dom}" , "2017.06.07", vnfo(year="2017", month="06", dom="07")], ``` -## File src/pycalver/__main__.py - Line 220 - W0511 (fixme) +## File src/pycalver/__main__.py - Line 229 - W0511 (fixme) - `message: TODO (mb 2020-09-05): version switch` - `author : Manuel Barkhau ` - `date : 2020-09-05T14:30:17` ``` - 200: def _bump( + 209: def _bump( ... - 218: - 219: try: -> 220: # TODO (mb 2020-09-05): version switch - 221: v1cli.rewrite(cfg, new_version) - 222: v2cli.rewrite(cfg, new_version) + 227: + 228: try: +> 229: # TODO (mb 2020-09-05): version switch + 230: v1cli.rewrite(cfg, new_version) + 231: # v2cli.rewrite(cfg, new_version) ``` -## File src/pycalver/__main__.py - Line 274 - W0511 (fixme) +## File src/pycalver/config.py - Line 236 - W0511 (fixme) + +- `message: TODO (mb 2020-09-06): new style pattern by default` +- `author : Manuel Barkhau ` +- `date : 2020-09-18T17:01:05` + +``` + 227: def _parse_config(raw_cfg: RawConfig) -> Config: + ... + 234: version_str = raw_cfg['current_version'] = version_str.strip("'\" ") + 235: +> 236: # TODO (mb 2020-09-06): new style pattern by default + 237: # version_pattern: str = raw_cfg.get('version_pattern', "vYYYY0M.BUILD[-TAG]") + 238: version_pattern: str = raw_cfg.get('version_pattern', "{pycalver}") +``` + + +## File src/pycalver/__main__.py - Line 285 - W0511 (fixme) - `message: TODO (mb 2020-09-05): version switch` - `author : Manuel Barkhau ` - `date : 2020-09-05T14:30:17` ``` - 272: - 273: def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config: -> 274: # TODO (mb 2020-09-05): version switch - 275: all_tags = vcs.get_tags(fetch=fetch) - 276: + 282: def _update_cfg_from_vcs(cfg: config.Config, fetch: bool) -> config.Config: + ... + 283: all_tags = vcs.get_tags(fetch=fetch) + 284: +> 285: # TODO (mb 2020-09-05): version switch + 286: cfg = v1cli.update_cfg_from_vcs(cfg, all_tags) + 287: # cfg = v2cli.update_cfg_from_vcs(cfg, all_tags) ``` -## File src/pycalver/__main__.py - Line 378 - W0511 (fixme) +## File src/pycalver/__main__.py - Line 392 - W0511 (fixme) - `message: # TODO (mb 2020-09-05): format from config` - `author : Manuel Barkhau ` - `date : 2020-09-05T14:30:17` ``` - 324: def bump( + 336: def bump( ... - 376: return - 377: -> 378: # # TODO (mb 2020-09-05): format from config - 379: # commit_message_kwargs = { - 380: # new_version -``` - - -# W0603: global-statement - -## File src/pycalver/__main__.py - Line 75 - W0603 (global-statement) - -- `message: Using the global statement` -- `author : Manuel Barkhau ` -- `date : 2020-09-05T14:30:17` - -``` - 73: def cli(verbose: int = 0) -> None: - 74: """Automatically update PyCalVer version strings on python projects.""" -> 75: global _VERBOSE - 76: _VERBOSE = verbose - 77: -``` - - -# W0613: unused-argument - -## File src/pycalver2/version.py - Line 431 - W0613 (unused-argument) - -- `message: Unused argument 'kwargs'` -- `author : Manuel Barkhau ` -- `date : 2020-09-05T14:30:17` - -``` - 429: - 430: -> 431: def _compile_format_template(pattern: str, kwargs: TemplateKwargs) -> str: - 432: # NOTE (mb 2020-09-04): Some parts are optional, we need the kwargs to - 433: # determine if part is set to its zero value + 390: return + 391: +> 392: # # TODO (mb 2020-09-05): format from config + 393: # commit_message_kwargs = { + 394: # new_version ``` # W0703: broad-except -## File src/pycalver/vcs.py - Line 107 - W0703 (broad-except) - -- `message: Catching too general exception Exception` -- `author : Manuel Barkhau ` -- `date : 2020-07-19T18:50:33` - -``` - 101: def has_remote(self) -> bool: - ... - 105: return False - 106: return True -> 107: except Exception: - 108: return False - 109: -``` - - -## File src/pycalver/__main__.py - Line 223 - W0703 (broad-except) +## File src/pycalver/__main__.py - Line 232 - W0703 (broad-except) - `message: Catching too general exception Exception` - `author : Manuel Barkhau ` - `date : 2020-09-05T14:30:17` ``` - 200: def _bump( + 209: def _bump( ... - 221: v1cli.rewrite(cfg, new_version) - 222: v2cli.rewrite(cfg, new_version) -> 223: except Exception as ex: - 224: logger.error(str(ex)) - 225: sys.exit(1) -``` - - -# C0412: ungrouped-imports - -## File src/pycalver/__main__.py - Line 21 - C0412 (ungrouped-imports) - -- `message: Imports from package pycalver are not grouped` -- `author : Manuel Barkhau ` -- `date : 2020-09-05T14:30:17` - -``` - 19: import pycalver.cli as v1cli - 20: import pycalver2.cli as v2cli -> 21: import pycalver.version as v1version - 22: import pycalver2.version as v2version - 23: from pycalver import vcs -``` - - -## File src/pycalver/__main__.py - Line 22 - C0412 (ungrouped-imports) - -- `message: Imports from package pycalver2 are not grouped` -- `author : Manuel Barkhau ` -- `date : 2020-09-05T14:30:17` - -``` - 20: import pycalver2.cli as v2cli - 21: import pycalver.version as v1version -> 22: import pycalver2.version as v2version - 23: from pycalver import vcs - 24: from pycalver import config + 230: v1cli.rewrite(cfg, new_version) + 231: # v2cli.rewrite(cfg, new_version) +> 232: except Exception as ex: + 233: logger.error(str(ex)) + 234: sys.exit(1) ``` diff --git a/setup.cfg b/setup.cfg index d638129..520c0de 100644 --- a/setup.cfg +++ b/setup.cfg @@ -128,10 +128,10 @@ jobs = 4 output-format = colorized # Maximum number of locals for function / method body -max-locals = 20 +max-locals = 21 # Maximum number of arguments for function / method -max-args = 8 +max-args = 9 good-names = logger,i,ex @@ -163,6 +163,7 @@ disable = missing-function-docstring, raise-missing-from, duplicate-code, + ungrouped-imports, generated-members = # members of typing.NamedTuple diff --git a/src/pycalver/__main__.py b/src/pycalver/__main__.py index 2f10642..6c4754f 100755 --- a/src/pycalver/__main__.py +++ b/src/pycalver/__main__.py @@ -73,6 +73,7 @@ def _validate_release_tag(release: str) -> None: @click.option('-v', '--verbose', count=True, help="Control log level. -vv for debug level.") def cli(verbose: int = 0) -> None: """Automatically update PyCalVer version strings on python projects.""" + # pylint:disable=global-statement; global flag is global. global _VERBOSE _VERBOSE = verbose @@ -164,6 +165,8 @@ def _try_print_diff(cfg: config.Config, new_version: str) -> None: else: click.echo(diff) except Exception as ex: + # pylint:disable=broad-except; Mostly we expect IOError here, but + # could be other things and there's no option to recover anyway. logger.error(str(ex), exc_info=True) sys.exit(1) diff --git a/src/pycalver/patterns.py b/src/pycalver/patterns.py index 3b36dee..a09bb01 100644 --- a/src/pycalver/patterns.py +++ b/src/pycalver/patterns.py @@ -185,37 +185,6 @@ FULL_PART_FORMATS = { } -# TODO (mb 2020-09-17): I think this is garbage -# PART_FORMATS = { -# 'major' : "[0-9]+", -# 'minor' : "[0-9]{3,}", -# 'patch' : "[0-9]{3,}", -# 'bid' : "[0-9]{4,}", -# 'MAJOR' : "[0-9]+", -# 'MINOR' : "[0-9]+", -# 'MM' : "[0-9]{2,}", -# 'MMM' : "[0-9]{3,}", -# 'MMMM' : "[0-9]{4,}", -# 'MMMMM' : "[0-9]{5,}", -# 'MMMMMM' : "[0-9]{6,}", -# 'MMMMMMM': "[0-9]{7,}", -# 'PATCH' : "[0-9]+", -# 'PP' : "[0-9]{2,}", -# 'PPP' : "[0-9]{3,}", -# 'PPPP' : "[0-9]{4,}", -# 'PPPPP' : "[0-9]{5,}", -# 'PPPPPP' : "[0-9]{6,}", -# 'PPPPPPP': "[0-9]{7,}", -# 'BID' : "[1-9][0-9]*", -# 'BB' : "[1-9][0-9]{1,}", -# 'BBB' : "[1-9][0-9]{2,}", -# 'BBBB' : "[1-9][0-9]{3,}", -# 'BBBBB' : "[1-9][0-9]{4,}", -# 'BBBBBB' : "[1-9][0-9]{5,}", -# 'BBBBBBB': "[1-9][0-9]{6,}", -# } - - class Pattern(typ.NamedTuple): raw : str # "{pycalver}", "{year}.{month}", "YYYY0M.BUILD" diff --git a/src/pycalver/vcs.py b/src/pycalver/vcs.py index 3ca6102..d7d1e95 100644 --- a/src/pycalver/vcs.py +++ b/src/pycalver/vcs.py @@ -75,7 +75,7 @@ class VCSAPI: logger.debug(cmd_str) output_data: bytes = sp.check_output(cmd_str.split(), env=env, stderr=sp.STDOUT) - # TODO (mb 2018-11-15): Detect encoding of output? + # TODO (mb 2018-11-15): Detect encoding of output? Use chardet? _encoding = "utf-8" return output_data.decode(_encoding) @@ -99,11 +99,13 @@ class VCSAPI: @property def has_remote(self) -> bool: + # pylint:disable=broad-except; Not sure how to anticipate all cases. try: output = self('show_remotes') if output.strip() == "": return False - return True + else: + return True except Exception: return False diff --git a/src/pycalver/version.py b/src/pycalver/version.py index 6bf8658..9282a71 100644 --- a/src/pycalver/version.py +++ b/src/pycalver/version.py @@ -21,16 +21,48 @@ logger = logging.getLogger("pycalver.version") TODAY = dt.datetime.utcnow().date() +MaybeInt = typ.Optional[int] + + class CalendarInfo(typ.NamedTuple): """Container for calendar components of version strings.""" - year : int - quarter : int - month : int - dom : int - doy : int - iso_week: int - us_week : int + year : MaybeInt + quarter : MaybeInt + month : MaybeInt + dom : MaybeInt + doy : MaybeInt + iso_week: MaybeInt + us_week : MaybeInt + + +class VersionInfo(typ.NamedTuple): + """Container for parsed version string.""" + + year : MaybeInt + quarter : MaybeInt + month : MaybeInt + dom : MaybeInt + doy : MaybeInt + iso_week: MaybeInt + us_week : MaybeInt + major : int + minor : int + patch : int + bid : str + tag : str + + +def _ver_to_cal_info(vinfo: VersionInfo) -> CalendarInfo: + return CalendarInfo( + vinfo.year, + vinfo.quarter, + vinfo.month, + vinfo.dom, + vinfo.doy, + vinfo.iso_week, + vinfo.us_week, + ) def _date_from_doy(year: int, doy: int) -> dt.date: @@ -95,23 +127,6 @@ def cal_info(date: dt.date = None) -> CalendarInfo: return CalendarInfo(**kwargs) -class VersionInfo(typ.NamedTuple): - """Container for parsed version string.""" - - year : typ.Optional[int] - quarter : typ.Optional[int] - month : typ.Optional[int] - dom : typ.Optional[int] - doy : typ.Optional[int] - iso_week: typ.Optional[int] - us_week : typ.Optional[int] - major : int - minor : int - patch : int - bid : str - tag : str - - FieldKey = str MatchGroupKey = str MatchGroupStr = str @@ -438,8 +453,8 @@ def incr( cur_cal_nfo = cal_info() - old_date = (old_vinfo.year or 0, old_vinfo.month or 0, old_vinfo.dom or 0) - cur_date = (cur_cal_nfo.year , cur_cal_nfo.month , cur_cal_nfo.dom) + old_date = (old_vinfo.year or 0 , old_vinfo.month or 0 , old_vinfo.dom or 0) + cur_date = (cur_cal_nfo.year or 0, cur_cal_nfo.month or 0, cur_cal_nfo.dom or 0) if old_date <= cur_date: cur_vinfo = cur_vinfo._replace(**cur_cal_nfo._asdict()) diff --git a/src/pycalver2/patterns.py b/src/pycalver2/patterns.py index c6081f8..2b27506 100644 --- a/src/pycalver2/patterns.py +++ b/src/pycalver2/patterns.py @@ -84,7 +84,6 @@ PART_PATTERNS = { 'MAJOR': r"[0-9]+", 'MINOR': r"[0-9]+", 'PATCH': r"[0-9]+", - 'MICRO': r"[0-9]+", 'BUILD': r"[0-9]+", 'BLD' : r"[1-9][0-9]*", 'TAG' : r"(?:alpha|beta|dev|pre|rc|post|final)", @@ -110,7 +109,6 @@ PATTERN_PART_FIELDS = { 'MAJOR': 'major', 'MINOR': 'minor', 'PATCH': 'patch', - 'MICRO': 'patch', 'BUILD': 'bid', 'BLD' : 'bid', 'TAG' : 'tag', @@ -193,7 +191,6 @@ PART_FORMATS: typ.Dict[str, typ.Callable[[FieldValue], str]] = { 'MAJOR': _fmt_num, 'MINOR': _fmt_num, 'PATCH': _fmt_num, - 'MICRO': _fmt_num, 'BUILD': _fmt_num, 'BLD' : _fmt_bld, 'TAG' : _fmt_num, diff --git a/src/pycalver2/version.py b/src/pycalver2/version.py index 6d9fe71..6093ff2 100644 --- a/src/pycalver2/version.py +++ b/src/pycalver2/version.py @@ -28,7 +28,6 @@ ZERO_VALUES = { 'MAJOR': "0", 'MINOR': "0", 'PATCH': "0", - 'MICRO': "0", 'TAG' : "final", 'PYTAG': "", 'NUM' : "0", @@ -67,18 +66,56 @@ assert set(TAG_BY_PEP440_TAG.values()) < set(PEP440_TAG_BY_TAG.keys()) # } +MaybeInt = typ.Optional[int] + + class CalendarInfo(typ.NamedTuple): """Container for calendar components of version strings.""" - year_y : int - year_g : int - quarter: int - month : int - dom : int - doy : int - week_w : int - week_u : int - week_v : int + year_y : MaybeInt + year_g : MaybeInt + quarter: MaybeInt + month : MaybeInt + dom : MaybeInt + doy : MaybeInt + week_w : MaybeInt + week_u : MaybeInt + week_v : MaybeInt + + +class VersionInfo(typ.NamedTuple): + """Container for parsed version string.""" + + year_y : MaybeInt + year_g : MaybeInt + quarter: MaybeInt + month : MaybeInt + dom : MaybeInt + doy : MaybeInt + week_w : MaybeInt + week_u : MaybeInt + week_v : MaybeInt + major : int + minor : int + patch : int + num : int + bid : str + tag : str + pytag : str + + +def _ver_to_cal_info(vinfo: VersionInfo) -> CalendarInfo: + return CalendarInfo( + vinfo.year_y, + vinfo.year_g, + vinfo.quarter, + vinfo.month, + vinfo.dom, + vinfo.doy, + vinfo.week_w, + vinfo.week_u, + vinfo.week_v, + ) def _date_from_doy(year: int, doy: int) -> dt.date: @@ -145,30 +182,6 @@ def cal_info(date: dt.date = None) -> CalendarInfo: return CalendarInfo(**kwargs) -MaybeInt = typ.Optional[int] - - -class VersionInfo(typ.NamedTuple): - """Container for parsed version string.""" - - year_y : MaybeInt - year_g : MaybeInt - quarter: MaybeInt - month : MaybeInt - dom : MaybeInt - doy : MaybeInt - week_w : MaybeInt - week_u : MaybeInt - week_v : MaybeInt - major : int - minor : int - patch : int - num : int - bid : str - tag : str - pytag : str - - VALID_FIELD_KEYS = set(VersionInfo._fields) | {'version'} FieldKey = str @@ -215,7 +228,7 @@ def _parse_version_info(field_values: FieldValues) -> VersionInfo: >>> vnfo = _parse_version_info({'year_y': "2021", 'month': "01", 'dom': "03"}) >>> (vnfo.year_y, vnfo.month, vnfo.dom, vnfo.tag) (2021, 1, 3, 'final') - >>> (vnfo.year_y, vnfo.week_w,vnfo.year_y, vnfo.week_u,vnfo.year_g, vnfo.week_v) + >>> (vnfo.year_y, vnfo.week_w, vnfo.year_y, vnfo.week_u,vnfo.year_g, vnfo.week_v) (2021, 0, 2021, 1, 2020, 53) """ for key in field_values: @@ -542,8 +555,6 @@ def format_version(vinfo: VersionInfo, pattern: str) -> str: 'v1.0.0' >>> format_version(vinfo_d, pattern="vMAJOR.MINOR[.PATCH[-TAG]]") 'v1.0' - >>> format_version(vinfo_d, pattern="vMAJOR.MINOR[.MICRO[-TAG]]") - 'v1.0' >>> format_version(vinfo_d, pattern="vMAJOR[.MINOR[.PATCH[-TAG]]]") 'v1' @@ -594,8 +605,8 @@ def incr( cur_cal_nfo = cal_info() - old_date = (old_vinfo.year_y or 0, old_vinfo.month or 0, old_vinfo.dom or 0) - cur_date = (cur_cal_nfo.year_y , cur_cal_nfo.month , cur_cal_nfo.dom) + old_date = (old_vinfo.year_y or 0 , old_vinfo.month or 0 , old_vinfo.dom or 0) + cur_date = (cur_cal_nfo.year_y or 0, cur_cal_nfo.month or 0, cur_cal_nfo.dom or 0) if old_date <= cur_date: cur_vinfo = cur_vinfo._replace(**cur_cal_nfo._asdict()) diff --git a/test/test_patterns.py b/test/test_patterns.py index 6e6d25f..cf96507 100644 --- a/test/test_patterns.py +++ b/test/test_patterns.py @@ -128,7 +128,7 @@ V2_PART_PATTERN_CASES = [ (['0V'], "52", "52"), (['0V'], "53", "53"), (['0V'], "54", None), - (['MAJOR', 'MINOR', 'PATCH', 'MICRO'], "0", "0"), + (['MAJOR', 'MINOR', 'PATCH'], "0", "0"), (['TAG' ], "alpha" , "alpha"), (['TAG' ], "alfa" , None), (['TAG' ], "beta" , "beta"), diff --git a/test/test_rewrite.py b/test/test_rewrite.py index 7a90a3d..c9107fe 100644 --- a/test/test_rewrite.py +++ b/test/test_rewrite.py @@ -5,7 +5,6 @@ from __future__ import absolute_import from __future__ import unicode_literals import copy -import datetime as dt from test import util from pycalver import config diff --git a/test/test_version.py b/test/test_version.py index 43f3720..ee010a5 100644 --- a/test/test_version.py +++ b/test/test_version.py @@ -200,9 +200,9 @@ def test_v1_parse_versions(pattern_str, line, expected_vinfo): # def test_v2_parse_versions(pattern_str, line, expected_vinfo): def test_v2_parse_versions(): - vnfo = v2version.parse_version_info("v201712.0033", pattern="vYYYY0M.BUILD[-TAG[NUM]]") + _vnfo = v2version.parse_version_info("v201712.0033", pattern="vYYYY0M.BUILD[-TAG[NUM]]") fvals = {'year_y': 2017, 'month': 12, 'bid': "0033"} - assert vnfo == v2version._parse_version_info(fvals) + assert _vnfo == v2version._parse_version_info(fvals) def test_make_segments():