From 88953b8a28abf6610bb3959d901d1cf83b8d5d2a Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 12 Oct 2023 16:44:01 +0200 Subject: [PATCH 1/5] MNT update linters and use ruff --- .github/workflows/build-test.yml | 4 ++-- .pre-commit-config.yaml | 15 ++++++--------- pyproject.toml | 5 +++-- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 06f6f17c..8e3c6e03 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -55,7 +55,7 @@ jobs: - name: Install dependencies run: | pip install .[docs,tests] - pip install black=="22.6.0" isort=="5.10.1" mypy=="1.0.0" + pip install black=="23.9.1" ruff=="0.0.292" mypy=="1.0.0" pip uninstall --yes scikit-learn if [ ${{ matrix.sklearn_version }} == "nightly" ]; then pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple scikit-learn; @@ -73,7 +73,7 @@ jobs: run: black --check --diff . - name: Check isort - run: isort --check --diff . + run: ruff --check --diff . - name: Tests env: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4026f7dc..395229fe 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,18 +10,15 @@ repos: - id: check-case-conflict - id: check-merge-conflict - repo: https://github.com/psf/black - rev: 23.1.0 + rev: 23.9.1 hooks: - id: black -- repo: https://github.com/pycqa/flake8 - rev: 6.0.0 +- repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.0.292 hooks: - - id: flake8 - types: [file, python] -- repo: https://github.com/PyCQA/isort - rev: 5.12.0 - hooks: - - id: isort + - id: ruff + args: ["--fix", "--show-source"] - repo: https://github.com/pre-commit/mirrors-mypy rev: v1.0.1 hooks: diff --git a/pyproject.toml b/pyproject.toml index 0bf3bda4..f524292f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,8 +3,9 @@ line-length = 88 target_version = ['py38', 'py39', 'py310', 'py311'] preview = true -[tool.isort] -profile = "black" +[tool.ruff] +# all rules can be found here: https://beta.ruff.rs/docs/rules/ +select = ["E", "F", "W", "I"] [tool.pytest.ini_options] filterwarnings = [ From e956fbd90189eca03b9f041db1a5a453adca729a Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 12 Oct 2023 16:45:56 +0200 Subject: [PATCH 2/5] update mypy version --- .github/workflows/build-test.yml | 2 +- .pre-commit-config.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 8e3c6e03..1fa1fdd8 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -55,7 +55,7 @@ jobs: - name: Install dependencies run: | pip install .[docs,tests] - pip install black=="23.9.1" ruff=="0.0.292" mypy=="1.0.0" + pip install black=="23.9.1" ruff=="0.0.292" mypy=="1.6.0" pip uninstall --yes scikit-learn if [ ${{ matrix.sklearn_version }} == "nightly" ]; then pip install --pre --extra-index https://pypi.anaconda.org/scientific-python-nightly-wheels/simple scikit-learn; diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 395229fe..6a4d8603 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,7 +20,7 @@ repos: - id: ruff args: ["--fix", "--show-source"] - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.0.1 + rev: v1.6.0 hooks: - id: mypy args: [--config-file=pyproject.toml] From 00c4521757756a07b4fb53ecfdd8bb57d95803d7 Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 12 Oct 2023 16:47:18 +0200 Subject: [PATCH 3/5] apply new linters --- examples/plot_custom_model_card.py | 4 +++- examples/plot_tabular_regression.py | 7 ++++--- skops/card/_model_card.py | 15 +++++++++------ skops/card/_templates.py | 27 ++++++++++++++++++--------- skops/card/tests/test_card.py | 12 ++++-------- skops/io/_general.py | 2 +- 6 files changed, 39 insertions(+), 28 deletions(-) diff --git a/examples/plot_custom_model_card.py b/examples/plot_custom_model_card.py index 0033950b..1385836f 100644 --- a/examples/plot_custom_model_card.py +++ b/examples/plot_custom_model_card.py @@ -163,7 +163,9 @@ display.figure_.savefig(plot_file_name) model_card.add_plot( **{ - "Regression on California Housing dataset/Results/Partial Dependence Plots": plot_file_name + "Regression on California Housing dataset/Results/Partial Dependence Plots": ( + plot_file_name + ) }, ) diff --git a/examples/plot_tabular_regression.py b/examples/plot_tabular_regression.py index 5e9d8fb0..762939b3 100644 --- a/examples/plot_tabular_regression.py +++ b/examples/plot_tabular_regression.py @@ -16,7 +16,6 @@ from tempfile import mkdtemp, mkstemp import matplotlib.pyplot as plt -import pandas as pd import sklearn from sklearn.datasets import load_diabetes from sklearn.linear_model import LinearRegression @@ -42,7 +41,8 @@ # Train a Model # ============= # To train a model, we need to convert our data first to vectors. We will use -# StandardScalar in our pipeline. We will fit a Linear Regression model with the outputs of the scalar. +# StandardScalar in our pipeline. We will fit a Linear Regression model with +# the outputs of the scalar. model = Pipeline( [ ("scaler", StandardScaler()), @@ -112,13 +112,14 @@ model_card_authors = "skops_user, lazarust" citation_bibtex = "bibtex\n@inproceedings{...,year={2022}}" model_card.add( + folded=False, **{ "Model Card Authors": model_card_authors, "Intended uses & limitations": limitations, "Citation": citation_bibtex, "Model description": model_description, "Model description/Intended uses & limitations": limitations, - } + }, ) # %% diff --git a/skops/card/_model_card.py b/skops/card/_model_card.py index f354415c..b7f20f38 100644 --- a/skops/card/_model_card.py +++ b/skops/card/_model_card.py @@ -1254,8 +1254,9 @@ def add_fairlearn_metric_frame( description: str | None = None, ) -> Self: """ - Add a :class:`fairlearn.metrics.MetricFrame` table to the model card. The table contains - the difference, group_ma, group_min, and ratio for each metric. + Add a :class:`fairlearn.metrics.MetricFrame` table to the model card. + The table contains the difference, group_ma, group_min, and ratio for + each metric. Parameters ---------- @@ -1451,9 +1452,10 @@ def save(self, path: str | Path, copy_files: bool = False) -> None: Filepath to save your card. plot_path: str - Filepath to save the plots. Use this when saving the model card before creating the - repository. Without this path the README will have an absolute path to the plot that - won't exist in the repository. + Filepath to save the plots. Use this when saving the model card + before creating the repository. Without this path the README will + have an absolute path to the plot that won't exist in the + repository. Notes ----- @@ -1487,7 +1489,8 @@ def _iterate_key_section_content( Parameters ---------- data : dict[str, Section] - The card data to iterate through. This is usually the sections and subsections. + The card data to iterate through. This is usually the sections and + subsections. level : int, optional The level of the section, by default 0. This keeps track of subsections. diff --git a/skops/card/_templates.py b/skops/card/_templates.py index e343c427..e79357ba 100644 --- a/skops/card/_templates.py +++ b/skops/card/_templates.py @@ -67,8 +67,10 @@ class Templates(Enum): "Model Details/Model Description/Model type": CONTENT_PLACEHOLDER, "Model Details/Model Description/Language(s) (NLP)": CONTENT_PLACEHOLDER, "Model Details/Model Description/License": CONTENT_PLACEHOLDER, - "Model Details/Model Description/Finetuned from model [optional]": CONTENT_PLACEHOLDER, - "Model Details/Model Description/Resources for more information": CONTENT_PLACEHOLDER, + "Model Details/Model Description/Finetuned from model [optional]": + CONTENT_PLACEHOLDER, + "Model Details/Model Description/Resources for more information": + CONTENT_PLACEHOLDER, "Uses": "", # Address questions around how the model is intended to be used, including @@ -102,8 +104,10 @@ class Templates(Enum): "Training Details/Training Procedure [optional]": "", # This relates heavily to the Technical Specifications. Content here should # link to that section when it is relevant to the training procedure. - "Training Details/Training Procedure [optional]/Preprocessing": CONTENT_PLACEHOLDER, - "Training Details/Training Procedure [optional]/Speeds, Sizes, Times": CONTENT_PLACEHOLDER, + "Training Details/Training Procedure [optional]/Preprocessing": + CONTENT_PLACEHOLDER, + "Training Details/Training Procedure [optional]/Speeds, Sizes, Times": + CONTENT_PLACEHOLDER, # This section provides information about throughput, start/end time, # checkpoint size if relevant, etc. @@ -137,10 +141,14 @@ class Templates(Enum): "Environmental Impact/Carbon Emitted": CONTENT_PLACEHOLDER, "Technical Specifications [optional]": "", - "Technical Specifications [optional]/Model Architecture and Objective": CONTENT_PLACEHOLDER, - "Technical Specifications [optional]/Compute Infrastructure": CONTENT_PLACEHOLDER, - "Technical Specifications [optional]/Compute Infrastructure/Hardware": CONTENT_PLACEHOLDER, - "Technical Specifications [optional]/Compute Infrastructure/Software": CONTENT_PLACEHOLDER, + "Technical Specifications [optional]/Model Architecture and Objective": + CONTENT_PLACEHOLDER, + "Technical Specifications [optional]/Compute Infrastructure": + CONTENT_PLACEHOLDER, + "Technical Specifications [optional]/Compute Infrastructure/Hardware": + CONTENT_PLACEHOLDER, + "Technical Specifications [optional]/Compute Infrastructure/Software": + CONTENT_PLACEHOLDER, "Citation [optional]": "", # If there is a paper or blog post introducing the model, the APA and Bibtex @@ -155,7 +163,8 @@ class Templates(Enum): "More Information [optional]": CONTENT_PLACEHOLDER, "Model Card Authors [optional]": CONTENT_PLACEHOLDER, "Model Card Contact": CONTENT_PLACEHOLDER, - "How to Get Started with the Model": f"""Use the code below to get started with the model. + "How to Get Started with the Model": + f"""Use the code below to get started with the model.
Click to expand diff --git a/skops/card/tests/test_card.py b/skops/card/tests/test_card.py index c78ce112..6aba98b3 100644 --- a/skops/card/tests/test_card.py +++ b/skops/card/tests/test_card.py @@ -807,14 +807,12 @@ def test_render_with_metadata(self, model_card): model_card.metadata.foo = "something" model_card.metadata.bar = "something else" rendered = model_card.render() - expected = textwrap.dedent( - """ + expected = textwrap.dedent(""" --- foo: something bar: something else --- - """ - ).strip() + """).strip() assert rendered.startswith(expected) @@ -1362,13 +1360,11 @@ def test_card_repr_empty_card(self, meth): model = fit_model() card = Card(model, model_diagram=False, template=None) result = meth(card) - expected = textwrap.dedent( - """ + expected = textwrap.dedent(""" Card( model=LinearRegression(), ) - """ - ).strip() + """).strip() assert result == expected @pytest.mark.parametrize("meth", [repr, str]) diff --git a/skops/io/_general.py b/skops/io/_general.py index 6d70b7ac..662252bc 100644 --- a/skops/io/_general.py +++ b/skops/io/_general.py @@ -191,7 +191,7 @@ def isnamedtuple(self, t) -> bool: f = getattr(t, "_fields", None) if not isinstance(f, tuple): return False - return all(type(n) == str for n in f) + return all(isinstance(n, str) for n in f) def function_get_state(obj: Any, save_context: SaveContext) -> dict[str, Any]: From 2203cceae470f8010c9a75efde6deff9f5c54a51 Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 12 Oct 2023 16:57:46 +0200 Subject: [PATCH 4/5] fix command --- .github/workflows/build-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index 1fa1fdd8..ae6add9b 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -73,7 +73,7 @@ jobs: run: black --check --diff . - name: Check isort - run: ruff --check --diff . + run: ruff check --diff . - name: Tests env: From dde6c105858fe12127bbf45a088413cc51429d14 Mon Sep 17 00:00:00 2001 From: adrinjalali Date: Thu, 12 Oct 2023 17:02:09 +0200 Subject: [PATCH 5/5] fix CI step name --- .github/workflows/build-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index ae6add9b..38dd1f34 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -72,7 +72,7 @@ jobs: - name: Check black run: black --check --diff . - - name: Check isort + - name: Check ruff run: ruff check --diff . - name: Tests