Skip to content

Commit

Permalink
refactor: use ruff (#217)
Browse files Browse the repository at this point in the history
ruff replaces `black`, `isort` & `pylint` for formatting & linting.
Note that `isort` is still used to sort imports when exporting to
notebook since ruff
does not provide a Python API.

Resolves #214
  • Loading branch information
mbelak-dtml authored Mar 7, 2024
1 parent 2b29337 commit eb78c67
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 91 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ jobs:
poetry run python -m pytest -n auto --disable-warnings --cov=edvart tests/
- name: Lint
run: |
poetry run pylint --rcfile=.pylintrc edvart/
poetry run black --check --line-length 100 edvart/ tests/
poetry run isort --check --line-length 100 --profile black edvart/ tests/
poetry run ruff check .
poetry run ruff format --check .
dismiss-stale-reviews:
runs-on: ubuntu-22.04
Expand Down
32 changes: 11 additions & 21 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,20 @@ If you add a new section, add the section description into `docs/sections.rst`

## Code style

* The line length is limited to 100 characters in Python code, except if it would make the code less readable.
* `black` is used for formatting Python code. The following command can be used to properly format the code:
* The line length is limited to 100 characters in Python code,
except if it would make the code less readable.
* `ruff` is used for formatting and linting Python code.
The following commands can be used to properly format the code and check
for linting errors with automatic fixing:
```bash
poetry run black --line-length 100 edvart/ tests/
poetry run ruff format .
poetry run ruff check . --fix
```
The following command can be used to check if the code is properly formatted:
The following command can be used to check if the code is properly
formatted and check for linting errors:
```bash
poetry run black --check --line-length 100 edvart/ tests/
````

* `isort` is used for sorting imports.
The following command can be used to properly sort imports:
```bash
poetry run isort --line-length 100 --profile black edvart/ tests/
```
The following command can be used to check if the imports are properly sorted:
```bash
poetry run isort --check --line-length 100 --profile black edvart/ tests/
```

* `pylint` is used to lint Python code. Tests are not required to be linted.
The following command can be used to lint the code:
```bash
poetry run pylint --rcfile=".pylintrc" edvart
poetry run ruff format --check .
poetry run ruff check .
```

All of the above code style requirements are enforced by the CI pipeline.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
<a href="https://pypi.org/project/edvart">
<img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/edvart.svg?label=PyPI%20downloads">
</a>
<a href="https://github.com/psf/black">
<img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg">
<a href="https://github.com/astral-sh/ruff">
<img alt="Ruff", src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json">
</a>
<a href="https://pycqa.github.io/isort/">
<img alt="Imports: isort" src="https://img.shields.io/badge/%20imports-isort-%231674b1?style=flat">
</a>

</p>

Edvart is an open-source Python library designed to simplify and streamline
Expand Down
80 changes: 42 additions & 38 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,27 @@
#
import os
import sys
import re
from datetime import datetime
from pathlib import Path

sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('../'))
import toml # make sure that toml is a developer dependency

# -- Project information -----------------------------------------------------
sys.path.insert(0, os.path.abspath("."))
sys.path.insert(0, os.path.abspath("../"))

project = 'edvart'
copyright = f'{datetime.now().year}, Datamole'
author = 'Hellen team'
# -- Project information -----------------------------------------------------

project = "edvart"
copyright = f"{datetime.now().year}, Datamole"
author = "Hellen team"

import toml # make sure that toml is a developer dependency

metadata = toml.load(Path(__file__).parent.parent / "pyproject.toml")["tool"]["poetry"]
version = release = metadata["version"]

version_long = version
# The short X.Y.Z version
version = '.'.join(version_long.split('.')[0:3])
version = ".".join(version_long.split(".")[0:3])
# The full version, including alpha/beta/rc tags
release = version_long

Expand All @@ -51,29 +50,29 @@
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.intersphinx',
'sphinx.ext.mathjax',
'sphinx.ext.viewcode',
'sphinx.ext.napoleon',
'sphinx_copybutton',
"sphinx.ext.autodoc",
"sphinx.ext.intersphinx",
"sphinx.ext.mathjax",
"sphinx.ext.viewcode",
"sphinx.ext.napoleon",
"sphinx_copybutton",
]

# napoleon_google_docstring = False
# napoleon_use_param = False
# napoleon_use_ivar = True

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
templates_path = ["_templates"]

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'
source_suffix = ".rst"

# The master toctree document.
master_doc = 'index'
master_doc = "index"

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand All @@ -85,20 +84,20 @@
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path .
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
pygments_style = "sphinx"


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
#html_theme = 'alabaster'
#html_theme = 'sphinx_rtd_theme'
html_theme = 'sphinx_rtd_theme'
# html_theme = 'alabaster'
# html_theme = 'sphinx_rtd_theme'
html_theme = "sphinx_rtd_theme"

# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
Expand All @@ -109,7 +108,7 @@
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
html_static_path = ["_static"]

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
Expand All @@ -125,7 +124,7 @@
# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'edvartdoc'
htmlhelp_basename = "edvartdoc"


# -- Options for LaTeX output ------------------------------------------------
Expand All @@ -134,15 +133,12 @@
# The paper size ('letterpaper' or 'a4paper').
#
# 'papersize': 'letterpaper',

# The font size ('10pt', '11pt' or '12pt').
#
# 'pointsize': '10pt',

# Additional stuff for the LaTeX preamble.
#
# 'preamble': '',

# Latex figure (float) alignment
#
# 'figure_align': 'htbp',
Expand All @@ -152,19 +148,21 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'edvart.tex', 'edvart Documentation',
'Your name (or your organization/company/team)', 'manual'),
(
master_doc,
"edvart.tex",
"edvart Documentation",
"Your name (or your organization/company/team)",
"manual",
),
]


# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'edvart', 'edvart Documentation',
[author], 1)
]
man_pages = [(master_doc, "edvart", "edvart Documentation", [author], 1)]


# -- Options for Texinfo output ----------------------------------------------
Expand All @@ -173,9 +171,15 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'edvart', 'edvart Documentation',
author, 'edvart', 'One line description of project.',
'Miscellaneous'),
(
master_doc,
"edvart",
"edvart Documentation",
author,
"edvart",
"One line description of project.",
"Miscellaneous",
),
]


Expand All @@ -184,4 +188,4 @@
# -- Options for intersphinx extension ---------------------------------------

# Example configuration for intersphinx: refer to the Python standard library.
intersphinx_mapping = {'https://docs.python.org/': None}
intersphinx_mapping = {"https://docs.python.org/": None}
2 changes: 1 addition & 1 deletion edvart/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pandas as pd

try:
import pyarrow # pylint: disable=unused-import
import pyarrow # noqa: F401
except ImportError:
PYARROW_PANDAS_BACKEND_AVAILABLE = False
else:
Expand Down
3 changes: 1 addition & 2 deletions edvart/report_sections/bivariate_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,8 +986,7 @@ def contingency_table(
n_digits_max = 1 + np.floor(np.log10(table.max().max()))
size_factor = (
# Constants chosen empirically to make the numbers fit in the cells
0.18
* max(4, n_digits_max)
0.18 * max(4, n_digits_max)
)
ax.figure.set_size_inches(size_factor * len(table.columns), size_factor * len(table))
# Set y axis
Expand Down
4 changes: 2 additions & 2 deletions edvart/report_sections/code_string_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def total_dedent(input_string: str) -> str:
input_string with no whitespace at the beginning of each line.
"""
input_string = input_string.strip()
lstripped_lines = [l.strip() for l in input_string.split("\n")]
lstripped_lines = [line.strip() for line in input_string.split("\n")]
return "\n".join(lstripped_lines)


Expand Down Expand Up @@ -53,7 +53,7 @@ def dedecorate(input_string: str) -> str:
input_string with beginning lines starting with '@' removed.
"""
lines = input_string.splitlines()
filtered_lines = dropwhile(lambda l: l.lstrip().startswith("@"), lines)
filtered_lines = dropwhile(lambda line_: line_.lstrip().startswith("@"), lines)

return "\n".join(filtered_lines)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def show_short_time_ft(
columns: Optional[List[str]] = None,
overlap: Optional[int] = None,
log: bool = True,
window: Union[str, Tuple, "array-like"] = "hamming",
window: Union[str, Tuple, np.typing.ArrayLike] = "hamming",
scaling: str = "spectrum",
figsize: Tuple[float, float] = (20, 7),
colormap: Any = "viridis",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import warnings
from functools import partial
from typing import Any, Callable, Dict, List, Optional
from typing import Any, Callable, Dict, List, Optional, Tuple

import nbformat.v4 as nbfv4
import pandas as pd
Expand Down Expand Up @@ -96,7 +96,7 @@ def show(self, df: pd.DataFrame) -> None:
show_stationarity_tests(df=df, columns=self.columns)


def default_stationarity_tests() -> Dict[pd.Series, Callable[[pd.Series], "test_result"]]:
def default_stationarity_tests() -> Dict[str, Callable[[pd.Series], Tuple]]:
"""Return a dictionary of stationarity test and functions.
Stationarity tests are:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ def __init__(
}
# Add FT and STFT only if required parameters specified
if sampling_rate is not None:
enum_to_implementation[
TimeseriesAnalysisSubsection.FourierTransform
] = FourierTransform(sampling_rate, verbosity_fourier_transform, columns)
enum_to_implementation[TimeseriesAnalysisSubsection.FourierTransform] = (
FourierTransform(sampling_rate, verbosity_fourier_transform, columns)
)
if stft_window_size is not None:
enum_to_implementation[TimeseriesAnalysisSubsection.ShortTimeFT] = ShortTimeFT(
sampling_rate, stft_window_size, verbosity_short_time_ft, columns
Expand Down
23 changes: 21 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,30 @@ sphinx = "~7.1"
sphinx-rtd-theme = "~1.3.0"
toml = "^0.10.0"
jupyter = "*"
black = "^22.3.0"
pylint = "~3.1"
sphinx-copybutton = "^0.5.2"
pytest-xdist = "^3.3.1"
ruff = "^0.3.0"

[build-system]
requires = ["poetry_core>=1.0.0"]
build-backend = "poetry.core.masonry.api"

[tool.ruff]
line-length = 100
target-version = "py310"
src = ["task"]

[tool.ruff.lint]
select = [
"E",
"F",
"N",
"W",
"I001",
]

[tool.ruff.lint.per-file-ignores]
"__init__.py" = ["E402", "F401"]

[tool.ruff.lint.isort]
known-first-party = ["edvart"]
4 changes: 2 additions & 2 deletions tests/test_multivariate_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import plotly.io as pio
import pytest

pio.renderers.default = "json"

from edvart import utils
from edvart.report_sections import multivariate_analysis
from edvart.report_sections.code_string_formatting import code_dedent, get_code
Expand All @@ -25,6 +23,8 @@
from .execution_utils import check_section_executes
from .pyarrow_utils import pyarrow_parameterize

pio.renderers.default = "json"


def get_test_df(pyarrow_dtypes: bool = False) -> pd.DataFrame:
test_df = pd.DataFrame(
Expand Down
Loading

0 comments on commit eb78c67

Please sign in to comment.