Skip to content

Commit

Permalink
Reformat codebase with isort
Browse files Browse the repository at this point in the history
  • Loading branch information
ichard26 committed Jul 27, 2022
1 parent e0a780a commit 44d5da0
Show file tree
Hide file tree
Showing 30 changed files with 235 additions and 185 deletions.
2 changes: 1 addition & 1 deletion action/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import shlex
import sys
from pathlib import Path
from subprocess import run, PIPE, STDOUT
from subprocess import PIPE, STDOUT, run

ACTION_PATH = Path(os.environ["GITHUB_ACTION_PATH"])
ENV_PATH = ACTION_PATH / ".black-env"
Expand Down
4 changes: 3 additions & 1 deletion fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import re

import hypothesmith
from hypothesis import HealthCheck, given, settings, strategies as st
from hypothesis import HealthCheck, given, settings
from hypothesis import strategies as st

import black
from blib2to3.pgen2.tokenize import TokenError
Expand Down Expand Up @@ -78,6 +79,7 @@ def test_idempotent_any_syntatically_valid_python(
# (if you want only bounded fuzzing, just use `pytest fuzz.py`)
try:
import sys

import atheris
except ImportError:
pass
Expand Down
10 changes: 1 addition & 9 deletions gallery/gallery.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,7 @@
from concurrent.futures import ThreadPoolExecutor
from functools import lru_cache, partial
from pathlib import Path
from typing import (
Generator,
List,
NamedTuple,
Optional,
Tuple,
Union,
cast,
)
from typing import Generator, List, NamedTuple, Optional, Tuple, Union, cast
from urllib.request import urlopen, urlretrieve

PYPI_INSTANCE = "https://pypi.org/pypi"
Expand Down
2 changes: 1 addition & 1 deletion scripts/migrate-black.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import os
import sys
from subprocess import check_output, run, Popen, PIPE
from subprocess import PIPE, Popen, check_output, run


def git(*args: str) -> str:
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# Copyright (C) 2020 Łukasz Langa
from setuptools import setup, find_packages
import sys
import os
import sys

from setuptools import find_packages, setup

assert sys.version_info >= (3, 6, 2), "black requires Python 3.6.2+"
from pathlib import Path # noqa E402
Expand Down
89 changes: 51 additions & 38 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import asyncio
from json.decoder import JSONDecodeError
import json
from contextlib import contextmanager
from datetime import datetime
from enum import Enum
import io
from multiprocessing import Manager, freeze_support
import json
import os
from pathlib import Path
from pathspec.patterns.gitwildmatch import GitWildMatchPatternError
import platform
import re
import signal
import sys
import tokenize
import traceback
from contextlib import contextmanager
from dataclasses import replace
from datetime import datetime
from enum import Enum
from json.decoder import JSONDecodeError
from multiprocessing import Manager, freeze_support
from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
Expand All @@ -34,48 +34,61 @@

import click
from click.core import ParameterSource
from dataclasses import replace
from mypy_extensions import mypyc_attr
from pathspec.patterns.gitwildmatch import GitWildMatchPatternError

from black.const import DEFAULT_LINE_LENGTH, DEFAULT_INCLUDES, DEFAULT_EXCLUDES
from black.const import STDIN_PLACEHOLDER
from black.nodes import STARS, syms, is_simple_decorator_expression
from black.nodes import is_string_token, is_number_token
from black.lines import Line, EmptyLineTracker
from black.linegen import transform_line, LineGenerator, LN
from _black_version import version as __version__
from black.cache import Cache, filter_cached, get_cache_info, read_cache, write_cache
from black.comments import normalize_fmt_off
from black.mode import FUTURE_FLAG_TO_FEATURE, Mode, TargetVersion
from black.mode import Feature, supports_feature, VERSION_TO_FEATURES
from black.cache import read_cache, write_cache, get_cache_info, filter_cached, Cache
from black.concurrency import cancel, shutdown, maybe_install_uvloop
from black.output import dump_to_file, ipynb_diff, diff, color_diff, out, err
from black.report import Report, Changed, NothingChanged
from black.concurrency import cancel, maybe_install_uvloop, shutdown
from black.const import (
DEFAULT_EXCLUDES,
DEFAULT_INCLUDES,
DEFAULT_LINE_LENGTH,
STDIN_PLACEHOLDER,
)
from black.files import (
find_project_root,
find_pyproject_toml,
parse_pyproject_toml,
find_user_pyproject_toml,
gen_python_files,
get_gitignore,
normalize_path_maybe_ignore,
parse_pyproject_toml,
wrap_stream_for_windows,
)
from black.files import gen_python_files, get_gitignore, normalize_path_maybe_ignore
from black.files import wrap_stream_for_windows
from black.parsing import InvalidInput # noqa F401
from black.parsing import lib2to3_parse, parse_ast, stringify_ast
from black.handle_ipynb_magics import (
mask_cell,
unmask_cell,
remove_trailing_semicolon,
put_trailing_semicolon_back,
TRANSFORMED_MAGICS,
PYTHON_CELL_MAGICS,
TRANSFORMED_MAGICS,
jupyter_dependencies_are_installed,
mask_cell,
put_trailing_semicolon_back,
remove_trailing_semicolon,
unmask_cell,
)


# lib2to3 fork
from blib2to3.pytree import Node, Leaf
from black.linegen import LN, LineGenerator, transform_line
from black.lines import EmptyLineTracker, Line
from black.mode import (
FUTURE_FLAG_TO_FEATURE,
VERSION_TO_FEATURES,
Feature,
Mode,
TargetVersion,
supports_feature,
)
from black.nodes import (
STARS,
is_number_token,
is_simple_decorator_expression,
is_string_token,
syms,
)
from black.output import color_diff, diff, dump_to_file, err, ipynb_diff, out
from black.parsing import InvalidInput # noqa F401
from black.parsing import lib2to3_parse, parse_ast, stringify_ast
from black.report import Changed, NothingChanged, Report
from blib2to3.pgen2 import token

from _black_version import version as __version__
from blib2to3.pytree import Leaf, Node

if TYPE_CHECKING:
from concurrent.futures import Executor
Expand Down Expand Up @@ -770,7 +783,7 @@ def reformat_many(
workers: Optional[int],
) -> None:
"""Reformat multiple files using a ProcessPoolExecutor."""
from concurrent.futures import Executor, ThreadPoolExecutor, ProcessPoolExecutor
from concurrent.futures import Executor, ProcessPoolExecutor, ThreadPoolExecutor

executor: Executor
worker_count = workers if workers is not None else DEFAULT_WORKERS
Expand Down
20 changes: 14 additions & 6 deletions src/black/brackets.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
"""Builds on top of nodes.py to track brackets."""

from dataclasses import dataclass, field
import sys
from dataclasses import dataclass, field
from typing import Dict, Iterable, List, Optional, Tuple, Union

if sys.version_info < (3, 8):
from typing_extensions import Final
else:
from typing import Final

from blib2to3.pytree import Leaf, Node
from black.nodes import (
BRACKET,
CLOSING_BRACKETS,
COMPARATORS,
LOGIC_OPERATORS,
MATH_OPERATORS,
OPENING_BRACKETS,
UNPACKING_PARENTS,
VARARGS_PARENTS,
is_vararg,
syms,
)
from blib2to3.pgen2 import token

from black.nodes import syms, is_vararg, VARARGS_PARENTS, UNPACKING_PARENTS
from black.nodes import BRACKET, OPENING_BRACKETS, CLOSING_BRACKETS
from black.nodes import MATH_OPERATORS, COMPARATORS, LOGIC_OPERATORS
from blib2to3.pytree import Leaf, Node

# types
LN = Union[Leaf, Node]
Expand Down
6 changes: 2 additions & 4 deletions src/black/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import os
import pickle
from pathlib import Path
import tempfile
from pathlib import Path
from typing import Dict, Iterable, Set, Tuple

from platformdirs import user_cache_dir

from black.mode import Mode

from _black_version import version as __version__

from black.mode import Mode

# types
Timestamp = float
Expand Down
15 changes: 10 additions & 5 deletions src/black/comments.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import re
import sys
from dataclasses import dataclass
from functools import lru_cache
import re
from typing import Iterator, List, Optional, Union

if sys.version_info >= (3, 8):
from typing import Final
else:
from typing_extensions import Final

from blib2to3.pytree import Node, Leaf, type_repr
from black.nodes import (
CLOSING_BRACKETS,
STANDALONE_COMMENT,
WHITESPACE,
container_of,
first_leaf_column,
preceding_leaf,
)
from blib2to3.pgen2 import token

from black.nodes import first_leaf_column, preceding_leaf, container_of
from black.nodes import CLOSING_BRACKETS, STANDALONE_COMMENT, WHITESPACE
from blib2to3.pytree import Leaf, Node, type_repr

# types
LN = Union[Leaf, Node]
Expand Down
5 changes: 2 additions & 3 deletions src/black/debug.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from dataclasses import dataclass
from typing import Iterator, TypeVar, Union

from blib2to3.pytree import Node, Leaf, type_repr
from blib2to3.pgen2 import token

from black.nodes import Visitor
from black.output import out
from black.parsing import lib2to3_parse
from blib2to3.pgen2 import token
from blib2to3.pytree import Leaf, Node, type_repr

LN = Union[Leaf, Node]
T = TypeVar("T")
Expand Down
8 changes: 4 additions & 4 deletions src/black/files.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from functools import lru_cache
import io
import os
from pathlib import Path
import sys
from functools import lru_cache
from pathlib import Path
from typing import (
TYPE_CHECKING,
Any,
Dict,
Iterable,
Expand All @@ -14,7 +15,6 @@
Sequence,
Tuple,
Union,
TYPE_CHECKING,
)

from mypy_extensions import mypyc_attr
Expand All @@ -30,9 +30,9 @@
else:
import tomli as tomllib

from black.handle_ipynb_magics import jupyter_dependencies_are_installed
from black.output import err
from black.report import Report
from black.handle_ipynb_magics import jupyter_dependencies_are_installed

if TYPE_CHECKING:
import colorama # noqa: F401
Expand Down
20 changes: 7 additions & 13 deletions src/black/handle_ipynb_magics.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
"""Functions to process IPython magics with."""

from functools import lru_cache
import dataclasses
import ast
from typing import Dict, List, Tuple, Optional

import collections
import dataclasses
import secrets
import sys
import collections
from functools import lru_cache
from typing import Dict, List, Optional, Tuple

if sys.version_info >= (3, 10):
from typing import TypeGuard
else:
from typing_extensions import TypeGuard

from black.report import NothingChanged
from black.output import out

from black.report import NothingChanged

TRANSFORMED_MAGICS = frozenset(
(
Expand Down Expand Up @@ -90,11 +88,7 @@ def remove_trailing_semicolon(src: str) -> Tuple[str, bool]:
Mirrors the logic in `quiet` from `IPython.core.displayhook`, but uses
``tokenize_rt`` so that round-tripping works fine.
"""
from tokenize_rt import (
src_to_tokens,
tokens_to_src,
reversed_enumerate,
)
from tokenize_rt import reversed_enumerate, src_to_tokens, tokens_to_src

tokens = src_to_tokens(src)
trailing_semicolon = False
Expand All @@ -118,7 +112,7 @@ def put_trailing_semicolon_back(src: str, has_trailing_semicolon: bool) -> str:
"""
if not has_trailing_semicolon:
return src
from tokenize_rt import src_to_tokens, tokens_to_src, reversed_enumerate
from tokenize_rt import reversed_enumerate, src_to_tokens, tokens_to_src

tokens = src_to_tokens(src)
for idx, token in reversed_enumerate(tokens):
Expand Down
Loading

0 comments on commit 44d5da0

Please sign in to comment.