Skip to content

Commit

Permalink
Merge pull request #2630 from pybamm-team/flake8-to-ruff
Browse files Browse the repository at this point in the history
Flake8 to ruff
  • Loading branch information
valentinsulzer authored Jan 27, 2023
2 parents 932a133 + 3359e13 commit 29ff828
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 92 deletions.
2 changes: 1 addition & 1 deletion .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Please add a line in the relevant section of [CHANGELOG.md](https://github.com/p

# Key checklist:

- [ ] No style issues: `$ flake8`
- [ ] No style issues: `$ pre-commit run`
- [ ] All tests pass: `$ python run-tests.py --unit`
- [ ] The documentation builds: `$ cd docs` and then `$ make clean; make html`

Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test_on_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ jobs:

- name: Check style
run: |
python -m pip install "tox<4"
tox -e flake8
python -m pip install pre-commit
pre-commit run ruff
build:
needs: style
Expand Down
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@ dist/
coverage.xml
htmlcov/

# black setup file seems to make Travis CI fail
pyproject.toml

# virtual enviroment
env/
venv/
Expand Down
7 changes: 4 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ repos:
hooks:
- id: black

- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.0.236"
hooks:
- id: flake8
- id: ruff
args: [--ignore=E741, --exclude=__init__.py]
15 changes: 8 additions & 7 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,18 @@ Finally, if you really, really, _really_ love developing PyBaMM, have a look at

PyBaMM follows the [PEP8 recommendations](https://www.python.org/dev/peps/pep-0008/) for coding style. These are very common guidelines, and community tools have been developed to check how well projects implement them. We recommend using pre-commit hooks to check your code before committing it. See [installing and using pre-commit](https://github.com/pybamm-team/PyBaMM/blob/develop/CONTRIBUTING.md#installing-and-using-pre-commit) section for more details.

### Flake8
### Ruff

We use [flake8](http://flake8.pycqa.org/en/latest/) to check our PEP8 adherence. To try this on your system, navigate to the PyBaMM directory in a console and type
We use [ruff](https://github.com/charliermarsh/ruff) to check our PEP8 adherence. To try this on your system, navigate to the PyBaMM directory in a console and type

```bash
flake8
python -m pip install pre-commit
pre-commit run ruff
```

Flake8 is configured inside the file `tox.ini`, under the section `[flake8]`, allowing us to ignore some errors. If you think this should be added or removed, please submit an [issue](#issues)
ruff is configured inside the file `pre-commit-config.yaml`, allowing us to ignore some errors. If you think this should be added or removed, please submit an [issue](#issues)

When you commit your changes they will be checked against flake8 automatically (see [infrastructure](#infrastructure)).
When you commit your changes they will be checked against ruff automatically (see [infrastructure](#infrastructure)).

### Black

Expand All @@ -89,7 +90,7 @@ black {source_file_or_directory}

If you want to use black in your editor, you may need to change the max line length in your editor settings.

Even when code has been formatted by black, you should still make sure that it adheres to the PEP8 standard set by [Flake8](#flake8).
Even when code has been formatted by black, you should still make sure that it adheres to the PEP8 standard set by [ruff](#ruff).

### Naming

Expand All @@ -111,7 +112,7 @@ On the other hand... We _do_ want to compare several tools, to generate document
1. Core PyBaMM: A minimal set, including things like NumPy, SciPy, etc. All infrastructure should run against this set of dependencies, as well as any numerical methods we implement ourselves.
2. Extras: Other inference packages and their dependencies. Methods we don't want to implement ourselves, but do want to provide an interface to can have their dependencies added here.
3. Documentation generating code: Everything you need to generate and work on the docs.
4. Development code: Everything you need to do PyBaMM development (so all of the above packages, plus flake8 and other testing tools).
4. Development code: Everything you need to do PyBaMM development (so all of the above packages, plus ruff and other testing tools).

Only 'core pybamm' is installed by default. The others have to be specified explicitly when running the installation command.

Expand Down
1 change: 0 additions & 1 deletion pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
is_jax_compatible,
get_git_commit_info,
)
from .logger import logger, set_logging_level
from .logger import logger, set_logging_level, get_new_logger
from .settings import settings
from .citations import Citations, citations, print_citations
Expand Down
6 changes: 3 additions & 3 deletions pybamm/discretisations/discretisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,9 @@ def is_variable_independent(self, var, all_vars_in_eqns):
if not isinstance(var, pybamm.Variable):
return False

this_var_is_independent = not (var.name in all_vars_in_eqns)
not_in_y_slices = not (var in list(self.y_slices.keys()))
not_in_discretised = not (var in list(self._discretised_symbols.keys()))
this_var_is_independent = var.name not in all_vars_in_eqns
not_in_y_slices = var not in list(self.y_slices.keys())
not_in_discretised = var not in list(self._discretised_symbols.keys())
is_0D = len(var.domain) == 0
this_var_is_independent = (
this_var_is_independent and not_in_y_slices and not_in_discretised and is_0D
Expand Down
8 changes: 0 additions & 8 deletions pybamm/input/parameters/lead_acid/Sulzer2019.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,18 +311,10 @@ def get_parameter_values():
"Positive electrode electrons in reaction": 2.0,
"Positive electrode exchange-current density [A.m-2]"
"": lead_dioxide_exchange_current_density_Sulzer2019,
"Signed stoichiometry of cations (oxygen reaction)": 4.0,
"Signed stoichiometry of oxygen (oxygen reaction)": 1.0,
"Electrons in oxygen reaction": 4.0,
"Positive electrode oxygen exchange-current density [A.m-2]"
"": oxygen_exchange_current_density_Sulzer2019,
"Reference oxygen molecule concentration [mol.m-3]": 1000.0,
"Oxygen reference OCP vs SHE [V]": 1.229,
"Signed stoichiometry of cations (hydrogen reaction)": 2.0,
"Electrons in hydrogen reaction": 2.0,
"Positive electrode reference exchange-current density (hydrogen) [A.m-2]"
"": 0.0,
"Hydrogen reference OCP vs SHE [V]": 0.0,
"Positive electrode double-layer capacity [F.m-2]": 0.2,
"Positive electrode density [kg.m-3]": 9375.0,
"Positive electrode specific heat capacity [J.kg-1.K-1]": 256.0,
Expand Down
5 changes: 3 additions & 2 deletions pybamm/input/parameters/lithium_ion/Ai2020.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ def graphite_entropy_Enertech_Ai2020_function(sto, c_s_max):
References
----------
.. [1] Ai, W., Kraft, L., Sturm, J., Jossen, A., & Wu, B. (2020).
Electrochemical Thermal-Mechanical Modelling of Stress Inhomogeneity in Lithium-Ion Pouch Cells. # noqa
Journal of The Electrochemical Society, 167(1), 013512. DOI: 10.1149/2.0122001JES # noqa
Electrochemical Thermal-Mechanical Modelling of Stress Inhomogeneity in
Lithium-Ion Pouch Cells.
Journal of The Electrochemical Society, 167(1), 013512. DOI: 10.1149/2.0122001JES
Parameters
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ def graphite_entropy_Enertech_Ai2020_function(sto, c_s_max):
References
----------
.. [1] Ai, W., Kraft, L., Sturm, J., Jossen, A., & Wu, B. (2020).
Electrochemical Thermal-Mechanical Modelling of Stress Inhomogeneity in Lithium-Ion Pouch Cells. # noqa
Journal of The Electrochemical Society, 167(1), 013512. DOI: 10.1149/2.0122001JES # noqa
Electrochemical Thermal-Mechanical Modelling of Stress Inhomogeneity in
Lithium-Ion Pouch Cells.
Journal of The Electrochemical Society, 167(1), 013512. DOI: 10.1149/2.0122001JES
Parameters
----------
Expand Down
2 changes: 1 addition & 1 deletion pybamm/solvers/casadi_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ def create_integrator(self, model, inputs, t_eval=None, use_event_switch=False):
pybamm.logger.debug("Creating CasADi integrator")

# Use grid if t_eval is given
use_grid = not (t_eval is None)
use_grid = t_eval is not None
if use_grid is True:
t_eval_shifted = t_eval - t_eval[0]
t_eval_shifted_rounded = np.round(t_eval_shifted, decimals=12).tobytes()
Expand Down
2 changes: 1 addition & 1 deletion pybamm/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ def load_function(filename, funcname=None):
root_path = filename.replace(os.getcwd(), "")
# If the function is not in the current working directory and the path provided is
# absolute
elif os.path.isabs(filename) and not os.getcwd() in filename: # pragma: no cover
elif os.path.isabs(filename) and os.getcwd() not in filename: # pragma: no cover
# Change directory to import the function
dir_path = os.path.split(filename)[0]
os.chdir(dir_path)
Expand Down
58 changes: 0 additions & 58 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ envlist = {windows}-{tests,unit,dev},tests,unit,dev

[testenv]
skipsdist = true
skip_install = flake8: true
usedevelop = true
passenv = !windows-!mac: SUNDIALS_INST
whitelist_externals = !windows-!mac: sh
Expand All @@ -13,7 +12,6 @@ setenv =
deps =
dev-!windows-!mac: cmake
dev: black
dev: flake8
dev,doctests: sphinx>=1.5
dev,doctests: guzzle-sphinx-theme
!windows-!mac: scikits.odes
Expand All @@ -39,11 +37,6 @@ commands =
python {toxinidir}/scripts/install_KLU_Sundials.py
- git clone https://github.com/pybind/pybind11.git {toxinidir}/pybind11

[testenv:flake8]
skip_install = true
deps = flake8>=3
commands = python -m flake8

[testenv:coverage]
deps =
coverage
Expand All @@ -69,57 +62,6 @@ deps =
changedir = docs
commands = sphinx-autobuild --open-browser -qT . {envtmpdir}/html

[flake8]
max-line-length = 88
exclude=
.git,
problems,
__init__.py,
venv,
bin,
etc,
lib,
lib64,
share,
pyvenv.cfg,
third-party,
sundials-5.0.0,
KLU_module_deps,
pybind11,
install_KLU_Sundials,
ignore=
# False positive for white space before ':' on list slice
# black should format these correctly
E203,

# Block comment should start with '# '
# Not if it's a commented out line
E265,

# Ambiguous variable names
# It's absolutely fine to have i and I
E741,

# List comprehension redefines variable
# Re-using throw-away variables like `i`, `x`, etc. is a Good Idea
F812,

# Blank line at end of file
# This increases readability
W391,

# Line break before binary operator
# This is now actually advised in pep8
W503,

# Line break after binary operator
W504,

# Invalid escape sequence
# These happen all the time in latex parts of docstrings,
# e.g. \sigma
W605,

[coverage:run]
source = pybamm
# By default coverage data isn't collected in forked processes, see
Expand Down

0 comments on commit 29ff828

Please sign in to comment.