Skip to content

Commit

Permalink
chore: use ruff (#1474)
Browse files Browse the repository at this point in the history
* chore: use ruff

* add release note
  • Loading branch information
lkstrp authored Jan 3, 2025
1 parent 6a974d0 commit 151dc73
Show file tree
Hide file tree
Showing 114 changed files with 459 additions and 518 deletions.
61 changes: 13 additions & 48 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,19 @@ repos:
rev: v5.0.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
- id: fix-encoding-pragma
- id: mixed-line-ending
- id: trailing-whitespace
- id: check-added-large-files
args: ["--maxkb=2000"]

# Sort package imports alphabetically
- repo: https://github.com/PyCQA/isort
rev: 5.13.2
# Run ruff to lint and format
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.8.1
hooks:
- id: isort
args: ["--profile", "black", "--filter-files"]

# Convert relative imports to absolute imports
- repo: https://github.com/MarcoGorelli/absolufy-imports
rev: v0.3.1
hooks:
- id: absolufy-imports
# Run the linter.
- id: ruff
args: [--fix]
# Run the formatter.
- id: ruff-format

# Find common spelling mistakes in comments and docstrings
- repo: https://github.com/codespell-project/codespell
Expand All @@ -37,37 +31,15 @@ repos:
types_or: [python, rst, markdown]
files: ^(scripts|doc)/

# Make docstrings PEP 257 compliant
# Broken for pre-commit<=4.0.0
# See https://github.com/PyCQA/docformatter/issues/293
# - repo: https://github.com/PyCQA/docformatter
# rev: v1.7.5
# hooks:
# - id: docformatter
# args: ["--in-place", "--make-summary-multi-line", "--pre-summary-newline"]

- repo: https://github.com/keewis/blackdoc
rev: v0.3.9
hooks:
- id: blackdoc

# Formatting with "black" coding style
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.10.0
hooks:
# Format Python files
- id: black
# Format Jupyter Python notebooks
- id: black-jupyter

# Remove output from Jupyter notebooks
# Remove output from Jupyter notebooks
- repo: https://github.com/aflc/pre-commit-jupyter
rev: v1.2.1
hooks:
- id: jupyter-notebook-cleanup
args: ["--remove-kernel-metadata"]
args: ['--remove-kernel-metadata']
exclude: examples/solve-on-remote.ipynb

# Do YAML formatting (before the linter checks it for misses)
# YAML formatting
- repo: https://github.com/macisamuele/language-formatters-pre-commit-hooks
rev: v2.14.0
hooks:
Expand All @@ -81,13 +53,6 @@ repos:
hooks:
- id: snakefmt

# For cleaning jupyter notebooks
- repo: https://github.com/aflc/pre-commit-jupyter
rev: v1.2.1
hooks:
- id: jupyter-notebook-cleanup
exclude: examples/solve-on-remote.ipynb

# Check for FSFE REUSE compliance (licensing)
- repo: https://github.com/fsfe/reuse-tool
rev: v5.0.2
Expand Down
3 changes: 1 addition & 2 deletions config/create_scenarios.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2023-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT
Expand All @@ -7,7 +6,7 @@
# You can modify the template to your needs and define all possible combinations of config values that should be considered.

if "snakemake" in globals():
filename = snakemake.output[0]
filename = snakemake.output[0] # noqa: F821
else:
filename = "../config/scenarios.yaml"

Expand Down
1 change: 0 additions & 1 deletion data/custom_extra_functionality.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: : 2023- The PyPSA-Eur Authors
#
# SPDX-License-Identifier: MIT
Expand Down
1 change: 0 additions & 1 deletion doc/conf.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
Expand Down
4 changes: 3 additions & 1 deletion doc/release_notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ Upcoming Release

* Bugfix: Align the naming convention for the CO2 network configuration (from `co2network` to `co2_network`). This may be a small breaking change.

* Feature: The installation via `make install` now prioritizes mamba over conda for faster installation. Conda is still used as a fallback. The command `make install` now also supports passing the name of the environment, e.g. `make install name=my-project`.
* Development: The installation via `make install` now prioritizes mamba over conda for faster installation. Conda is still used as a fallback. The command `make install` now also supports passing the name of the environment, e.g. `make install name=my-project`.

* Development: Ruff is now used for linting and formatting. It is used in the pre-commit, so no changes are needed. But you might wanna set it up in your IDE.

* Update locations and capacities of ammonia plants.

Expand Down
5 changes: 4 additions & 1 deletion envs/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ dependencies:
- jpype1
- pyxlsb
- graphviz
- pre-commit
- geojson

# Keep in conda environment when calling ipython
Expand All @@ -60,6 +59,10 @@ dependencies:
- rasterio==1.4.1
- libgdal-core<3.10.0 # rasterio>=1.4.2 needed for GDAL 3.10

# Development dependencies
- pre-commit
- ruff

- pip:
- gurobipy
- highspy<1.8
Expand Down
53 changes: 53 additions & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# SPDX-FileCopyrightText: : 2021-2024 The PyPSA-Eur Authors
#
# SPDX-License-Identifier: CC0-1.0

extend-include = ['*.ipynb']

[lint]
select = [
'F', # pyflakes
'E', # pycodestyle: Error
'W', # pycodestyle: Warning
'I', # isort
'D', # pydocstyle
'UP', # pyupgrade
# 'ANN', # flake-8 annotations
'TID', # flake8-tidy-imports
# 'NPY', # numpy
# 'RUF', # ruff
]

ignore = [
'ANN401', # Dynamically typed expressions are forbidden
'E712', # comparison to False should be 'if cond is False:' or 'if not cond:'
'E741', # ambiguous variable names
'D203', # 1 blank line required before class docstring
'D212', # Multi-line docstring summary should start at the second line
'D401', # First line should be in imperative mood
]


[lint.per-file-ignores]
# pydocstyle ignores, which could be enabled in future when existing
# issues are fixed
"!**/{xxx.py}" = [
'E501', # line too long
'D100', # Missing docstring in public module
'D101', # Missing docstring in public class
'D102', # Missing docstring in public method
'D103', # Missing docstring in public function
'D104', # Missing docstring in public package
'D105', # Missing docstring in magic method
'D107', # Missing docstring in __init__
'D200', # One-line docstring should fit on one line with quotes
'D202', # No blank lines allowed after function docstring
'D205', # 1 blank line required between summary line and description
'D400', # First line should end with a period
'D404', # First word of the docstring should not be "This
'D413', # Missing blank line after last section
'D415', # First line should end with a period, question mark, or exclamation point
'D417', # Missing argument descriptions in the docstring
# Include once available
# https://github.com/astral-sh/ruff/issues/2310
]
1 change: 0 additions & 1 deletion scripts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
Expand Down
21 changes: 9 additions & 12 deletions scripts/_benchmark.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
""" """

from __future__ import absolute_import, print_function

import logging
import os
Expand Down Expand Up @@ -42,7 +39,7 @@ def __init__(
self.timestamps = kw.pop("timestamps", True)
self.include_children = kw.pop("include_children", True)

super(MemTimer, self).__init__(*args, **kw)
super().__init__(*args, **kw)

def run(self):
# get baseline memory usage
Expand All @@ -58,7 +55,7 @@ def run(self):

if self.filename is not None:
stream = open(self.filename, "w")
stream.write("MEM {0:.6f} {1:.4f}\n".format(*cur_mem))
stream.write("MEM {:.6f} {:.4f}\n".format(*cur_mem))
stream.flush()
else:
stream = None
Expand All @@ -74,7 +71,7 @@ def run(self):
)

if stream is not None:
stream.write("MEM {0:.6f} {1:.4f}\n".format(*cur_mem))
stream.write("MEM {:.6f} {:.4f}\n".format(*cur_mem))
stream.flush()

n_measurements += 1
Expand All @@ -95,7 +92,7 @@ def run(self):
self.pipe.send(n_measurements)


class memory_logger(object):
class memory_logger:
"""
Context manager for taking and reporting memory measurements at fixed
intervals from a separate process, for the duration of a context.
Expand Down Expand Up @@ -185,7 +182,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
return False


class timer(object):
class timer:
level = 0
opened = False

Expand All @@ -211,14 +208,14 @@ def __enter__(self):

def print_usec(self, usec):
if usec < 1000:
print("%.1f usec" % usec)
print(f"{usec:.1f} usec")
else:
msec = usec / 1000
if msec < 1000:
print("%.1f msec" % msec)
print(f"{msec:.1f} msec")
else:
sec = msec / 1000
print("%.1f sec" % sec)
print(f"{sec:.1f} sec")

def __exit__(self, exc_type, exc_val, exc_tb):
if not self.opened and self.verbose:
Expand All @@ -239,7 +236,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
return False


class optional(object):
class optional:
def __init__(self, variable, contextman):
self.variable = variable
self.contextman = contextman
Expand Down
16 changes: 7 additions & 9 deletions scripts/_helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
Expand All @@ -10,7 +9,6 @@
import os
import re
import time
import urllib
from functools import partial, wraps
from os.path import exists
from pathlib import Path
Expand Down Expand Up @@ -189,13 +187,13 @@ def set_scenario_config(snakemake):
scenario = snakemake.config["run"].get("scenarios", {})
if scenario.get("enable") and "run" in snakemake.wildcards.keys():
try:
with open(scenario["file"], "r") as f:
with open(scenario["file"]) as f:
scenario_config = yaml.safe_load(f)
except FileNotFoundError:
# fallback for mock_snakemake
script_dir = Path(__file__).parent.resolve()
root_dir = script_dir.parent
with open(root_dir / scenario["file"], "r") as f:
with open(root_dir / scenario["file"]) as f:
scenario_config = yaml.safe_load(f)
update_config(snakemake.config, scenario_config[snakemake.wildcards.run])

Expand Down Expand Up @@ -748,9 +746,9 @@ def update_config_from_wildcards(config, w, inplace=True):
if dg_enable:
config["sector"]["electricity_distribution_grid"] = True
if dg_factor is not None:
config["sector"][
"electricity_distribution_grid_cost_factor"
] = dg_factor
config["sector"]["electricity_distribution_grid_cost_factor"] = (
dg_factor
)

if "biomasstransport" in opts:
config["sector"]["biomass_transport"] = True
Expand Down Expand Up @@ -883,12 +881,12 @@ def rename_techs(label: str) -> str:
Removes some prefixes and renames if certain conditions defined in function body are met.
Parameters:
Parameters
----------
label: str
Technology label to be renamed
Returns:
Returns
-------
str
Renamed label
Expand Down
2 changes: 0 additions & 2 deletions scripts/add_brownfield.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT
Expand All @@ -19,7 +18,6 @@
update_config_from_wildcards,
)
from add_existing_baseyear import add_build_year_to_new_assets
from pypsa.clustering.spatial import normed_or_uniform

logger = logging.getLogger(__name__)
idx = pd.IndexSlice
Expand Down
10 changes: 3 additions & 7 deletions scripts/add_electricity.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
# SPDX-FileCopyrightText: Contributors to PyPSA-Eur <https://github.com/pypsa/pypsa-eur>
#
# SPDX-License-Identifier: MIT

"""
Adds existing electrical generators, hydro-electric plants as well as
greenfield and battery and hydrogen storage to the clustered network.
Expand Down Expand Up @@ -111,8 +111,6 @@
"""

import logging
from pathlib import Path
from typing import Dict, List

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -502,9 +500,7 @@ def attach_wind_and_solar(
+ connection_cost
)
logger.info(
"Added connection cost of {:0.0f}-{:0.0f} Eur/MW/a to {}".format(
connection_cost.min(), connection_cost.max(), car
)
f"Added connection cost of {connection_cost.min():0.0f}-{connection_cost.max():0.0f} Eur/MW/a to {car}"
)
else:
capital_cost = costs.at[car, "capital_cost"]
Expand Down Expand Up @@ -746,7 +742,7 @@ def attach_hydro(n, costs, ppl, profile_hydro, hydro_capacities, carriers, **par
)


def attach_OPSD_renewables(n: pypsa.Network, tech_map: Dict[str, List[str]]) -> None:
def attach_OPSD_renewables(n: pypsa.Network, tech_map: dict[str, list[str]]) -> None:
"""
Attach renewable capacities from the OPSD dataset to the network.
Expand Down
Loading

0 comments on commit 151dc73

Please sign in to comment.