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

Lint Python code with ruff #2359

Merged
merged 2 commits into from
Feb 19, 2025
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
1 change: 1 addition & 0 deletions docs/contributing/2.-coding-standard.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ All code submitted to hug should run through the following tools:
- Flake8
- flake8-bugbear
- Bandit
- ruff
- pep8-naming
- vulture
- safety
2 changes: 1 addition & 1 deletion isort/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ def _with_straight_imports(
) -> List[str]:
output: List[str] = []

as_imports = any((module in parsed.as_map["straight"] for module in straight_modules))
as_imports = any(module in parsed.as_map["straight"] for module in straight_modules)

# combine_straight_imports only works for bare imports, 'as' imports not included
if config.combine_straight_imports and not as_imports:
Expand Down
6 changes: 3 additions & 3 deletions isort/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,11 +892,11 @@ def _get_config_data(file_path: str, sections: Tuple[str, ...]) -> Dict[str, Any

for key, value in settings.items():
existing_value_type = _get_str_to_type_converter(key)
if existing_value_type == tuple:
if existing_value_type is tuple:
settings[key] = tuple(_as_list(value))
elif existing_value_type == frozenset:
elif existing_value_type is frozenset:
settings[key] = frozenset(_as_list(settings.get(key))) # type: ignore
elif existing_value_type == bool:
elif existing_value_type is bool:
# Only some configuration formats support native boolean values.
if not isinstance(value, bool):
value = _as_bool(value)
Expand Down
44 changes: 44 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ dev = [
"pytest-benchmark>=3.4.1",
"pytest-mock>=1.10",
"requirementslib>=1.5",
"ruff>=0.9.6",
"safety>=2.2.0",
"stdlibs>=2024.10.21.16",
"toml>=0.10.2",
Expand Down Expand Up @@ -169,6 +170,49 @@ allow_untyped_defs = true
allow_incomplete_defs = true
allow_untyped_calls = true

[tool.ruff]
line-length = 100
lint.select = [
"ASYNC",
"B",
"C4",
"C90",
"E",
"F",
"FLY",
"PERF",
"PIE",
"PLC",
"PLE",
"RUF",
"S",
"UP",
"W",
]
lint.ignore = [
"B017",
"B028",
"B904",
"E203",
"E501",
"PERF203",
"RUF100",
"UP006",
"UP035",
]
lint.exclude = [ "isort/_vendored/*" ]
lint.mccabe.max-complexity = 91 # Default is 10

[tool.ruff.lint.per-file-ignores]
"isort/deprecated/finders.py" = [ "UP034" ]
"isort/hooks.py" = [ "S603" ]
"isort/output.py" = [ "PLC0206" ]
"isort/settings.py" = [ "PLC0414", "S603", "S607" ]
"isort/setuptools_commands.py" = [ "RUF012" ]
"tests/*" = [ "RUF001", "S" ]
"tests/unit/example_crlf_file.py" = [ "F401" ]
"tests/unit/test_wrap_modes.py" = [ "PIE804" ]

[tool.isort]
profile = "hug"
src_paths = ["isort", "tests"]
Expand Down
2 changes: 1 addition & 1 deletion scripts/build_profile_docs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#! /bin/env python
import os
from typing import Any, Dict, Generator, Iterable, Type
from typing import Any, Dict

from isort.profiles import profiles

Expand Down
12 changes: 5 additions & 7 deletions scripts/check_acknowledgments.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ async def main():
)
results = response.json()
contributors.extend(
(
contributor
for contributor in results
if contributor["type"] == GITHUB_USER_TYPE
and contributor["login"] not in IGNORED_AUTHOR_LOGINS
and f"@{contributor['login'].lower()}" not in ACKNOWLEDGEMENTS
)
contributor
for contributor in results
if contributor["type"] == GITHUB_USER_TYPE
and contributor["login"] not in IGNORED_AUTHOR_LOGINS
and f"@{contributor['login'].lower()}" not in ACKNOWLEDGEMENTS
)

unacknowledged_users = await asyncio.gather(
Expand Down
1 change: 1 addition & 0 deletions scripts/lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ uv run black --target-version py39 --check .
uv run isort --profile hug --check --diff isort/ tests/
uv run isort --profile hug --check --diff example_*/
uv run --with=Flake8-pyproject flake8 isort/ tests/
uv run ruff check
# 51457: https://github.com/tiangolo/typer/discussions/674
# 72715: https://github.com/timothycrosley/portray/issues/95
uv run safety check -i 72715 -i 51457 -i 59587
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def setup_class(self):

def test_variables(self):
assert self.instance.code == "x = ["
assert self.instance.original_error == SyntaxError
assert self.instance.original_error is SyntaxError


class TestLiteralSortTypeMismatch(TestISortError):
Expand All @@ -96,8 +96,8 @@ def setup_class(self):
)

def test_variables(self):
assert self.instance.kind == tuple
assert self.instance.expected_kind == list
assert self.instance.kind is tuple
assert self.instance.expected_kind is list


class TestAssignmentsFormatMismatch(TestISortError):
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ def test_import_by_paren_issue_460() -> None:
import io
import os
"""
assert isort.code((test_input)) == test_input
assert isort.code(test_input) == test_input


def test_function_with_docstring() -> None:
Expand Down Expand Up @@ -3783,7 +3783,7 @@ def test_command_line(tmpdir, capfd, multiprocess: bool) -> None:

tmpdir.join("file1.py").write("import re\nimport os\n\nimport contextlib\n\n\nimport isort")
tmpdir.join("file2.py").write(
("import collections\nimport time\n\nimport abc" "\n\n\nimport isort")
"import collections\nimport time\n\nimport abc" "\n\n\nimport isort"
)
arguments = [str(tmpdir), "--settings-path", os.getcwd()]
if multiprocess:
Expand Down
27 changes: 27 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading