Skip to content

Commit

Permalink
Updated linting to ruff (fitbenchmarking#1320)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Lister <[email protected]>
  • Loading branch information
RabiyaF and AndrewLister-STFC authored Nov 5, 2024
1 parent 2527cad commit aa15cf7
Show file tree
Hide file tree
Showing 142 changed files with 8,811 additions and 6,729 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Linting

on:
push:
pull_request:
release:
types: [published]

jobs:
ruff_linux:
name: Ruff (linux)
if: github.event.repository.fork == false
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Run check
uses: chartboost/ruff-action@v1
- name: Run format
uses: chartboost/ruff-action@v1
with:
args: 'format --check'
19 changes: 0 additions & 19 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,6 @@ jobs:
name: default_unit_tests
path: test-results/default_unit_pytest.xml

linting_test:
name: Linting Tests (Linux)
if: github.event.repository.fork == false
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Install python
uses: actions/setup-python@v5
with:
python-version: '3.12'
check-latest: true
- name: Install default dependencies for fitbenchmarking
run: |
pip install .[bumps,DFO,gradient-free,minuit,SAS,numdifftools,lmfit,nlopt]
pip install --upgrade .[dev]
- name: Run linting
run: ci/linting_tests.sh

publish-test-results:
name: Publish Test Results
if: github.event.repository.fork == false && always()
Expand Down
35 changes: 7 additions & 28 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,15 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v5.0.0
hooks:
- id: check-added-large-files
- id: check-merge-conflict

- repo: https://github.com/PyCQA/isort
rev: 5.13.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
hooks:
- id: isort

- repo: https://github.com/hhatto/autopep8
rev: v2.0.4
hooks:
- id: autopep8

- repo: local
hooks:
- id: pylint
name: pylint
entry: pylint
language: system
types: [python]
args:
[
"-rn", # Only display messages
"-sn", # Don't display the score
]

- repo: https://github.com/PyCQA/flake8
rev: 7.0.0
hooks:
- id: flake8

# Run the linter.
- id: ruff
# Run the formatter.
- id: ruff-format
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Documentation Status](https://img.shields.io/readthedocs/fitbenchmarking?style=flat-square)](https://fitbenchmarking.readthedocs.io/en/latest)
[![Coverage Status](https://img.shields.io/coveralls/github/fitbenchmarking/fitbenchmarking.svg?style=flat-square)](https://coveralls.io/github/fitbenchmarking/fitbenchmarking)
[![Chat](https://img.shields.io/badge/chat-CompareFitMinimizers-lightgrey.svg?style=flat-square&logo=slack)](https://slack.com/)
[![Zenodo](https://img.shields.io/badge/Zenodo-10.5281/zenodo.6597790-blue.svg?style=flat-square)](https://doi.org/10.5281/zenodo.6597790)
[![Zenodo](https://img.shields.io/badge/Zenodo-10.5281/zenodo.11198140-blue.svg?style=flat-square)](https://doi.org/10.5281/zenodo.11198140)
[![JOSS](https://img.shields.io/badge/JOSS-10.21105/joss.03127-brightgreen.svg?style=flat-square)](https://doi.org/10.21105/joss.03127)
# FitBenchmarking

Expand Down
14 changes: 0 additions & 14 deletions ci/linting_tests.sh

This file was deleted.

21 changes: 13 additions & 8 deletions conftest.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import pytest

# set choices for --test-type
test_choices = ['all', 'default', 'matlab']
test_choices = ["all", "default", "matlab"]


def pytest_addoption(parser):
"""
Command line input function which requires 'all', 'default' or 'matlab'
as an input
"""
parser.addoption(
"--test-type", action="store", default="all", type=str,
choices=test_choices
"--test-type",
action="store",
default="all",
type=str,
choices=test_choices,
)


def pytest_configure(config):
"""
Sets variable `pytest.test_type` to be used within testing
"""
pytest.test_type = config.getoption('--test-type')
pytest.test_type = config.getoption("--test-type")


def run_for_test_types(TEST_TYPE, *test_types):
Expand All @@ -36,7 +40,8 @@ def run_for_test_types(TEST_TYPE, *test_types):
skipped based on chosen TEST_TYPE
:rtype: callable
"""
to_skip = [type for type in test_choices
if type not in test_types]
return pytest.mark.skipif(TEST_TYPE in to_skip,
reason="Tests can't be run with selected test type")
to_skip = [type for type in test_choices if type not in test_types]
return pytest.mark.skipif(
TEST_TYPE in to_skip,
reason="Tests can't be run with selected test type",
)
43 changes: 24 additions & 19 deletions docs/source/_ext/algorithmcheckdocs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
controller that correspond to a given key from the dictionary
"""

from pprint import pformat
import os
from importlib import import_module
from pprint import pformat

from docutils import nodes
from docutils.parsers.rst import Directive, directives
import os


class AlgorithmCheckDocs(Directive):
Expand All @@ -23,39 +24,43 @@ class AlgorithmCheckDocs(Directive):
# .. algorithmcheckdocs::
# :key: general
option_spec = {
'key': directives.unchanged_required,
"key": directives.unchanged_required,
}

def run(self):
"""
Accesses variable
"""
inp_key = self.options['key']
inp_key = self.options["key"]

# import controller factory
factory = import_module(
'fitbenchmarking.controllers.controller_factory')
"fitbenchmarking.controllers.controller_factory"
)

# set relative path to controllers directory
cwd = os.path.dirname(__file__)
dirname = os.path.join(cwd, '../../../fitbenchmarking/controllers')
dirname = os.path.join(cwd, "../../../fitbenchmarking/controllers")

# loop over all fitbenchmarking controllers, and extract the minimizers
# in algorithm_check that correspond to the given dict key
value = {}
for filename in os.listdir(dirname):
if not filename.endswith('.py')\
or filename in ['__init__.py',
'base_controller.py',
'controller_factory.py',
'matlab_mixin.py']:
if not filename.endswith(".py") or filename in [
"__init__.py",
"base_controller.py",
"controller_factory.py",
"matlab_mixin.py",
]:
continue
software_name =\
os.path.splitext(filename)[0].replace('_controller', '')
software_name = os.path.splitext(filename)[0].replace(
"_controller", ""
)

controller_class = \
factory.ControllerFactory.create_controller(software_name)
alg_dict = getattr(controller_class, 'algorithm_check')
controller_class = factory.ControllerFactory.create_controller(
software_name
)
alg_dict = getattr(controller_class, "algorithm_check")

value[software_name] = alg_dict[inp_key]

Expand All @@ -71,7 +76,7 @@ def setup(app):
app.add_directive("algorithmcheckdocs", AlgorithmCheckDocs)

return {
'version': '0.1',
'parallel_read_safe': True,
'parallel_write_safe': True,
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
17 changes: 9 additions & 8 deletions docs/source/_ext/prettyprintvalue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Module which allows access to variables in the code
"""

from pprint import pformat
from importlib import import_module
from pprint import pformat

from docutils import nodes
from docutils.parsers.rst import Directive, directives

Expand All @@ -16,16 +17,16 @@ class PrettyPrintModuleValue(Directive):
has_content = False
optional_arguments = 2
option_spec = {
'module': directives.unchanged_required,
'var': directives.unchanged_required,
"module": directives.unchanged_required,
"var": directives.unchanged_required,
}

def run(self):
"""
Accesses variable
"""
inp_module = self.options['module']
inp_var = self.options['var']
inp_module = self.options["module"]
inp_var = self.options["var"]

# Get variable value
module = import_module(inp_module)
Expand All @@ -43,7 +44,7 @@ def setup(app):
app.add_directive("prettyprintmodulevalue", PrettyPrintModuleValue)

return {
'version': '0.1',
'parallel_read_safe': True,
'parallel_write_safe': True,
"version": "0.1",
"parallel_read_safe": True,
"parallel_write_safe": True,
}
Loading

0 comments on commit aa15cf7

Please sign in to comment.