Skip to content

Commit

Permalink
Configure and use isort 馃槏
Browse files Browse the repository at this point in the history
This should, eventually (with e.g. instructions in the contribution section, CI, etc.) make it easier to contribute to refex without breaking any style things. It also resolves a bunch of bad imports that are bad for various reasons (e.g. not moving the raw_ast import when it was renamed to ast_matchers, or stuff about whether typing is a third-party library, or...)

Pretty much everything looks good. Humongous thanks to @timothycrosley and everyone on [isort issue #1486](PyCQA/isort#1486).

This also lets me ignore the internal linter that keeps bugging me about where refex imports go. One would imagine they should go in the first-party section -- internal linter doesn't agree (for reasons described in that issue), but it's hard to standardize on a lint-unfriendly order without some tool like isort to help back me up.

PiperOrigin-RevId: 332093853
  • Loading branch information
devinj authored and Copybara-Service committed Oct 8, 2020
1 parent 93ed23e commit 4e0110a
Show file tree
Hide file tree
Showing 38 changed files with 83 additions and 52 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Expand Up @@ -16,6 +16,7 @@

import os
import sys

sys.path.insert(0, os.path.abspath('..'))

# -- Project information -----------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions pyproject.toml
Expand Up @@ -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 = """
Expand Down
7 changes: 4 additions & 3 deletions refex/cli.py
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions refex/fix/fixer.py
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions refex/fix/fixers/correctness_fixers.py
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions refex/fix/fixers/idiom_fixers.py
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions refex/fix/fixers/modern_python_fixers.py
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions refex/fix/fixers/test_correctness_fixers.py
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion refex/fix/fixers/test_idiom_fixers.py
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions refex/fix/fixers/test_modern_python_fixers.py
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions refex/fix/fixers/unittest_fixers.py
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions refex/fix/test_fixer.py
Expand Up @@ -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(
Expand Down
1 change: 1 addition & 0 deletions refex/fix/test_generate_example.py
Expand Up @@ -20,6 +20,7 @@
import ast

from absl.testing import absltest

from refex.fix import generate_example


Expand Down
2 changes: 0 additions & 2 deletions refex/formatting.py
Expand Up @@ -75,7 +75,6 @@
import subprocess
import sys
import tempfile

import typing
from typing import Any, Iterable, Mapping, Optional, Text, Tuple

Expand All @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions refex/future_string.py
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions refex/match.py
Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions refex/parsed_file.py
Expand Up @@ -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)
Expand Down
4 changes: 1 addition & 3 deletions refex/python/evaluate.py
Expand Up @@ -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


Expand Down
9 changes: 5 additions & 4 deletions refex/python/matcher.py
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions refex/python/matcher_test_util.py
Expand Up @@ -18,6 +18,7 @@
from __future__ import print_function

from absl.testing import absltest

from refex.python import matcher


Expand Down
3 changes: 2 additions & 1 deletion refex/python/matchers/base_matchers.py
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion refex/python/matchers/syntax_matchers.py
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion refex/python/matchers/test_ast_matchers.py
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions refex/python/matchers/test_base_matchers.py
Expand Up @@ -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'))
Expand Down
1 change: 1 addition & 0 deletions refex/python/matchers/test_lexical_matchers.py
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions refex/python/matchers/test_syntax_matchers.py
Expand Up @@ -19,21 +19,20 @@
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
import six

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'))


Expand Down

0 comments on commit 4e0110a

Please sign in to comment.