Exemple #1
0
def test_isort_should_warn_on_empty_custom_config_issue_1433(tmpdir):
    """Feedback should be provided when a user provides a custom settings file that has no
    discoverable configuration.
    See: https://github.com/PyCQA/isort/issues/1433
    """
    settings_file = tmpdir.join(".custom.cfg")
    settings_file.write(
        """
[settings]
quiet = true
"""
    )
    with pytest.warns(UserWarning):
        assert not Config(settings_file=str(settings_file)).quiet

    isort.settings._get_config_data.cache_clear()
    settings_file.write(
        """
[isort]
quiet = true
"""
    )
    with pytest.warns(None) as warning:
        assert Config(settings_file=str(settings_file)).quiet
    assert not warning
Exemple #2
0
def test_isort_warns_when_known_sections_dont_match_issue_1331():
    """Test to ensure that isort warns if there is a mismatch between sections and known_sections.
    See: https://github.com/pycqa/isort/issues/1331.
    """
    assert (
        isort.place_module(
            "bot_core",
            config=Config(
                known_robotlocomotion_upstream=["bot_core"],
                sections=["ROBOTLOCOMOTION_UPSTREAM", "THIRDPARTY"],
            ),
        )
        == "ROBOTLOCOMOTION_UPSTREAM"
    )
    with pytest.warns(UserWarning):
        assert (
            isort.place_module(
                "bot_core",
                config=Config(
                    known_robotlocomotion_upstream=["bot_core"],
                    sections=["ROBOTLOOMOTION_UPSTREAM", "THIRDPARTY"],
                ),
            )
            == "THIRDPARTY"
        )
    with pytest.warns(UserWarning):
        assert (
            isort.place_module(
                "bot_core", config=Config(known_robotlocomotion_upstream=["bot_core"])
            )
            == "THIRDPARTY"
        )
Exemple #3
0
def test_aliases():
    assert imports_in_code("import os as os")[0].alias == "os"
    assert not imports_in_code(
        "import os as os",
        config=Config(remove_redundant_aliases=True, ),
    )[0].alias

    assert imports_in_code("from os import path as path")[0].alias == "path"
    assert not imports_in_code(
        "from os import path as path",
        config=Config(remove_redundant_aliases=True))[0].alias
def test_isort_enables_deduping_section_headers_issue_953():
    """isort should provide a way to only have identical import headings show up once.
    See: https://github.com/pycqa/isort/issues/953
    """
    isort_code = partial(
        isort.code,
        config=Config(
            import_heading_firstparty="Local imports.",
            import_heading_localfolder="Local imports.",
            dedup_headings=True,
            known_first_party=["isort"],
        ),
    )

    assert (isort_code("from . import something") == """# Local imports.
from . import something
""")
    assert (isort_code("""from isort import y

from . import something""") == """# Local imports.
from isort import y

from . import something
""")
    assert isort_code("import os") == "import os\n"
Exemple #5
0
def test_isort_respects_quiet_from_sort_file_api_see_1461(capsys, tmpdir):
    """Test to ensure isort respects the quiet API parameter when passed in via the API.
    See: https://github.com/PyCQA/isort/issues/1461.
    """
    settings_file = tmpdir.join(".isort.cfg")
    custom_settings_file = tmpdir.join(".custom.isort.cfg")
    tmp_file = tmpdir.join("file.py")
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file)

    out, error = capsys.readouterr()
    assert not error
    assert "Fixing" in out

    # When passed in directly as a setting override
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file, quiet=True)
    out, error = capsys.readouterr()
    assert not error
    assert not out

    # Present in an automatically loaded configuration file
    isort.settings._find_config.cache_clear()
    settings_file.write(
        """
[isort]
quiet = true
"""
    )
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file)
    out, error = capsys.readouterr()
    assert not error
    assert not out

    # In a custom configuration file
    settings_file.write(
        """
[isort]
quiet = false
"""
    )
    custom_settings_file.write(
        """
[isort]
quiet = true
"""
    )
    tmp_file.write("import b\nimport a\n")
    isort.file(tmp_file, settings_file=str(custom_settings_file))
    out, error = capsys.readouterr()
    assert not error
    assert not out

    # Reused configuration object
    custom_config = Config(settings_file=str(custom_settings_file))
    isort.file(tmp_file, config=custom_config)
    out, error = capsys.readouterr()
    assert not error
    assert not out
Exemple #6
0
def git_hook(strict: bool = False,
             modify: bool = False,
             lazy: bool = False,
             settings_file: str = "") -> int:
    """
    Git pre-commit hook to check staged files for isort errors

    :param bool strict - if True, return number of errors on exit,
        causing the hook to fail. If False, return zero so it will
        just act as a warning.
    :param bool modify - if True, fix the sources if they are not
        sorted properly. If False, only report result without
        modifying anything.
    :param bool lazy - if True, also check/fix unstaged files.
        This is useful if you frequently use ``git commit -a`` for example.
        If False, ony check/fix the staged files for isort errors.
    :param str settings_file - A path to a file to be used as
                               the configuration file for this run.
        When settings_file is the empty string, the configuration file
        will be searched starting at the directory containing the first
        staged file, if any, and going upward in the directory structure.

    :return number of errors if in strict mode, 0 otherwise.
    """

    # Get list of files modified and staged
    diff_cmd = [
        "git", "diff-index", "--cached", "--name-only",
        "--diff-filter=ACMRTUXB", "HEAD"
    ]
    if lazy:
        diff_cmd.remove("--cached")

    files_modified = get_lines(diff_cmd)
    if not files_modified:
        return 0

    errors = 0
    config = Config(
        settings_file=settings_file,
        settings_path=os.path.dirname(os.path.abspath(files_modified[0])),
    )
    for filename in files_modified:
        if filename.endswith(".py"):
            # Get the staged contents of the file
            staged_cmd = ["git", "show", f":{filename}"]
            staged_contents = get_output(staged_cmd)

            try:
                if not api.check_code_string(staged_contents,
                                             file_path=Path(filename),
                                             config=config):
                    errors += 1
                    if modify:
                        api.sort_file(filename, config=config)
            except exceptions.FileSkipped:  # pragma: no cover
                pass

    return errors if strict else 0
