diff --git a/docs/conf.py b/docs/conf.py index b38f474..d87b1b6 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -16,6 +16,7 @@ import os import sys + sys.path.insert(0, os.path.abspath('..')) # -- Project information ----------------------------------------------------- diff --git a/pyproject.toml b/pyproject.toml index 5788a21..306024f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,16 @@ pytest = "^6" [tool.poetry.extras] docs = ["m2r", "sphinx"] +[tool.isort] +profile = "google" +# Upstream version within Google actually uses contextlib2 and mock for Python 2 +# compatibility, and this is transformed away by copybara. +# Adding contextlib2 to the stdlib makes the sorting correct in the canonical +# github version even when run on the upstream version pre-copybara. Adding +# mock... doesn't help as much (sorts as "mock", not "unittest"). +# These can both go away starting in 2021. +extra_standard_library = ["contextlib2", "mock"] + # https://tox.readthedocs.io/ [tool.tox] legacy_tox_ini = """ diff --git a/refex/cli.py b/refex/cli.py index abe8c7c..1f3ff3d 100755 --- a/refex/cli.py +++ b/refex/cli.py @@ -36,22 +36,23 @@ import io import json import os -import pkg_resources import re import sys import tempfile import textwrap import traceback -from typing import Dict, List, Iterable, Optional, Text, Tuple, Union +from typing import Dict, Iterable, List, Optional, Text, Tuple, Union from absl import app import attr import colorama +import pkg_resources +import six + from refex import formatting from refex import search from refex.fix import find_fixer from refex.python import syntactic_template -import six _IGNORABLE_ERRNO = frozenset([ errno.ENOENT, # file was removed after we went looking diff --git a/refex/fix/fixer.py b/refex/fix/fixer.py index 392dc0f..565e60a 100644 --- a/refex/fix/fixer.py +++ b/refex/fix/fixer.py @@ -20,16 +20,17 @@ import abc import operator +from typing import Callable, List, Mapping, Optional, Text, TypeVar import attr +import six + from refex import formatting from refex import future_string from refex import search from refex import substitution from refex.python import matcher from refex.python.matchers import syntax_matchers -import six -from typing import Callable, List, Mapping, Optional, Text, TypeVar class ParsedPythonFixer( diff --git a/refex/fix/fixers/correctness_fixers.py b/refex/fix/fixers/correctness_fixers.py index 15d44fd..a1c405d 100644 --- a/refex/fix/fixers/correctness_fixers.py +++ b/refex/fix/fixers/correctness_fixers.py @@ -25,14 +25,14 @@ from __future__ import print_function from __future__ import unicode_literals # for convenience +import six + from refex import formatting from refex.fix import fixer from refex.python import syntactic_template from refex.python.matchers import ast_matchers from refex.python.matchers import base_matchers from refex.python.matchers import syntax_matchers -import six - # Python 2 compatibility hack to be able to get b'...' and '...'. if six.PY2: diff --git a/refex/fix/fixers/idiom_fixers.py b/refex/fix/fixers/idiom_fixers.py index 781341a..d36f4a1 100644 --- a/refex/fix/fixers/idiom_fixers.py +++ b/refex/fix/fixers/idiom_fixers.py @@ -22,6 +22,7 @@ from __future__ import unicode_literals # for convenience import textwrap + from refex import formatting from refex import future_string from refex.fix import fixer diff --git a/refex/fix/fixers/modern_python_fixers.py b/refex/fix/fixers/modern_python_fixers.py index 7bf3ddc..7de3b0c 100644 --- a/refex/fix/fixers/modern_python_fixers.py +++ b/refex/fix/fixers/modern_python_fixers.py @@ -17,14 +17,15 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals # for convenience + +import six + from refex import formatting from refex.fix import fixer from refex.python import syntactic_template from refex.python.matchers import ast_matchers from refex.python.matchers import base_matchers from refex.python.matchers import syntax_matchers -import six - SIMPLE_PYTHON_FIXERS = [] # Disabled except when running in Python 2. diff --git a/refex/fix/fixers/test_correctness_fixers.py b/refex/fix/fixers/test_correctness_fixers.py index aa0bfdf..3fbafcb 100644 --- a/refex/fix/fixers/test_correctness_fixers.py +++ b/refex/fix/fixers/test_correctness_fixers.py @@ -22,6 +22,7 @@ from absl.testing import absltest from absl.testing import parameterized + from refex import search from refex.fix import fixer from refex.fix.fixers import correctness_fixers diff --git a/refex/fix/fixers/test_idiom_fixers.py b/refex/fix/fixers/test_idiom_fixers.py index a02c623..1abf822 100644 --- a/refex/fix/fixers/test_idiom_fixers.py +++ b/refex/fix/fixers/test_idiom_fixers.py @@ -23,10 +23,11 @@ from absl.testing import absltest from absl.testing import parameterized +import six + from refex import search from refex.fix import fixer from refex.fix.fixers import idiom_fixers -import six def _rewrite(fixer_, code): diff --git a/refex/fix/fixers/test_modern_python_fixers.py b/refex/fix/fixers/test_modern_python_fixers.py index 7db146c..06c764c 100644 --- a/refex/fix/fixers/test_modern_python_fixers.py +++ b/refex/fix/fixers/test_modern_python_fixers.py @@ -19,6 +19,7 @@ from absl.testing import absltest from absl.testing import parameterized + from refex import search from refex.fix.fixers import modern_python_fixers diff --git a/refex/fix/fixers/unittest_fixers.py b/refex/fix/fixers/unittest_fixers.py index b4307a0..e0f8aaa 100644 --- a/refex/fix/fixers/unittest_fixers.py +++ b/refex/fix/fixers/unittest_fixers.py @@ -24,6 +24,7 @@ from __future__ import division from __future__ import print_function from __future__ import unicode_literals # for convenience + from refex import future_string from refex.fix import fixer from refex.python import syntactic_template diff --git a/refex/fix/test_fixer.py b/refex/fix/test_fixer.py index 52df39c..c8dd636 100644 --- a/refex/fix/test_fixer.py +++ b/refex/fix/test_fixer.py @@ -18,23 +18,23 @@ from __future__ import print_function from __future__ import unicode_literals # for convenience. -import re from unittest import mock +import re from absl.testing import absltest from absl.testing import parameterized import attr + from refex import formatting from refex import future_string from refex import search from refex import substitution +from refex.fix import find_fixer +from refex.fix import fixer from refex.python import syntactic_template from refex.python.matchers import ast_matchers from refex.python.matchers import syntax_matchers -from refex.fix import find_fixer -from refex.fix import fixer - def _search_replace_fixer(search_expr, replace, message=None, url='', **kwargs): return fixer.SimplePythonFixer( diff --git a/refex/fix/test_generate_example.py b/refex/fix/test_generate_example.py index df3e475..bff06ed 100644 --- a/refex/fix/test_generate_example.py +++ b/refex/fix/test_generate_example.py @@ -20,6 +20,7 @@ import ast from absl.testing import absltest + from refex.fix import generate_example diff --git a/refex/formatting.py b/refex/formatting.py index c45af83..aa58024 100644 --- a/refex/formatting.py +++ b/refex/formatting.py @@ -75,7 +75,6 @@ import subprocess import sys import tempfile - import typing from typing import Any, Iterable, Mapping, Optional, Text, Tuple @@ -89,7 +88,6 @@ from refex import parsed_file from refex import substitution - _DEFAULT_STYLES = ( colorama.Style.BRIGHT + colorama.Fore.YELLOW, colorama.Style.BRIGHT + colorama.Fore.BLUE, diff --git a/refex/future_string.py b/refex/future_string.py index 9456faf..920dd72 100644 --- a/refex/future_string.py +++ b/refex/future_string.py @@ -49,10 +49,9 @@ import collections import string +from typing import Any, Mapping, Text import six -from typing import Any, Text, Mapping - # For refex_doctest.py # The examples are specific to Python 2. diff --git a/refex/match.py b/refex/match.py index df1729a..5179349 100644 --- a/refex/match.py +++ b/refex/match.py @@ -59,10 +59,10 @@ # from __future__ import google_type_annotations from __future__ import print_function -import attr - from typing import Any, Tuple +import attr + @attr.s(frozen=True) class Match(object): diff --git a/refex/parsed_file.py b/refex/parsed_file.py index 0c1f35e..f93777f 100644 --- a/refex/parsed_file.py +++ b/refex/parsed_file.py @@ -16,21 +16,20 @@ ------------------------ """ +# No portable raw unicode literal exists without unicode_literals. +# see https://stackoverflow.com/questions/33027281 from __future__ import absolute_import from __future__ import division # from __future__ import google_type_annotations from __future__ import print_function - -# No portable raw unicode literal exists without unicode_literals. -# see https://stackoverflow.com/questions/33027281 from __future__ import unicode_literals import re +from typing import Iterable, Mapping, Optional, Text import asttokens import attr import cached_property -from typing import Mapping, Optional, Iterable, Text @attr.s(frozen=True, eq=True, order=False) diff --git a/refex/python/evaluate.py b/refex/python/evaluate.py index 7fa8c55..e449101 100644 --- a/refex/python/evaluate.py +++ b/refex/python/evaluate.py @@ -30,19 +30,17 @@ # from __future__ import google_type_annotations from __future__ import print_function - import textwrap from refex.python import error_strings from refex.python import matcher from refex.python import matchers from refex.python import semiliteral_eval - # Actually collect all the matchers into the matchers module, so they can be # enumerated. +import refex.python.matchers.ast_matchers # pylint: disable=unused-import import refex.python.matchers.base_matchers # pylint: disable=unused-import import refex.python.matchers.lexical_matchers # pylint: disable=unused-import -import refex.python.matchers.ast_matchers # pylint: disable=unused-import import refex.python.matchers.syntax_matchers # pylint: disable=unused-import diff --git a/refex/python/matcher.py b/refex/python/matcher.py index 86bf6d7..b9626af 100644 --- a/refex/python/matcher.py +++ b/refex/python/matcher.py @@ -80,21 +80,22 @@ import collections import contextlib import copy +import enum import functools import sys import tokenize +from typing import Any, Dict, Iterator, Optional, Text import weakref from absl import logging import asttokens import attr import cached_property -import enum -from refex import match -from refex import parsed_file import six from six.moves import reprlib -from typing import Any, Dict, Iterator, Text, Optional + +from refex import match +from refex import parsed_file _match = match # when `match` is shadowed, e.g. class attributes. diff --git a/refex/python/matcher_test_util.py b/refex/python/matcher_test_util.py index ff840bf..7a237a3 100644 --- a/refex/python/matcher_test_util.py +++ b/refex/python/matcher_test_util.py @@ -18,6 +18,7 @@ from __future__ import print_function from absl.testing import absltest + from refex.python import matcher diff --git a/refex/python/matchers/base_matchers.py b/refex/python/matchers/base_matchers.py index c288654..c6eb51c 100644 --- a/refex/python/matchers/base_matchers.py +++ b/refex/python/matchers/base_matchers.py @@ -70,13 +70,14 @@ from __future__ import print_function import re +from typing import Container, List import weakref import attr import cached_property + from refex import match from refex.python import matcher -from typing import Container, List @matcher.safe_to_eval diff --git a/refex/python/matchers/syntax_matchers.py b/refex/python/matchers/syntax_matchers.py index 901ebd2..4898f67 100644 --- a/refex/python/matchers/syntax_matchers.py +++ b/refex/python/matchers/syntax_matchers.py @@ -91,11 +91,12 @@ import attr import cached_property +import six + from refex.python import matcher from refex.python import python_pattern from refex.python.matchers import ast_matchers from refex.python.matchers import base_matchers -import six def _remap_macro_variables(pattern): diff --git a/refex/python/matchers/test_ast_matchers.py b/refex/python/matchers/test_ast_matchers.py index 4947832..23b714c 100644 --- a/refex/python/matchers/test_ast_matchers.py +++ b/refex/python/matchers/test_ast_matchers.py @@ -21,11 +21,12 @@ from absl.testing import absltest from absl.testing import parameterized +import six + from refex import match from refex.python import matcher from refex.python.matchers import ast_matchers from refex.python.matchers import base_matchers -import six def expression(e): diff --git a/refex/python/matchers/test_base_matchers.py b/refex/python/matchers/test_base_matchers.py index 807b8c7..1fdc2f7 100644 --- a/refex/python/matchers/test_base_matchers.py +++ b/refex/python/matchers/test_base_matchers.py @@ -26,11 +26,11 @@ from absl.testing import parameterized from refex import match +from refex.python import evaluate from refex.python import matcher from refex.python import matcher_test_util -from refex.python.matchers import base_matchers from refex.python.matchers import ast_matchers -from refex.python import evaluate +from refex.python.matchers import base_matchers _NOTHING = base_matchers.Unless(base_matchers.Anything()) _FAKE_CONTEXT = matcher.MatchContext(matcher.parse_ast('', 'foo.py')) diff --git a/refex/python/matchers/test_lexical_matchers.py b/refex/python/matchers/test_lexical_matchers.py index ea2f709..c04aa28 100644 --- a/refex/python/matchers/test_lexical_matchers.py +++ b/refex/python/matchers/test_lexical_matchers.py @@ -18,6 +18,7 @@ from __future__ import print_function from absl.testing import absltest + from refex.python import matcher_test_util from refex.python.matchers import ast_matchers from refex.python.matchers import lexical_matchers diff --git a/refex/python/matchers/test_syntax_matchers.py b/refex/python/matchers/test_syntax_matchers.py index 3c1d69b..859134f 100644 --- a/refex/python/matchers/test_syntax_matchers.py +++ b/refex/python/matchers/test_syntax_matchers.py @@ -19,9 +19,9 @@ from __future__ import division from __future__ import print_function +from unittest import mock import textwrap import unittest -from unittest import mock from absl.testing import absltest from absl.testing import parameterized @@ -29,11 +29,10 @@ from refex.python import matcher from refex.python import matcher_test_util -from refex.python.matchers import base_matchers from refex.python.matchers import ast_matchers +from refex.python.matchers import base_matchers from refex.python.matchers import syntax_matchers - _FAKE_CONTEXT = matcher.MatchContext(matcher.parse_ast('', 'foo.py')) diff --git a/refex/python/syntactic_template.py b/refex/python/syntactic_template.py index 78d93ce..4991486 100644 --- a/refex/python/syntactic_template.py +++ b/refex/python/syntactic_template.py @@ -41,18 +41,19 @@ import ast import tokenize +from typing import Text from absl import logging import attr import cached_property +import six + from refex import formatting from refex.python import matcher from refex.python import python_pattern from refex.python.matchers import ast_matchers from refex.python.matchers import base_matchers from refex.python.matchers import syntax_matchers -import six -from typing import Text @attr.s(frozen=True) diff --git a/refex/python/test_evaluate.py b/refex/python/test_evaluate.py index 5687a64..3b60f18 100644 --- a/refex/python/test_evaluate.py +++ b/refex/python/test_evaluate.py @@ -18,6 +18,7 @@ from __future__ import print_function from absl.testing import absltest + from refex.python import evaluate from refex.python.matchers import ast_matchers from refex.python.matchers import base_matchers diff --git a/refex/python/test_syntactic_template.py b/refex/python/test_syntactic_template.py index b76611a..e0376fb 100644 --- a/refex/python/test_syntactic_template.py +++ b/refex/python/test_syntactic_template.py @@ -20,12 +20,13 @@ from absl.testing import absltest from absl.testing import parameterized +import six + from refex import formatting from refex.python import matcher from refex.python import syntactic_template from refex.python.matchers import ast_matchers from refex.python.matchers import base_matchers -import six class LexicalTemplateTest(parameterized.TestCase): diff --git a/refex/refex_doctest.py b/refex/refex_doctest.py index ae21a44..51f32f5 100644 --- a/refex/refex_doctest.py +++ b/refex/refex_doctest.py @@ -22,11 +22,14 @@ import sys from absl.testing import absltest + import refex.python.matcher_test_util # so that it's found by _submodules: pylint: disable=unused-import import refex.search +# isort: split # We put doctest after absltest so that it picks up the unittest monkeypatch. -# Otherwise doctest tests aren't runnable at all. +# Otherwise doctest tests aren't runnable at all with Bazel. + import doctest diff --git a/refex/search.py b/refex/search.py index 662e7a5..e891163 100644 --- a/refex/search.py +++ b/refex/search.py @@ -96,10 +96,15 @@ import itertools import re import sys +from typing import (Dict, Iterable, Mapping, MutableMapping, MutableSequence, + MutableSet, Optional, Pattern, Sequence, Text, Tuple, + Union) from absl import logging import attr import cached_property +import six + from refex import formatting from refex import match from refex import parsed_file @@ -108,8 +113,6 @@ from refex.python import matcher from refex.python.matchers import base_matchers from refex.python.matchers import syntax_matchers -import six -from typing import Dict, Iterable, Mapping, MutableMapping, MutableSequence, MutableSet, Optional, Pattern, Sequence, Text, Tuple, Union Span = Tuple[int, int] # TODO(b/118783544): Only string keys. diff --git a/refex/substitution.py b/refex/substitution.py index 52613b0..c9725d1 100644 --- a/refex/substitution.py +++ b/refex/substitution.py @@ -51,13 +51,12 @@ import collections import operator import re +from typing import (FrozenSet, Iterable, List, Mapping, Optional, Text, Tuple, + Union) import attr import six -from typing import FrozenSet, Iterable, List, Mapping, Optional, Text, Tuple, Union - - # Only slightly structured category name: dot-separated, no empty intra-dot # sequences, no whitespace, doesn't begin with a -, and doesn't begin/end on a # dot. diff --git a/refex/test_cli.py b/refex/test_cli.py index 4f55749..0ed1351 100644 --- a/refex/test_cli.py +++ b/refex/test_cli.py @@ -20,6 +20,7 @@ import argparse import contextlib import json +from unittest import mock import os import re import sys @@ -28,10 +29,10 @@ from absl.testing import absltest from absl.testing import parameterized -from unittest import mock -from refex import cli import six +from refex import cli + class ParseArgsLeftoversTest(absltest.TestCase): diff --git a/refex/test_example_binary.py b/refex/test_example_binary.py index 72a8259..b46949a 100644 --- a/refex/test_example_binary.py +++ b/refex/test_example_binary.py @@ -20,7 +20,6 @@ from absl.testing import absltest - _EXECUTABLE = [sys.executable, 'examples/example_binary.py'] diff --git a/refex/test_formatting.py b/refex/test_formatting.py index 334fb6c..e315aac 100644 --- a/refex/test_formatting.py +++ b/refex/test_formatting.py @@ -22,6 +22,7 @@ from absl.testing import absltest from absl.testing import parameterized import colorama + from refex import formatting from refex import match from refex import parsed_file diff --git a/refex/test_future_string.py b/refex/test_future_string.py index b5d72d4..b7c6034 100644 --- a/refex/test_future_string.py +++ b/refex/test_future_string.py @@ -24,9 +24,10 @@ from absl.testing import absltest from absl.testing import parameterized import attr -from refex import future_string import six +from refex import future_string + @attr.s class Stringifiable(object): diff --git a/refex/test_parsed_file.py b/refex/test_parsed_file.py index a1c2f6b..2405202 100644 --- a/refex/test_parsed_file.py +++ b/refex/test_parsed_file.py @@ -19,6 +19,7 @@ from absl.testing import absltest from absl.testing import parameterized + from refex import parsed_file diff --git a/refex/test_substitution.py b/refex/test_substitution.py index 3d02739..711ec5d 100644 --- a/refex/test_substitution.py +++ b/refex/test_substitution.py @@ -19,6 +19,7 @@ from absl.testing import absltest from absl.testing import parameterized + from refex import substitution