Skip to content

Commit

Permalink
Replace linter and formatter with ruff and mypy (#392)
Browse files Browse the repository at this point in the history
  • Loading branch information
lan496 committed Jan 18, 2024
2 parents ba444f4 + 6a7936b commit 5ce87bc
Show file tree
Hide file tree
Showing 19 changed files with 435 additions and 200 deletions.
45 changes: 16 additions & 29 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exclude: src\/msg_database.c
fail_fast: false
repos:
- repo: https://github.com/Takishima/cmake-pre-commit-hooks
rev: v1.5.3
rev: v1.9.5
hooks:
- id: clang-format
additional_dependencies: [ clang-format >= 16 ]
Expand All @@ -23,40 +23,27 @@ repos:
- '-DSPGLIB_WITH_TESTS=ON'
stages: [ manual ]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-added-large-files
- repo: https://github.com/pycqa/flake8
rev: 5.0.4
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.13
hooks:
- id: flake8
files: python\/.*
args:
- "--max-line-length=88"
- "--ignore=E203,W503"
- repo: https://github.com/psf/black
rev: 22.6.0
hooks:
- id: black
files: python\/.*
args: [ "--line-length=88" ]
- repo: https://github.com/pycqa/pydocstyle
rev: 6.1.1
hooks:
- id: pydocstyle
files: python/spglib
additional_dependencies: [ "toml" ]
- repo: https://github.com/pycqa/isort
rev: 5.12.0
- id: ruff
args: [ "--fix", "--show-fixes" ]
- id: ruff-format
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.8.0
hooks:
- id: isort
files: python\/.*
args: [ "--profile", "black" ]
- id: mypy
pass_filenames: false
additional_dependencies:
- types-PyYAML
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.16
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies:
Expand All @@ -66,14 +53,14 @@ repos:
# TODO: Temporarily separate docs formatiing
# https://github.com/executablebooks/mdformat/issues/234
- repo: https://github.com/executablebooks/mdformat
rev: 0.7.16
rev: 0.7.17
hooks:
- id: mdformat
additional_dependencies:
- mdformat-myst
files: doc\/(?!README).*\.md$
- repo: https://github.com/python-jsonschema/check-jsonschema
rev: 0.21.0
rev: 0.27.3
hooks:
- id: check-github-workflows
- id: check-readthedocs
Expand Down
13 changes: 5 additions & 8 deletions doc/conf.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
from importlib.metadata import version
from spglib import __version__ as version

project = "Spglib"
copyright = "2009, Atsushi Togo"
release = version("spglib") # e.g. "v.1.16.5"
# The short X.Y version.
version = ".".join(release.split(".")[:3]) # e.g. "v.1.16"

extensions = [
'sphinx.ext.mathjax',
"sphinx.ext.mathjax",
"sphinx.ext.viewcode",
"sphinxcontrib.bibtex",
"myst_parser",
Expand Down Expand Up @@ -54,7 +51,7 @@
# -----------------------------------------------------------------------------

html_theme = "sphinx_book_theme"
html_title = f"Spglib v{release}"
html_title = f"Spglib v{version}"
html_theme_options = {
# https://sphinx-book-theme.readthedocs.io/en/latest/reference.html
"repository_url": "https://github.com/spglib/spglib",
Expand All @@ -77,9 +74,9 @@
{
"path": "../python/spglib/spglib.py",
"module": "spglib",
}
},
]
autodoc2_hidden_objects = ['undoc', 'dunder', 'private', 'inherited']
autodoc2_hidden_objects = ["undoc", "dunder", "private", "inherited"]
autodoc2_hidden_regexes = [
"spglib.get_pointgroup",
# Layer group
Expand Down
35 changes: 24 additions & 11 deletions example/python_api/example_full.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def _show_lattice(lattice):
axis,
)
+ tuple(vec)
)
),
)


Expand Down Expand Up @@ -144,16 +144,15 @@ def main():
print("")
print("[get_pointgroup]")
print(
" Pointgroup of MgB2 is %s."
% spglib.get_pointgroup(symmetry["rotations"])[0]
" Pointgroup of MgB2 is %s." % spglib.get_pointgroup(symmetry["rotations"])[0],
)
print("")

