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

Use ruff format instead of black #427

Merged
merged 6 commits into from
May 13, 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
11 changes: 5 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ nox -R # Run all available sessions, while reusing virtualenvs (i.e.
nox -s tests # Run unit tests on supported Python versions (that are available)
nox -s tests-3.7 # Run unit tests on Python v3.7 (assuming it is available locally)
nox -s integration_tests-3.11 # Run integration tests on Python 3.11
nox -s lint # Run linters (mypy + ruff) on all supported Python versions
nox -s format # Check formatting (isort + black)
nox -s reformat # Fix formatting (isort + black)
nox -s lint # Run linters (mypy + ruff check) on all supported Python versions
nox -s format # Check formatting (ruff format)
nox -s reformat # Fix formatting (ruff format)
```

If you want to run a command individually, the corresponding session is defined inside
Expand All @@ -90,9 +90,8 @@ commands will work:
pytest # Run unit tests
pytest -m integration # Run integration tests
mypy # Run static type checking
ruff check . # Run ruff
isort fawltydeps tests # Fix sorting of import statements
black . # Fix code formatting
ruff check . # Run ruff linter
ruff format . # Run ruff formatter
```

#### Shortcut: Nix
Expand Down
4 changes: 2 additions & 2 deletions docs/CodeDesign.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ We value composability and functional style.

We want the code to be readable, testable and maintainable. That is why we use:

- code formatter `black` to unify the code style,
- `ruff` and `isort` for linting
- code formatter `ruff format` to unify the code style,
- `ruff check` for linting
- `mypy` for typecheck
- `pytest` for testing
to ensure this quality.
Expand Down
1 change: 1 addition & 0 deletions fawltydeps/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""FawltyDeps configuration and command-line options."""

from __future__ import annotations

import argparse
Expand Down
1 change: 1 addition & 0 deletions fawltydeps/traverse_project.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Traverse a project to identify appropriate inputs to FawltyDeps."""

import logging
from pathlib import Path
from typing import AbstractSet, Iterator, Optional, Set, Tuple, Type, Union
Expand Down
6 changes: 2 additions & 4 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,11 @@ def lint(session):
def format(session): # noqa: A001
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--enable-colors")
session.run("isort", "fawltydeps", "tests", "--check", "--diff", "--color")
session.run("black", ".", "--check", "--diff", "--color")
session.run("ruff", "format", "--diff", ".")


@nox.session
def reformat(session):
install_groups(session, include=["format"], include_self=False)
session.run("codespell", "--write-changes")
session.run("isort", "fawltydeps", "tests")
session.run("black", ".")
session.run("ruff", "format", ".")
65 changes: 1 addition & 64 deletions poetry.lock

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

24 changes: 3 additions & 21 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,8 @@ types-setuptools = "^65.6.0.2"
optional = true

[tool.poetry.group.format.dependencies]
black = {version = "^22", extras = ["colorama"]}
isort = [
# isort 5.12.0 drops support for Python v3.7:
{version = "^5.10", extras = ["colors"], python = ">=3.8"},
{version = ">=5.10,<5.12.0", extras = ["colors"], python = "<3.8"},
]
codespell = "^2.2.4"
ruff = ">=0.3"

[tool.poetry.group.dev]
optional = true
Expand All @@ -99,7 +94,6 @@ optional = true
# something to the above groups (i.e. something that targets a specific purpose
# (e.g. a CI action), consider whether it's also useful to have this available
# in a developers environment
black = {version = "^22", extras = ["colorama"]}
codespell = "^2.2.4"
hypothesis = "^6.68.2"
mypy = "^1.0.1"
Expand All @@ -108,12 +102,6 @@ pytest = "^7.1.0"
ruff = ">=0.3"
types-setuptools = "^65.6.0.2"

[tool.black]
target-version = ["py37"]

[tool.isort]
profile = "black"

[tool.mypy]
files = ['*.py', 'fawltydeps/*.py', 'tests/*.py']
plugins = ["pydantic.mypy"]
Expand Down Expand Up @@ -145,9 +133,6 @@ addopts = "-m 'not integration'"
cache_dir = "~/.cache/pytest"