def test_reformatter_class(
    tmp_pathplus: PathPlus,
    advanced_file_regression: AdvancedFileRegressionFixture,
    capsys,
    advanced_data_regression: AdvancedDataRegressionFixture,
    demo_environment,
    yapf_style,
    isort_config_file,
):

    isort_config = Config(settings_file=isort_config_file)
    r = Reformatter(tmp_pathplus / "code.py",
                    yapf_style=yapf_style,
                    isort_config=isort_config)

    with pytest.raises(ValueError,
                       match=r"'Reformatter.run\(\)' must be called first!"):
        r.to_string()

    with pytest.raises(ValueError,
                       match=r"'Reformatter.run\(\)' must be called first!"):
        r.to_file()

    with pytest.raises(ValueError,
                       match=r"'Reformatter.run\(\)' must be called first!"):
        r.get_diff()

    assert r.run() == 1
    r.to_file()

    advanced_file_regression.check_file(tmp_pathplus / "code.py")
    advanced_file_regression.check(r.to_string(), extension="._py_")

    captured = capsys.readouterr()

    assert not captured.out
    assert not captured.err

    # Calling a second time shouldn't change anything
    r = Reformatter(tmp_pathplus / "code.py",
                    yapf_style=yapf_style,
                    isort_config=isort_config)
    assert r.run() == 0
    r.to_file()

    advanced_file_regression.check_file(tmp_pathplus / "code.py")
Exemple #8
0
def git_hook(strict: bool = False, modify: bool = False) -> int:
    """
    Git pre-commit hook to check staged files for isort errors

    :param bool strict - if True, return number of errors on exit,
        causing the hook to fail. If False, return zero so it will
        just act as a warning.
    :param bool modify - if True, fix the sources if they are not
        sorted properly. If False, only report result without
        modifying anything.

    :return number of errors if in strict mode, 0 otherwise.
    """

    # Get list of files modified and staged
    diff_cmd = [
        "git", "diff-index", "--cached", "--name-only",
        "--diff-filter=ACMRTUXB", "HEAD"
    ]
    files_modified = get_lines(diff_cmd)
    if not files_modified:
        return 0

    errors = 0
    config = Config(
        settings_path=os.path.dirname(os.path.abspath(files_modified[0])))
    for filename in files_modified:
        if filename.endswith(".py"):
            # Get the staged contents of the file
            staged_cmd = ["git", "show", f":{filename}"]
            staged_contents = get_output(staged_cmd)

            try:
                if not api.check_code_string(staged_contents,
                                             file_path=Path(filename),
                                             config=config):
                    errors += 1
                    if modify:
                        api.sort_file(filename, config=config)
            except exceptions.FileSkipped:  # pragma: no cover
                pass

    return errors if strict else 0
Exemple #9
0
def reformat_file(filename: PathLike, yapf_style: str,
                  isort_config_file: PathLike) -> int:
    """
	Reformat the given file.

	:param filename:
	:param yapf_style:
	:param isort_config_file:
	"""

    isort_config = Config(settings_file=str(isort_config_file))

    r = Reformatter(filename, yapf_style, isort_config)

    if r.run():
        click.echo(r.get_diff(), color=resolve_color_default())
        ret = 1
    else:
        ret = 0

    r.to_file()

    return ret
Exemple #10
0
	Returns a list of child attributes for the given object.

	:param obj:
	"""

    child_attrs = dir(obj)
    if "__init__" not in child_attrs:
        child_attrs.append("__init__")

    for child_attr_name in alpha_sort(child_attrs, alphabet=method_alphabet):
        if not is_dunder(child_attr_name) or child_attr_name == "__init__":
            if child_attr_name not in SKIP_ATTRS:
                yield child_attr_name


isort_config = Config(force_single_line=True)
method_alphabet = f"_{string.ascii_uppercase}{string.ascii_lowercase}{ascii_digits}"

SYSTEM_MODULES = [
    "System",
    "System.Collections",
    "System.ComponentModel",
    "System.Configuration",
    "System.Configuration.Assemblies",
    "System.Data",
    "System.Globalization",
    "System.IO",
    "System.Reflection",
    "System.Runtime",
    "System.Runtime.CompilerServices",
    "System.Runtime.InteropServices",
Exemple #11
0
# stdlib
import pathlib
import re

# 3rd party
import isort  # type: ignore
from isort import Config

isort_config = Config(settings_file=".isort.cfg")

for filename in pathlib.Path("dulwich-stubs").rglob("*.pyi"):
    # print(filename)
    file_contents = filename.read_text()
    for entry in re.findall(r"([A-Za-z0-9_.]+) as ([A-Za-z0-9_.]+)",
                            file_contents):
        if entry[0] == entry[1]:
            file_contents = file_contents.replace(f"{entry[0]} as {entry[1]}",
                                                  entry[0])

    file_contents = isort.code(file_contents, config=isort_config)
    filename.write_text(file_contents)