From 0552e918e54bd68c0be06f7ef89ea5dae3e6e533 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Sun, 22 Aug 2021 10:15:42 +0100 Subject: [PATCH 1/2] drop coveralls and enforce 100% coverage with no cover instead --- .github/workflows/main.yml | 4 +-- README.rst | 3 -- modernize/__main__.py | 2 +- modernize/fixes/fix_itertools_imports_six.py | 8 ++--- modernize/fixes/fix_metaclass.py | 4 +-- modernize/fixes/fix_unichr.py | 2 +- modernize/fixes/fix_urllib_six.py | 18 +++++----- modernize/main.py | 38 ++++++++++---------- modernize/utils.py | 20 +++++------ pyproject.toml | 9 +---- tests/test_fix_imports_six.py | 19 ---------- tests/test_fixes.py | 8 ++--- tests/test_future_behaviour.py | 4 +-- tests/utils.py | 8 ++--- 14 files changed, 58 insertions(+), 89 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 668db06..ff85d00 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -45,9 +45,7 @@ jobs: pip install tox - name: tox - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - run: tox -e py,lint,coveralls,release + run: tox -e py,lint,release - name: upload dist uses: actions/upload-artifact@v2 diff --git a/README.rst b/README.rst index 6422e85..4da1b86 100644 --- a/README.rst +++ b/README.rst @@ -6,9 +6,6 @@ |_|_|_\___/\__,_\___|_| |_||_|_/__\___| -.. image:: https://img.shields.io/coveralls/github/PyCQA/modernize?label=coveralls&logo=coveralls - :alt: Coveralls - :target: https://coveralls.io/github/PyCQA/modernize .. image:: https://img.shields.io/readthedocs/modernize?logo=read-the-docs :alt: Read the Docs :target: https://modernize.readthedocs.io/en/latest/ diff --git a/modernize/__main__.py b/modernize/__main__.py index c03922b..0f1d010 100644 --- a/modernize/__main__.py +++ b/modernize/__main__.py @@ -5,4 +5,4 @@ from .main import main if __name__ == "__main__": - sys.exit(main()) + sys.exit(main()) # pragma: no cover diff --git a/modernize/fixes/fix_itertools_imports_six.py b/modernize/fixes/fix_itertools_imports_six.py index 8024d71..f0dcf0a 100644 --- a/modernize/fixes/fix_itertools_imports_six.py +++ b/modernize/fixes/fix_itertools_imports_six.py @@ -33,12 +33,12 @@ def transform(self, node, results): for child in children[::2]: if child.type == token.NAME: name_node = child - elif child.type == token.STAR: + elif child.type == token.STAR: # pragma: no cover # Just leave the import as is. return else: - assert child.type == syms.import_as_name - name_node = child.children[0] + assert child.type == syms.import_as_name # pragma: no cover + name_node = child.children[0] # pragma: no cover member_name = name_node.value if member_name in ( "imap", @@ -61,7 +61,7 @@ def transform(self, node, results): remove_comma ^= True while children and children[-1].type == token.COMMA: - children.pop().remove() + children.pop().remove() # pragma: no cover # If there are no imports left, just get rid of the entire statement if ( diff --git a/modernize/fixes/fix_metaclass.py b/modernize/fixes/fix_metaclass.py index 2bccdb9..47a886d 100644 --- a/modernize/fixes/fix_metaclass.py +++ b/modernize/fixes/fix_metaclass.py @@ -117,7 +117,7 @@ def find_metas(cls_node): for i, simple_node in list(enumerate(node.children)): if simple_node.type == syms.simple_stmt and simple_node.children: expr_node = simple_node.children[0] - if expr_node.type == syms.expr_stmt and expr_node.children: + if expr_node.type == syms.expr_stmt and expr_node.children: # pragma: no branch # Check if the expr_node is a simple assignment. left_node = expr_node.children[0] if isinstance(left_node, Leaf) and left_node.value == "__metaclass__": @@ -143,7 +143,7 @@ def fixup_indent(suite): node = kids.pop() if isinstance(node, Leaf) and node.type != token.DEDENT: if node.prefix: - node.prefix = "" + node.prefix = "" # pragma: no cover return else: kids.extend(node.children[::-1]) diff --git a/modernize/fixes/fix_unichr.py b/modernize/fixes/fix_unichr.py index ff1e681..f01fc34 100644 --- a/modernize/fixes/fix_unichr.py +++ b/modernize/fixes/fix_unichr.py @@ -12,6 +12,6 @@ class FixUnichr(fixer_base.ConditionalFix): def transform(self, node, results): if self.should_skip(node): - return + return # pragma: no cover if is_probably_builtin(node): fixer_util.touch_import("six", "unichr", node) diff --git a/modernize/fixes/fix_urllib_six.py b/modernize/fixes/fix_urllib_six.py index 39733fb..9e53d43 100644 --- a/modernize/fixes/fix_urllib_six.py +++ b/modernize/fixes/fix_urllib_six.py @@ -163,17 +163,17 @@ def transform_member(self, node, results): # Simple case with only a single member being imported if member: # this may be a list of length one, or just a node - if isinstance(member, list): + if isinstance(member, list): # pragma: no branch member = member[0] new_name = None - for change in MAPPING[mod_member.value]: - if member.value in change[1]: + for change in MAPPING[mod_member.value]: # pragma: no branch + if member.value in change[1]: # pragma: no branch new_name = change[0] break if new_name: mod_member.replace(Name(new_name, prefix=pref)) else: - self.cannot_convert(node, "This is an invalid module element") + self.cannot_convert(node, "This is an invalid module element") # pragma: no cover # Multiple members being imported else: @@ -216,7 +216,7 @@ def handle_name(name, prefix): names.append(Comma()) names.extend(handle_name(elts[-1], pref)) new = FromImport(module, names) - if not first or node.parent.prefix.endswith(indentation): + if not first or node.parent.prefix.endswith(indentation): # pragma: no branch new.prefix = indentation new_nodes.append(new) first = False @@ -234,16 +234,16 @@ def transform_dot(self, node, results): module_dot = results.get("bare_with_attr") member = results.get("member") new_name = None - if isinstance(member, list): + if isinstance(member, list): # pragma: no branch member = member[0] - for change in MAPPING[module_dot.value]: + for change in MAPPING[module_dot.value]: # pragma: no branch if member.value in change[1]: new_name = change[0] break if new_name: module_dot.replace(Name(new_name, prefix=module_dot.prefix)) else: - self.cannot_convert(node, "This is an invalid module element") + self.cannot_convert(node, "This is an invalid module element") # pragma: no cover def transform(self, node, results): if results.get("module"): @@ -255,5 +255,5 @@ def transform(self, node, results): # Renaming and star imports are not supported for these modules. elif results.get("module_star"): self.cannot_convert(node, "Cannot handle star imports.") - elif results.get("module_as"): + elif results.get("module_as"): # pragma: no branch self.cannot_convert(node, "This module is now multiple modules") diff --git a/modernize/main.py b/modernize/main.py index 962fbf0..fe30a92 100644 --- a/modernize/main.py +++ b/modernize/main.py @@ -31,7 +31,7 @@ def format_usage(usage): """Method that doesn't output "Usage:" prefix""" - return usage + return usage # pragma: no cover def main(args=None): @@ -134,9 +134,9 @@ def main(args=None): flags = {} options, args = parser.parse_args(args) if not options.write and options.no_diffs: - warn("Not writing files and not printing diffs; that's not very useful.") + warn("Not writing files and not printing diffs; that's not very useful.") # pragma: no cover if not options.write and options.nobackups: - parser.error("Can't use '-n' without '-w'.") + parser.error("Can't use '-n' without '-w'.") # pragma: no cover if options.list_fixes: print( "Standard transformations available for the " @@ -145,21 +145,21 @@ def main(args=None): for fixname in sorted(avail_fixes): print(" {} ({})".format(fixname, fixname.split(".fix_", 1)[1])) print() - if not args: + if not args: # pragma: no branch return 0 if not args: - print("At least one file or directory argument required.", file=sys.stderr) - print("Use --help to show usage.", file=sys.stderr) - return 2 + print("At least one file or directory argument required.", file=sys.stderr) # pragma: no cover + print("Use --help to show usage.", file=sys.stderr) # pragma: no cover + return 2 # pragma: no cover if "-" in args: - refactor_stdin = True - if options.write: + refactor_stdin = True # pragma: no cover + if options.write: # pragma: no cover print("Can't write to stdin.", file=sys.stderr) return 2 if options.print_function: - flags["print_function"] = True + flags["print_function"] = True # pragma: no cover if options.fixers_here: - sys.path.append(os.getcwd()) + sys.path.append(os.getcwd()) # pragma: no cover # Set up logging handler level = logging.DEBUG if options.verbose else logging.INFO @@ -174,9 +174,9 @@ def main(args=None): matched = None for tgt in avail_fixes: if tgt == fix or tgt.endswith(f".fix_{fix}"): - matched = tgt - unwanted_fixes.add(matched) - break + matched = tgt # pragma: no cover + unwanted_fixes.add(matched) # pragma: no cover + break # pragma: no cover else: print(f"Error: fix '{fix}' was not found", file=sys.stderr) return 2 @@ -203,7 +203,7 @@ def main(args=None): splitfixes.extend(fix.split(",")) for fix in splitfixes: if fix == "default": - default_present = True + default_present = True # pragma: no cover else: matched = None for tgt in avail_fixes: @@ -213,7 +213,7 @@ def main(args=None): break else: # A non-standard fix -- trust user to have supplied path - explicit.add(fix) + explicit.add(fix) # pragma: no cover requested = default_fixes.union(explicit) if default_present else explicit else: requested = default_fixes @@ -230,7 +230,7 @@ def main(args=None): file=sys.stderr, ) else: - print(" (None)", file=sys.stderr) + print(" (None)", file=sys.stderr) # pragma: no cover print(" Applying the following explicit transformations:", file=sys.stderr) if explicit: for fixname in sorted(explicit): @@ -250,9 +250,9 @@ def main(args=None): options.nobackups, not options.no_diffs, ) - if not rt.errors: + if not rt.errors: # pragma: no branch if refactor_stdin: - rt.refactor_stdin() + rt.refactor_stdin() # pragma: no cover else: try: rt.refactor( diff --git a/modernize/utils.py b/modernize/utils.py index 2d1921c..f632559 100644 --- a/modernize/utils.py +++ b/modernize/utils.py @@ -11,7 +11,7 @@ def _check_future_import(node): else return None.""" # node should be the import statement here if not (node.type == syms.simple_stmt and node.children): - return set() + return set() # pragma: no cover node = node.children[0] # now node is the import_from node if not ( @@ -23,7 +23,7 @@ def _check_future_import(node): if node.children[3].type == token.LPAR: # from __future__ import (.. - node = node.children[4] + node = node.children[4] # pragma: no cover else: # from __future__ import ... node = node.children[3] @@ -35,15 +35,15 @@ def _check_future_import(node): for n in node.children: if n.type == token.NAME: result.add(n.value) - elif n.type == syms.import_as_name: - n = n.children[0] - assert n.type == token.NAME - result.add(n.value) + elif n.type == syms.import_as_name: # pragma: no cover + n = n.children[0] # pragma: no cover + assert n.type == token.NAME # pragma: no cover + result.add(n.value) # pragma: no cover return result elif node.type == syms.import_as_name: - node = node.children[0] - assert node.type == token.NAME - return {node.value} + node = node.children[0] # pragma: no cover + assert node.type == token.NAME # pragma: no cover + return {node.value} # pragma: no cover elif node.type == token.NAME: return {node.value} else: # pragma: no cover @@ -54,7 +54,7 @@ def add_future(node, symbol): root = fixer_util.find_root(node) - for idx, node in enumerate(root.children): + for idx, node in enumerate(root.children): # pragma: no branch if ( node.type == syms.simple_stmt and len(node.children) > 0 diff --git a/pyproject.toml b/pyproject.toml index 03afe76..36e89e2 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,7 +49,7 @@ addopts = [ "--strict-config", "--strict-markers", "--cov", - "--cov-fail-under=92.11", + "--cov-fail-under=100", "--cov-report=term-missing:skip-covered", ] xfail_strict = true @@ -97,13 +97,6 @@ wheel_build_env = build # empty environment to build universal wheel once per tox invocation # https://github.com/ionelmc/tox-wheel#build-configuration -[testenv:coveralls] -passenv = GITHUB_* -deps = - coveralls - coverage>=5.3 -commands = coveralls - [testenv:lint] deps = pre-commit commands = pre-commit run --all-files --show-diff-on-failure {posargs} diff --git a/tests/test_fix_imports_six.py b/tests/test_fix_imports_six.py index 66df698..596bfd9 100644 --- a/tests/test_fix_imports_six.py +++ b/tests/test_fix_imports_six.py @@ -43,22 +43,3 @@ def test_moved_module(): def test_moved_module_fromlist(): check_on_input(*MOVED_MODULE_FROMLIST) - - -@unittest.skipIf(sys.version_info[0] >= 3, "Test only runs on Python 2") -def test_validate_mapping(): - for py2_name, six_name in fix_imports_six.FixImportsSix.mapping.items(): - try: - __import__(py2_name) - __import__(six_name) - except ImportError: - if "tkinter" in six_name: - # Ignore error if tkinter not installed - if tkinter is not None: - raise - elif "winreg" in six_name: - # Ignore error if we're not on Windows - if sys.platform.startswith("win"): - raise - else: - raise diff --git a/tests/test_fixes.py b/tests/test_fixes.py index e406d0f..995c4e1 100644 --- a/tests/test_fixes.py +++ b/tests/test_fixes.py @@ -13,11 +13,11 @@ def check_existence(prefix, module_names): dotted_prefix = prefix + "." for module_name in module_names: if not module_name.startswith(dotted_prefix): - msg = f"{module_name!r} does not start with {prefix!r}" - raise AssertionError(msg) + msg = f"{module_name!r} does not start with {prefix!r}" # pragma: no cover + raise AssertionError(msg) # pragma: no cover try: __import__(module_name) - except ImportError: + except ImportError: # pragma: no cover raise AssertionError(f"{module_name!r} cannot be imported") @@ -34,5 +34,5 @@ def test_fixers_importable(): for module_name in fixers: try: __import__(module_name) - except ImportError: + except ImportError: # pragma: no cover raise AssertionError(f"{module_name!r} cannot be imported") diff --git a/tests/test_future_behaviour.py b/tests/test_future_behaviour.py index e6fba63..5e1cc34 100644 --- a/tests/test_future_behaviour.py +++ b/tests/test_future_behaviour.py @@ -54,7 +54,7 @@ def _check_for_multiple_futures(file_name, source_content): result_content += line for future, how_many in counts.items(): if how_many > 1: - raise Exception( + raise Exception( # pragma: no cover f"The same future repeated more than once ({how_many} times):\n" f"{future}\n\n* Input file:\n{source_content}\n\n" f"* Output file:\n{result_content}\n" @@ -100,7 +100,7 @@ def test_two_files_on_single_run(): for input_name in input_names: futs = _check_for_multiple_futures(input_name, TWO_PRINTS_CONTENT) if not futs: - raise Exception("File {0} got no from __future__ (but it should)") + raise Exception("File {0} got no from __future__ (but it should)") # pragma: no cover finally: shutil.rmtree(tmpdirname) diff --git a/tests/utils.py b/tests/utils.py index 6f84434..4fb3487 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -31,7 +31,7 @@ def _check(this_input_content, which_check, check_return_code=True): if check_return_code and expected_return_code is not None: if expected_return_code != return_code: - raise AssertionError( + raise AssertionError( # pragma: no cover "Actual return code: %s\nExpected return code: %s" % (return_code, expected_return_code) ) @@ -41,7 +41,7 @@ def _check(this_input_content, which_check, check_return_code=True): if check_return_code and expected_return_code is not None: if expected_return_code != return_code: - raise AssertionError( + raise AssertionError( # pragma: no cover "Actual return code: %s\nExpected return code: %s" % (return_code, expected_return_code) ) @@ -49,11 +49,11 @@ def _check(this_input_content, which_check, check_return_code=True): output_content = "" with open(test_input_name) as output_file: for line in output_file: - if line: + if line: # pragma: no branch output_content += line if output_content != expected_content: - raise AssertionError( + raise AssertionError( # pragma: no cover "%s\nInput:\n%sOutput:\n%s\nExpecting:\n%s" % ( which_check, From 84ddddacfb1810655f16d99e9e5d67625eb4f2e5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 22 Aug 2021 09:16:01 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- modernize/fixes/fix_metaclass.py | 4 +++- modernize/fixes/fix_urllib_six.py | 12 +++++++++--- modernize/main.py | 8 ++++++-- tests/test_fix_imports_six.py | 3 --- tests/test_future_behaviour.py | 4 +++- 5 files changed, 21 insertions(+), 10 deletions(-) diff --git a/modernize/fixes/fix_metaclass.py b/modernize/fixes/fix_metaclass.py index 47a886d..b22afbb 100644 --- a/modernize/fixes/fix_metaclass.py +++ b/modernize/fixes/fix_metaclass.py @@ -117,7 +117,9 @@ def find_metas(cls_node): for i, simple_node in list(enumerate(node.children)): if simple_node.type == syms.simple_stmt and simple_node.children: expr_node = simple_node.children[0] - if expr_node.type == syms.expr_stmt and expr_node.children: # pragma: no branch + if ( + expr_node.type == syms.expr_stmt and expr_node.children + ): # pragma: no branch # Check if the expr_node is a simple assignment. left_node = expr_node.children[0] if isinstance(left_node, Leaf) and left_node.value == "__metaclass__": diff --git a/modernize/fixes/fix_urllib_six.py b/modernize/fixes/fix_urllib_six.py index 9e53d43..7655926 100644 --- a/modernize/fixes/fix_urllib_six.py +++ b/modernize/fixes/fix_urllib_six.py @@ -173,7 +173,9 @@ def transform_member(self, node, results): if new_name: mod_member.replace(Name(new_name, prefix=pref)) else: - self.cannot_convert(node, "This is an invalid module element") # pragma: no cover + self.cannot_convert( + node, "This is an invalid module element" + ) # pragma: no cover # Multiple members being imported else: @@ -216,7 +218,9 @@ def handle_name(name, prefix): names.append(Comma()) names.extend(handle_name(elts[-1], pref)) new = FromImport(module, names) - if not first or node.parent.prefix.endswith(indentation): # pragma: no branch + if not first or node.parent.prefix.endswith( + indentation + ): # pragma: no branch new.prefix = indentation new_nodes.append(new) first = False @@ -243,7 +247,9 @@ def transform_dot(self, node, results): if new_name: module_dot.replace(Name(new_name, prefix=module_dot.prefix)) else: - self.cannot_convert(node, "This is an invalid module element") # pragma: no cover + self.cannot_convert( + node, "This is an invalid module element" + ) # pragma: no cover def transform(self, node, results): if results.get("module"): diff --git a/modernize/main.py b/modernize/main.py index fe30a92..f450786 100644 --- a/modernize/main.py +++ b/modernize/main.py @@ -134,7 +134,9 @@ def main(args=None): flags = {} options, args = parser.parse_args(args) if not options.write and options.no_diffs: - warn("Not writing files and not printing diffs; that's not very useful.") # pragma: no cover + warn( + "Not writing files and not printing diffs; that's not very useful." + ) # pragma: no cover if not options.write and options.nobackups: parser.error("Can't use '-n' without '-w'.") # pragma: no cover if options.list_fixes: @@ -148,7 +150,9 @@ def main(args=None): if not args: # pragma: no branch return 0 if not args: - print("At least one file or directory argument required.", file=sys.stderr) # pragma: no cover + print( + "At least one file or directory argument required.", file=sys.stderr + ) # pragma: no cover print("Use --help to show usage.", file=sys.stderr) # pragma: no cover return 2 # pragma: no cover if "-" in args: diff --git a/tests/test_fix_imports_six.py b/tests/test_fix_imports_six.py index 596bfd9..7c3b728 100644 --- a/tests/test_fix_imports_six.py +++ b/tests/test_fix_imports_six.py @@ -1,7 +1,5 @@ from __future__ import generator_stop -import sys -import unittest try: from six.moves import tkinter @@ -10,7 +8,6 @@ from utils import check_on_input -from modernize.fixes import fix_imports_six MOVED_MODULE = ( """\ diff --git a/tests/test_future_behaviour.py b/tests/test_future_behaviour.py index 5e1cc34..55a93aa 100644 --- a/tests/test_future_behaviour.py +++ b/tests/test_future_behaviour.py @@ -100,7 +100,9 @@ def test_two_files_on_single_run(): for input_name in input_names: futs = _check_for_multiple_futures(input_name, TWO_PRINTS_CONTENT) if not futs: - raise Exception("File {0} got no from __future__ (but it should)") # pragma: no cover + raise Exception( + "File {0} got no from __future__ (but it should)" + ) # pragma: no cover finally: shutil.rmtree(tmpdirname)