[tool.ruff]
# Black uses line-length = 88, but allows exceptions when breaking the line
# would lead to other rule violations. Use 100 as a maximum hard limit:
line-length = 100
target-version = "py37"
extend-include = ["*.ipynb"]
extend-exclude = [
Expand Down Expand Up @@ -183,10 +168,8 @@ ignore = [
"UP006", # Use `list` instead of `List` for type annotation
"UP007", # Use `X | Y` for type annotations

# Ruff recommends avoiding these checks when using `ruff format`. Since
# `ruff format` is a drop-in replacement for `black`, we avoid the same
# checks here (https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
# has more details):
# Ruff recommends avoiding these checks when using `ruff format`.
# Details: https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules
"W191", # tab-indentation
"E111", # indentation-with-invalid-multiple
"E114", # indentation-with-invalid-multiple-comment
Expand Down Expand Up @@ -247,7 +230,6 @@ build-backend = "poetry.core.masonry.api"
code = ["fawltydeps"]
deps = ["pyproject.toml"]
ignore_unused = [
"black",
"codespell",
"hypothesis",
"mypy",
Expand Down
19 changes: 7 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Fixtures for tests."""

import venv
from pathlib import Path
from tempfile import mkdtemp
Expand Down Expand Up @@ -171,33 +172,27 @@ def format_setup_py(deps: Deps, extras: ExtraDeps) -> str:

def format_setup_cfg(deps: Deps, no_extras: ExtraDeps) -> str:
assert not no_extras # not supported
return (
dedent(
"""\
return dedent(
"""\
[metadata]
name = "MyLib"

[options]
install_requires =
"""
)
+ "\n".join(f" {d}" for d in deps)
)
) + "\n".join(f" {d}" for d in deps)

def format_pyproject_toml(deps: Deps, extras: ExtraDeps) -> str:
return (
dedent(
f"""\
return dedent(
f"""\
[project]
name = "MyLib"

dependencies = {deps!r}

[project.optional-dependencies]
"""
)
+ "\n".join(f"{k} = {v!r}" for k, v in extras.items())
)
) + "\n".join(f"{k} = {v!r}" for k, v in extras.items())

def format_deps(
filename: str, all_deps: Union[Deps, Tuple[Deps, ExtraDeps]]
Expand Down
4 changes: 3 additions & 1 deletion tests/project_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Common helpers shared between test_real_project and test_sample_projects."""

from __future__ import annotations

import hashlib
Expand Down Expand Up @@ -370,7 +371,8 @@ class BaseProject(ABC):

@staticmethod
def _init_args_from_toml(
toml_data: TomlData, ExperimentClass: Type[BaseExperiment] # noqa: N803
toml_data: TomlData,
ExperimentClass: Type[BaseExperiment], # noqa: N803
) -> Dict[str, Any]:
"""Extract members from TOML into kwargs for a subclass constructor."""
# We ultimately _trust_ the .toml files read here, so we can skip all
Expand Down
1 change: 1 addition & 0 deletions tests/test_analysis.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Verify behavior of the Analysis class."""

# ruff: noqa: PLR2004,SLF001
from fawltydeps.main import calculated_once

Expand Down
1 change: 1 addition & 0 deletions tests/test_cmdline_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
The strategy construction can be viewed as a reference
to how the CLI options are expected to be used.
"""

import io
import os
import string
Expand Down
1 change: 1 addition & 0 deletions tests/test_compare_imports_to_dependencies.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test the imports to dependencies comparison function."""

import logging

import pytest
Expand Down
4 changes: 3 additions & 1 deletion tests/test_dir_traversal.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test core functionality of DirectoryTraversal class."""

import sys
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
Expand Down Expand Up @@ -1039,7 +1040,8 @@ def test_DirectoryTraversal_w_abs_paths(vector: DirectoryTraversalVector, tmp_pa
"vector", [pytest.param(v, id=v.id) for v in directory_traversal_vectors]
)
def test_DirectoryTraversal_w_rel_paths(
vector: DirectoryTraversalVector, inside_tmp_path # noqa: ARG001
vector: DirectoryTraversalVector,
inside_tmp_path, # noqa: ARG001
):
traversal = vector.setup(Path()) # Traverse relatively from inside tmp_path
vector.verify_traversal(traversal, Path())
Expand Down
1 change: 1 addition & 0 deletions tests/test_extract_declared_dependencies_pyproject_toml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test extracting dependencies from pyproject.toml."""

import logging
from dataclasses import dataclass, field
from typing import List
Expand Down
1 change: 1 addition & 0 deletions tests/test_extract_declared_dependencies_success.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test that dependencies are parsed from requirements files."""

from textwrap import dedent

import pytest
Expand Down
1 change: 1 addition & 0 deletions tests/test_extract_imports_simple.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test that we can extract simple imports from Python code."""

import json
import logging
from io import BytesIO
Expand Down
10 changes: 7 additions & 3 deletions tests/test_install_deps.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Verify behavior of TemporaryPipInstallResolver."""

import logging

import pytest
Expand Down Expand Up @@ -26,7 +27,8 @@ def test_resolve_dependencies_install_deps__via_local_cache(local_pypi): # noqa


def test_resolve_dependencies_install_deps__raises_unresolved_error_on_pip_install_failure(
caplog, local_pypi # noqa: ARG001
caplog,
local_pypi, # noqa: ARG001
):
# This tests the case where TemporaryPipInstallResolver encounters the
# inevitable pip install error and returns to resolve_dependencies()
Expand All @@ -42,7 +44,8 @@ def test_resolve_dependencies_install_deps__raises_unresolved_error_on_pip_insta


def test_resolve_dependencies_install_deps__unresolved_error_only_warns_failing_packages(
caplog, local_pypi # noqa: ARG001
caplog,
local_pypi, # noqa: ARG001
):
# When we fail to install _some_ - but not all - packages, the error message
# should only mention the packages that we failed to install.
Expand All @@ -58,7 +61,8 @@ def test_resolve_dependencies_install_deps__unresolved_error_only_warns_failing_


def test_resolve_dependencies_install_deps_on_mixed_packages__raises_unresolved_error(
caplog, local_pypi # noqa: ARG001
caplog,
local_pypi, # noqa: ARG001
):
caplog.set_level(logging.DEBUG)
deps = {"click", "does_not_exist", "leftpadx"}
Expand Down
1 change: 1 addition & 0 deletions tests/test_local_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Verify behavior of package module looking at a given Python environment."""

import sys
import venv
from pathlib import Path
Expand Down
1 change: 1 addition & 0 deletions tests/test_real_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
FawltyDeps on them, with hardcoded expectations per project on what FawltyDeps
should find/report.
"""

from __future__ import annotations

import json
Expand Down
1 change: 1 addition & 0 deletions tests/test_sample_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
├── expected.toml (mandatory)
└── ... (regular Python project)
"""

from __future__ import annotations

from dataclasses import dataclass
Expand Down
Loading
Loading