Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix and test infinite line addition. #294

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bin/tidy-imports
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ def main():
)
sorted_imports = sort_imports(x)
if options.canonicalize:
sorted_imports = canonicalize_imports(sorted_imports, params=options.params)
return sorted_imports
cannonical_imports = canonicalize_imports(sorted_imports, params=options.params)
else:
cannonical_imports = sort_imports
return cannonical_imports
process_actions(args, options.actions, modify)


Expand Down
2 changes: 1 addition & 1 deletion lib/python/pyflyby/_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def __hash__(self):


@total_ordering
class FileText(object):
class FileText:
"""
Represents a contiguous sequence of lines from a file.
"""
Expand Down
6 changes: 4 additions & 2 deletions lib/python/pyflyby/_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ def __new__(cls, *args, **kwargs):
for arg in args:
if arg is None:
pass
elif isinstance(arg, cls):
elif isinstance(arg, cls) or hasattr(self, "__dict__"):
dicts.append(arg.__dict__)
else:
raise TypeError
raise TypeError(
"expected None, or instance of %s cls, got %s" % (cls, arg)
)
if kwargs:
dicts.append(kwargs)
for kwargs in dicts:
Expand Down
1 change: 1 addition & 0 deletions lib/python/pyflyby/_imports2s.py
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ def next_line(index):
and line_pkg_dict.get(i) != line_pkg_dict.get(i - 1)
and len(pkg_lines[line_pkg_dict.get(i)]) > 1
and next_line(i).startswith(("import", "from"))
and output_lines[-1] != ''
):
output_lines.append("")
output_lines.append(line)
Expand Down
18 changes: 11 additions & 7 deletions lib/python/pyflyby/_importstmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from pyflyby._util import (Inf, cached_attribute, cmp,
longest_common_prefix)

from black import (find_pyproject_toml, format_str,
parse_pyproject_toml, TargetVersion,
FileMode as Mode)
from black import format_str, FileMode as Mode
from black.files import find_pyproject_toml, parse_pyproject_toml
from black.mode import TargetVersion


def read_black_config():
Expand Down Expand Up @@ -52,27 +52,27 @@ def read_black_config():


class ImportFormatParams(FormatParams):
align_imports = True
align_imports:bool = True
"""
Whether and how to align 'from modulename import aliases...'. If ``True``,
then the 'import' keywords will be aligned within a block. If an integer,
then the 'import' keyword will always be at that column. They will be
wrapped if necessary.
"""

from_spaces = 1
from_spaces:int = 1
"""
The number of spaces after the 'from' keyword. (Must be at least 1.)
"""

separate_from_imports = True
separate_from_imports:bool = True
"""
Whether all 'from ... import ...' in an import block should come after
'import ...' statements. ``separate_from_imports = False`` works well with
``from_spaces = 3``. ('from __future__ import ...' always comes first.)
"""

align_future = False
align_future:bool = False
"""
Whether 'from __future__ import ...' statements should be aligned with
others. If False, uses a single space after the 'from' and 'import'
Expand Down Expand Up @@ -124,6 +124,10 @@ class Import(object):
Import('from foo import bar')

"""

fullname:str
import_as:str

def __new__(cls, arg):
if isinstance(arg, cls):
return arg
Expand Down
46 changes: 43 additions & 3 deletions tests/tests_sorts.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pyflyby._parse import PythonBlock
from pyflyby._imports2s import sort_imports
from pyflyby._parse import PythonBlock
from pyflyby._imports2s import sort_imports, fix_unused_and_missing_imports
from textwrap import dedent
from lib.python.pyflyby._importstmt import ImportFormatParams
import pytest

code1 = dedent("""
Expand Down Expand Up @@ -45,10 +46,49 @@
import zz
""")

@pytest.mark.parametrize('code, expected',[(code1, expected1)])
# stable should not change
stable_1 = dedent(
"""
#!/usr/local/bin/python3

from deshaw.abc import DAYS
from deshaw.py import tuple

print(DAYS.days(20240101, 20241231))
print(tuple("hello"))
"""
)


@pytest.mark.parametrize("code, expected", [(code1, expected1)])
def test_sort_1(code, expected):

assert str(sort_imports(PythonBlock(code))) == expected
# expected is stable
assert str(sort_imports(PythonBlock(expected))) == expected


@pytest.mark.parametrize("code", [stable_1])
def test_stable(code):
params = ImportFormatParams(
align_imports=(32,),
from_spaces=3,
separate_from_imports=False,
max_line_length=None,
use_black=False,
align_future=False,
hanging_indent="never",
)
add_missing = False
remove_unused = False
add_mandatory = False

result = fix_unused_and_missing_imports(
PythonBlock(code),
params=params,
add_missing=add_missing,
remove_unused=remove_unused,
add_mandatory=add_mandatory,
)

assert str(result) == str(stable_1)
Loading