Skip to content

Commit

Permalink
Switch to ruff pre-commit, fix .project cookiecutter deprecation (#156)
Browse files Browse the repository at this point in the history
* switch to ruff for better user exerience

* fix test improve error message

* fix deprecation warnings

* Update {{cookiecutter.plugin_name}}/pyproject.toml

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>

---------

Co-authored-by: Juan Nunez-Iglesias <jni@fastmail.com>
  • Loading branch information
Czaki and jni authored Apr 8, 2023
1 parent 6b482d6 commit 0d0662e
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 45 deletions.
51 changes: 27 additions & 24 deletions tests/test_create_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def test_run_cookiecutter_and_plugin_tests(cookies, capsys):

assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == "foo-bar"
assert result.project.isdir()
assert result.project.join("src").isdir()
assert result.project.join("src", "foo_bar", "__init__.py").isfile()
assert result.project.join("src", "foo_bar", "_tests", "test_reader.py").isfile()
assert result.project_path.name == "foo-bar"
assert result.project_path.is_dir()
assert result.project_path.joinpath("src").is_dir()
assert result.project_path.joinpath("src", "foo_bar", "__init__.py").is_file()
assert result.project_path.joinpath("src", "foo_bar", "_tests", "test_reader.py").is_file()

run_tox(str(result.project))
run_tox(str(result.project_path))


def test_run_cookiecutter_and_plugin_tests_with_napari_prefix(cookies, capsys):
Expand All @@ -41,11 +41,11 @@ def test_run_cookiecutter_and_plugin_tests_with_napari_prefix(cookies, capsys):

assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == "napari-foo"
assert result.project.isdir()
assert result.project.join("src").isdir()
assert result.project.join("src", "napari_foo", "__init__.py").isfile()
assert result.project.join("src", "napari_foo", "_tests", "test_reader.py").isfile()
assert result.project_path.name == "napari-foo"
assert result.project_path.is_dir()
assert result.project_path.joinpath("src").is_dir()
assert result.project_path.joinpath("src", "napari_foo", "__init__.py").is_file()
assert result.project_path.joinpath("src", "napari_foo", "_tests", "test_reader.py").is_file()


def test_run_cookiecutter_select_plugins(cookies, capsys):
Expand All @@ -60,20 +60,20 @@ def test_run_cookiecutter_select_plugins(cookies, capsys):

assert result.exit_code == 0
assert result.exception is None
assert result.project.basename == "anything"
assert result.project.isdir()
assert result.project.join("src").isdir()
assert result.project.join("src", "anything", "__init__.py").isfile()
assert result.project.join("src", "anything", "_tests", "test_reader.py").isfile()

assert not result.project.join("src", "anything", "_widget.py").isfile()
assert not result.project.join(
assert result.project_path.name == "anything"
assert result.project_path.is_dir()
assert result.project_path.joinpath("src").is_dir()
assert result.project_path.joinpath("src", "anything", "__init__.py").is_file()
assert result.project_path.joinpath("src", "anything", "_tests", "test_reader.py").is_file()

assert not result.project_path.joinpath("src", "anything", "_widget.py").is_file()
assert not result.project_path.joinpath(
"src", "anything", "_tests", "test_widget.py"
).isfile()
assert not result.project.join("src", "anything", "_writer.py").isfile()
assert not result.project.join(
).is_file()
assert not result.project_path.joinpath("src", "anything", "_writer.py").is_file()
assert not result.project_path.joinpath(
"src", "anything", "_tests", "test_writer.py"
).isfile()
).is_file()


@pytest.mark.parametrize("include_reader_plugin", ["y", "n"])
Expand All @@ -92,4 +92,7 @@ def test_pre_commit_validity(cookies, include_reader_plugin, include_writer_plug
}
)
result.project_path.joinpath("setup.cfg").is_file()
subprocess.run(["pre-commit", "run", "--all"], cwd=str(result.project_path), check=True, capture_output=True)
try:
subprocess.run(["pre-commit", "run", "--all", "--show-diff-on-failure"], cwd=str(result.project_path), check=True, capture_output=True)
except subprocess.CalledProcessError as e:
pytest.fail(f"pre-commit failed with output:\n{e.stdout.decode()}\nerrror:\n{e.stderr.decode()}")
22 changes: 4 additions & 18 deletions {{cookiecutter.plugin_name}}/.pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,15 @@ repos:
- id: end-of-file-fixer
- id: trailing-whitespace
exclude: ^\.napari-hub/.*
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
- id: check-yaml # checks for correct yaml syntax for github actions ex.
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.256
hooks:
- id: isort
- repo: https://github.com/asottile/pyupgrade
rev: v2.32.1
hooks:
- id: pyupgrade
args: [--py38-plus, --keep-runtime-typing]
- repo: https://github.com/myint/autoflake
rev: v1.4
hooks:
- id: autoflake
args: ["--in-place", "--remove-all-unused-imports"]
- id: ruff
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies: [flake8-typing-imports>=1.9.0]
- repo: https://github.com/tlambert03/napari-plugin-checks
rev: v0.3.0
hooks:
Expand Down
50 changes: 47 additions & 3 deletions {{cookiecutter.plugin_name}}/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,51 @@ write_to = "src/{{cookiecutter.module_name}}/_version.py"

[tool.black]
line-length = 79
target-version = ['py38', 'py39', 'py310']

[tool.isort]
profile = "black"
line_length = 79

[tool.ruff]
line-length = 79
select = [
"E", "F", "W", #flake8
"UP", # pyupgrade
"I", # isort
"BLE", # flake8-blind-exception
"B", # flake8-bugbear
"A", # flake8-builtins
"C4", # flake8-comprehensions
"ISC", # flake8-implicit-str-concat
"G", # flake8-logging-format
"PIE", # flake8-pie
"SIM", # flake8-simplify
]
ignore = [
"E501", # line too long. let black handle this
"UP006", "UP007", # type annotation. As using magicgui require runtime type annotation then we disable this.
"SIM117", # flake8-simplify - some of merged with statements are not looking great with black, reanble after drop python 3.9
]

exclude = [
".bzr",
".direnv",
".eggs",
".git",
".mypy_cache",
".pants.d",
".ruff_cache",
".svn",
".tox",
".venv",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"*vendored*",
"*_vendor*",
]

target-version = "py38"
fix = true

0 comments on commit 0d0662e

Please sign in to comment.