Skip to content

Commit

Permalink
fix issue psf#251: isort removes newlines before standalone comments …
Browse files Browse the repository at this point in the history
…if the following line is an import of the same group
  • Loading branch information
EgorChernik committed Jun 25, 2019
1 parent 7c556fa commit d0f3f2d
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ _build
.vscode
docs/_static/pypi.svg
.tox
.idea
__pycache__
black.egg-info
build/
Expand Down
18 changes: 16 additions & 2 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@

__version__ = "19.3b0"
DEFAULT_LINE_LENGTH = 88
DEFAULT_IMPORT_MODE = 0
DEFAULT_EXCLUDES = (
r"/(\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|_build|buck-out|build|dist)/"
)
Expand Down Expand Up @@ -182,6 +183,7 @@ class Feature(Enum):
@dataclass
class FileMode:
target_versions: Set[TargetVersion] = Factory(set)
import_mode: int = DEFAULT_IMPORT_MODE
line_length: int = DEFAULT_LINE_LENGTH
string_normalization: bool = True
is_pyi: bool = False
Expand Down Expand Up @@ -359,6 +361,14 @@ def read_pyproject_toml(
),
is_eager=True,
)
@click.option(
"--import-mode",
default=DEFAULT_IMPORT_MODE,
help=(
"0-handle imports as usual. Default value\n"
"1-skip adding empty line between standalone comment and import\n"
),
)
@click.option(
"--config",
type=click.Path(
Expand All @@ -370,6 +380,7 @@ def read_pyproject_toml(
)
@click.pass_context
def main(
import_mode: int,
ctx: click.Context,
code: Optional[str],
line_length: int,
Expand Down Expand Up @@ -406,6 +417,7 @@ def main(
versions = set()
mode = FileMode(
target_versions=versions,
import_mode=import_mode,
line_length=line_length,
is_pyi=pyi,
string_normalization=not skip_string_normalization,
Expand Down Expand Up @@ -721,7 +733,7 @@ def format_str(src_contents: str, *, mode: FileMode) -> FileContent:
is_pyi=mode.is_pyi,
normalize_strings=mode.string_normalization,
)
elt = EmptyLineTracker(is_pyi=mode.is_pyi)
elt = EmptyLineTracker(is_pyi=mode.is_pyi, import_mode=mode.import_mode)
empty_line = Line()
after = 0
split_line_features = {
Expand Down Expand Up @@ -1464,6 +1476,7 @@ class EmptyLineTracker:
are consumed by `maybe_empty_lines()` and included in the computation.
"""

import_mode: int = DEFAULT_IMPORT_MODE
is_pyi: bool = False
previous_line: Optional[Line] = None
previous_after: int = 0
Expand Down Expand Up @@ -1504,7 +1517,8 @@ def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
return self._maybe_empty_lines_for_class_or_def(current_line, before)

if (
self.previous_line
self.import_mode == 0
and self.previous_line
and self.previous_line.is_import
and not current_line.is_import
and depth == self.previous_line.depth
Expand Down
112 changes: 112 additions & 0 deletions tests/data/import_handling_issue_251.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""The asyncio package, tracking PEP 3156."""

# flake8: noqa

from logging import (
ERROR,
)
import sys

# This relies on each of the submodules having an __all__ variable.
from .base_events import *
from .coroutines import *
from .events import * # comment here
# bugfix_1
from .futures import *
from .locks import * # comment here
from .protocols import *
# bugfix_2
from ..runners import * # comment here
from ..queues import *
from ..streams import *

from some_library import (
Just, Enough, Libraries, To, Fit, In, This, Nice, Split, Which, We, No, Longer, Use
)
from name_of_a_company.extremely_long_project_name.component.ttypes import CuteLittleServiceHandlerFactoryyy
from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *

from .a.b.c.subprocess import *
from . import (tasks)
from . import (A, B, C)
from . import SomeVeryLongNameAndAllOfItsAdditionalLetters1, \
SomeVeryLongNameAndAllOfItsAdditionalLetters2
# bugfix_3
__all__ = (
base_events.__all__
+ coroutines.__all__
+ events.__all__
+ futures.__all__
+ locks.__all__
+ protocols.__all__
+ runners.__all__
+ queues.__all__
+ streams.__all__
+ tasks.__all__
)


# output


"""The asyncio package, tracking PEP 3156."""

# flake8: noqa

from logging import ERROR
import sys

# This relies on each of the submodules having an __all__ variable.
from .base_events import *
from .coroutines import *
from .events import * # comment here
# bugfix_1
from .futures import *
from .locks import * # comment here
from .protocols import *
# bugfix_2
from ..runners import * # comment here
from ..queues import *
from ..streams import *

from some_library import (
Just,
Enough,
Libraries,
To,
Fit,
In,
This,
Nice,
Split,
Which,
We,
No,
Longer,
Use,
)
from name_of_a_company.extremely_long_project_name.component.ttypes import (
CuteLittleServiceHandlerFactoryyy,
)
from name_of_a_company.extremely_long_project_name.extremely_long_component_name.ttypes import *

from .a.b.c.subprocess import *
from . import tasks
from . import A, B, C
from . import (
SomeVeryLongNameAndAllOfItsAdditionalLetters1,
SomeVeryLongNameAndAllOfItsAdditionalLetters2,
)
# bugfix_3
__all__ = (
base_events.__all__
+ coroutines.__all__
+ events.__all__
+ futures.__all__
+ locks.__all__
+ protocols.__all__
+ runners.__all__
+ queues.__all__
+ streams.__all__
+ tasks.__all__
)
8 changes: 8 additions & 0 deletions tests/test_black.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,14 @@ def test_tuple_assign(self) -> None:
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())

@patch("black.dump_to_file", dump_to_stderr)
def test__maybe_empty_lines(self) -> None:
source, expected = read_data("import_handling_issue_251")
actual = fs(source, mode=black.FileMode(import_mode=1))
self.assertFormatEqual(expected, actual)
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode(import_mode=1))

def test_tab_comment_indentation(self) -> None:
contents_tab = "if 1:\n\tif 2:\n\t\tpass\n\t# comment\n\tpass\n"
contents_spc = "if 1:\n if 2:\n pass\n # comment\n pass\n"
Expand Down

0 comments on commit d0f3f2d

Please sign in to comment.