dataset = spglib.get_symmetry_dataset(rutile)
print("[get_symmetry_dataset] ['international']")
print(
" Spacegroup of Rutile is %s (%d)."
% (dataset["international"], dataset["number"])
% (dataset["international"], dataset["number"]),
)
print("")
print("[get_symmetry_dataset] ['pointgroup']")
Expand All @@ -162,7 +161,7 @@ def main():
print("[get_symmetry_dataset] ['hall']")
print(
" Hall symbol of Rutile is %s (%d)."
% (dataset["hall"], dataset["hall_number"])
% (dataset["hall"], dataset["hall_number"]),
)
print("")
print("[get_symmetry_dataset] ['wyckoffs']")
Expand All @@ -176,7 +175,7 @@ def main():
print("[get_symmetry_dataset] ['rotations'], ['translations']")
print(" Symmetry operations of Rutile unitcell are:")
for i, (rot, trans) in enumerate(
zip(dataset["rotations"], dataset["translations"])
zip(dataset["rotations"], dataset["translations"]),
):
print(" --------------- %4d ---------------" % (i + 1))
print(" rotation:")
Expand All @@ -202,7 +201,10 @@ def main():
print(" Standardize distorted rutile structure:")
print(" (to_primitive=0 and no_idealize=0)")
lattice, positions, numbers = spglib.standardize_cell(
rutile_dist, to_primitive=0, no_idealize=0, symprec=1e-1
rutile_dist,
to_primitive=0,
no_idealize=0,
symprec=1e-1,
)
_show_cell(lattice, positions, numbers)
print("")
Expand All @@ -211,7 +213,10 @@ def main():
print(" Standardize distorted rutile structure:")
print(" (to_primitive=0 and no_idealize=1)")
lattice, positions, numbers = spglib.standardize_cell(
rutile_dist, to_primitive=0, no_idealize=1, symprec=1e-1
rutile_dist,
to_primitive=0,
no_idealize=1,
symprec=1e-1,
)
_show_cell(lattice, positions, numbers)
print("")
Expand All @@ -220,7 +225,10 @@ def main():
print(" Standardize distorted silicon structure:")
print(" (to_primitive=1 and no_idealize=0)")
lattice, positions, numbers = spglib.standardize_cell(
silicon_dist, to_primitive=1, no_idealize=0, symprec=1e-1
silicon_dist,
to_primitive=1,
no_idealize=0,
symprec=1e-1,
)
_show_cell(lattice, positions, numbers)
print("")
Expand All @@ -229,7 +237,10 @@ def main():
print(" Standardize distorted silicon structure:")
print(" (to_primitive=1 and no_idealize=1)")
lattice, positions, numbers = spglib.standardize_cell(
silicon_dist, to_primitive=1, no_idealize=1, symprec=1e-1
silicon_dist,
to_primitive=1,
no_idealize=1,
symprec=1e-1,
)
_show_cell(lattice, positions, numbers)
print("")
Expand Down Expand Up @@ -257,7 +268,9 @@ def main():
print("")

mapping, grid = spglib.get_ir_reciprocal_mesh(
[11, 11, 11], silicon_prim, is_shift=[0, 0, 0]
[11, 11, 11],
silicon_prim,
is_shift=[0, 0, 0],
)
num_ir_kpt = len(np.unique(mapping))
print("[get_ir_reciprocal_mesh]")
Expand Down
75 changes: 75 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,78 @@ command_line = '-m pytest'
# TODO: Cannot report relative to project path because we cannot do editable installs yet
#source = ['python/spglib']
source = ['spglib']

[tool.ruff]
line-length = 88
extend-select = [
"F", # pyflakes
# "W", # pycodestyle-warnings
"E", # pycodestyle-errors
# "C90", # mccabe
"I", # isort
# "N", # pep8-naming
"D", # pydocstyle
# "UP", # pyupgrade
# "YTT", # flake8-2020
# "ANN", # flake8-annotations
# "S", # flake8-bandit
# "BLE", # flake8-blind-except
# "FBT", # flake8-boolean-trap
# "B", # flake8-bugbear
# "A", # flake8-builtins
# "COM", # flake8-comma
# "C4", # flake8-comprehensions
# "T10", # flake8-debugger
# "EM", # flake8-errmsg
# "FA", # flake8-future-annotations
# "ISC", # flake8-implicit-str-concat
# "ICN", # flake8-import-conventions
# "G", # flake8-logging-format
# "PIE", # flake8-pie
# "T20", # flake8-print
# "PYI", # flake8-pyi
# "PT", # flake8-pytest-style
# "Q", # flake8-quotes
# "RSE", # flake8-raise
# "RET", # flake8-return
# "SLF", # flake8-self
# "SIM", # flake8-simplify
# "TID", # flake8-tidy-imports
# "TCH", # flake8-type-checking
# "ARG", # flake8-unused-arguments
# "PTH", # flake8-use-pathlib
# "TD", # flake8-todos
# "ERA", # eradicate
# "PD", # pandas-vet
# "PGH", # pygrep-hooks
# "PL", # pylint
# "TRY", # tryceratops
# "FLY", # flynt
# "NPY", # NumPy
# "PERF", # perflint
# "FURB", # refurb
# "RUF", # Ruff-specific
]
extend-ignore = [
"D100",
"D101",
"D102",
"D103",
"D203", # Confilct with D211
"D205",
"D213", # Conflict with D212
]
exclude = [
"database",
]

[tool.mypy]
mypy_path = ["python"]
files = ["python", "test/functional/python", "example/python_api"]
python_version = "3.12"
warn_unused_configs = true
show_error_codes = true
# TODO: Enable strict mode once we clean up type annotations
# strict = true
enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"]
no_strict_optional = true

0 comments on commit 5ce87bc

Please sign in to comment.