Skip to content
This repository has been archived by the owner on Jan 29, 2024. It is now read-only.

Commit

Permalink
Merge pull request #269 from kjaymiller/kjaymiller/issue259
Browse files Browse the repository at this point in the history
Replaces Black with Ruff Format
  • Loading branch information
kjaymiller authored Nov 28, 2023
2 parents 8c6d08f + 299371b commit 6bcc89d
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ source venv/bin/activate
2. Install necessary files

```sh
python -m pip install cruft packaging ruff black
python -m pip install cruft packaging ruff
```

3. Generate the project using this template
Expand Down
16 changes: 6 additions & 10 deletions hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,30 +120,26 @@ def check_for_files() -> None:
def error_msg(pkg: str) -> str:
return f"`{pkg}` is not installed. Run `pip install {pkg}` to install it."

def run_ruff_fix_and_black() -> None:
"""checks if ruff and black are installed and runs them on the project"""
def run_ruff_lint_and_format() -> None:
"""checks if ruff are installed and runs it on the project"""
if importlib.util.find_spec("ruff"):
subprocess.run(["python3", "-m" "ruff", "--fix", "src"])
subprocess.run(["python3", "-m" "ruff", "check", "--fix", "src"])
subprocess.run(["python3", "-m", "ruff", "format", "src"])
else:
logging.warning(error_msg("ruff"))

if importlib.util.find_spec("black"):
subprocess.run(["python3", "-m", "black", "src", "-q", "--config", "pyproject.toml"])
else:
logging.warning(error_msg("black"))

def run_bicep_format() -> None:
"""formats your bicep files"""
subprocess.run(["az", "bicep", "format", "--file", "infra/main.bicep"])


def lint() -> None:
"""Runs all linters"""
run_ruff_fix_and_black()
run_ruff_lint_and_format()
run_bicep_format()

if __name__ == "__main__":
rich.print("Removing unecessary files")
check_for_files()
rich.print("Linting files")
rich.print("Linting and formatting files")
lint()
3 changes: 1 addition & 2 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ cookiecutter
packaging

# linting
ruff
black
ruff<=0.1.6

# testing
pytest
Expand Down
17 changes: 3 additions & 14 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.12
# by the following command:
#
# pip-compile requirements.in
Expand All @@ -8,18 +8,14 @@ arrow==1.2.3
# via cookiecutter
binaryornot==0.4.4
# via cookiecutter
black==23.7.0
# via -r requirements.in
certifi==2023.7.22
# via requests
chardet==5.2.0
# via binaryornot
charset-normalizer==3.2.0
# via requests
click==8.1.6
# via
# black
# cookiecutter
# via cookiecutter
cookiecutter==2.3.0
# via
# -r requirements.in
Expand All @@ -36,17 +32,10 @@ markupsafe==2.1.3
# via jinja2
mdurl==0.1.2
# via markdown-it-py
mypy-extensions==1.0.0
# via black
packaging==23.1
# via
# -r requirements.in
# black
# pytest
pathspec==0.11.2
# via black
platformdirs==3.10.0
# via black
pluggy==1.2.0
# via pytest
pygments==2.16.1
Expand All @@ -67,7 +56,7 @@ requests==2.31.0
# via cookiecutter
rich==13.5.2
# via cookiecutter
ruff==0.0.284
ruff==0.1.6
# via -r requirements.in
six==1.16.0
# via python-dateutil
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cookiecutter_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_project_generation(bakery):
def test_project_post_hook_triggers_warning_if_linters_not_installed(
cookies_session, default_context, mocker, caplog
):
"""If ruff or black is not installed, the post hook should trigger a warning"""
"""If ruff is not installed, the post hook should trigger a warning"""

mocker.patch("hooks.post_gen_project.importlib.util.find_spec", return_value=None)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"ms-azuretools.vscode-bicep",
"charliermarsh.ruff",
"ms-python.python",
"ms-python.black-formatter",
{% if cookiecutter.project_host == "aca" %}
"ms-azuretools.vscode-docker",
{% endif %}
Expand Down Expand Up @@ -55,6 +54,14 @@
".coverage": true,
".pytest_cache": true,
"__pycache__": true
},
[python]: {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": true,
"source.fixAll": true
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ jobs:
python -m pip install --upgrade pip
pip install -r requirements-dev.in
- name: Lint with ruff
run: ruff .
- name: Check formatting with black
run: black . --check --verbose
run: |
ruff check .
ruff format.
17 changes: 1 addition & 16 deletions {{cookiecutter.__src_folder_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
line-length = 120
select = ["E", "F", "I", "UP"]
ignore = ["D203"]
extend-exclude = ["flaskapp/migrations/"]

[tool.ruff.isort]
{% if cookiecutter.project_backend == "flask" %}
Expand All @@ -11,22 +12,6 @@ known-first-party = ["flaskapp"]
known-first-party = ["fastapi_app"]
{% endif %}

[tool.black]
line-length = 120
{% if cookiecutter.project_backend == "flask" %}
extend-exclude = '''
(
flaskapp/migrations/
)
'''
force-exclude = '''
# Needed for pre-commit
(
flaskapp/migrations/
)
'''
{% endif %}

[tool.pytest.ini_options]
addopts = "-ra -vv"
{% if cookiecutter.project_backend == "django" %}
Expand Down
1 change: 0 additions & 1 deletion {{cookiecutter.__src_folder_name}}/requirements-dev.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,3 @@ pytest-playwright

# Linters
ruff
black

0 comments on commit 6bcc89d

Please sign in to comment.