Skip to content

Commit

Permalink
pyupgrade, pre-commit autoupdate (#2411)
Browse files Browse the repository at this point in the history
* Run pyupgrade for python>=3.10
* Update configuration for recent ruff 


manual changes:
* .pre-commit-config.yam
* pyproject.toml
  • Loading branch information
dweindl authored Apr 23, 2024
1 parent 1f18ed8 commit f59fed7
Show file tree
Hide file tree
Showing 56 changed files with 268 additions and 243 deletions.
9 changes: 4 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.6.0
hooks:
- id: check-added-large-files
- id: check-merge-conflict
Expand All @@ -12,7 +12,7 @@ repos:
- id: trailing-whitespace
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.11
rev: v0.4.1
hooks:
# Run the linter.
- id: ruff
Expand All @@ -28,10 +28,9 @@ repos:
- python/sdist/pyproject.toml

- repo: https://github.com/asottile/pyupgrade
rev: v3.15.0
rev: v3.15.2
hooks:
- id: pyupgrade
args: ["--py39-plus"]
additional_dependencies: [pyupgrade==3.15.0]
args: ["--py310-plus"]

exclude: '^(ThirdParty|models)/'
7 changes: 4 additions & 3 deletions python/sdist/amici/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
import sys
from pathlib import Path
from types import ModuleType as ModelModule
from typing import Any, Callable, Union
from typing import Any
from collections.abc import Callable


def _get_amici_path():
Expand Down Expand Up @@ -138,7 +139,7 @@ def get_model(self) -> amici.Model:
class add_path:
"""Context manager for temporarily changing PYTHONPATH"""

def __init__(self, path: Union[str, Path]):
def __init__(self, path: str | Path):
self.path: str = str(path)

def __enter__(self):
Expand All @@ -151,7 +152,7 @@ def __exit__(self, exc_type, exc_value, traceback):


def import_model_module(
module_name: str, module_path: Union[Path, str]
module_name: str, module_path: Path | str
) -> ModelModule:
"""
Import Python module of an AMICI model
Expand Down
1 change: 1 addition & 0 deletions python/sdist/amici/_codegen/cxx_functions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Info about C++ functions in the generated model code."""

from __future__ import annotations

from dataclasses import dataclass
Expand Down
1 change: 1 addition & 0 deletions python/sdist/amici/_codegen/model_class.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Function for generating the ``amici::Model`` subclass for an amici model."""

from __future__ import annotations

from .cxx_functions import functions, multiobs_functions
Expand Down
6 changes: 3 additions & 3 deletions python/sdist/amici/_codegen/template.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Functions to apply template substitution to files."""

from pathlib import Path
from string import Template
from typing import Union


class TemplateAmici(Template):
Expand All @@ -17,8 +17,8 @@ class TemplateAmici(Template):


def apply_template(
source_file: Union[str, Path],
target_file: Union[str, Path],
source_file: str | Path,
target_file: str | Path,
template_data: dict[str, str],
) -> None:
"""
Expand Down
6 changes: 3 additions & 3 deletions python/sdist/amici/antimony_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
https://antimony.sourceforge.net/
https://tellurium.readthedocs.io/en/latest/antimony.html
"""

from pathlib import Path
from typing import Union


def antimony2sbml(ant_model: Union[str, Path]) -> str:
def antimony2sbml(ant_model: str | Path) -> str:
"""Convert Antimony model to SBML.
:param ant_model: Antimony model as string or path to file
Expand Down Expand Up @@ -46,7 +46,7 @@ def antimony2sbml(ant_model: Union[str, Path]) -> str:
return sbml_str


def antimony2amici(ant_model: Union[str, Path], *args, **kwargs):
def antimony2amici(ant_model: str | Path, *args, **kwargs):
"""Convert Antimony model to AMICI model.
Converts the Antimony model provided as string of file to SBML and then imports it into AMICI.
Expand Down
1 change: 0 additions & 1 deletion python/sdist/amici/bngl_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
in the :term:`BNGL` format.
"""


from pysb.importers.bngl import model_from_bngl

from .pysb_import import pysb2amici
Expand Down
10 changes: 5 additions & 5 deletions python/sdist/amici/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@
Functionality for building the C++ extensions of an amici-created model
package.
"""

import subprocess
import sys
from typing import Optional, Union
from pathlib import Path
import os


def build_model_extension(
package_dir: Union[str, Path],
verbose: Optional[Union[bool, int]] = False,
compiler: Optional[str] = None,
extra_msg: Optional[str] = None,
package_dir: str | Path,
verbose: bool | int | None = False,
compiler: str | None = None,
extra_msg: str | None = None,
) -> None:
"""
Compile the model extension of an amici-created model package.
Expand Down
7 changes: 3 additions & 4 deletions python/sdist/amici/conserved_quantities_demartino.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import math
import random
import sys
from typing import Optional, Union
from collections.abc import MutableSequence, Sequence

from .logging import get_logger
Expand All @@ -21,8 +20,8 @@ def compute_moiety_conservation_laws(
num_species: int,
num_reactions: int,
max_num_monte_carlo: int = 20,
rng_seed: Union[None, bool, int] = False,
species_names: Optional[Sequence[str]] = None,
rng_seed: None | bool | int = False,
species_names: Sequence[str] | None = None,
) -> tuple[list[list[int]], list[list[float]]]:
"""Compute moiety conservation laws.
Expand Down Expand Up @@ -116,7 +115,7 @@ def _output(
int_matched: list[int],
species_indices: list[list[int]],
species_coefficients: list[list[float]],
species_names: Optional[Sequence[str]] = None,
species_names: Sequence[str] | None = None,
verbose: bool = False,
log_level: int = logging.DEBUG,
):
Expand Down
4 changes: 2 additions & 2 deletions python/sdist/amici/conserved_quantities_rref.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Find conserved quantities deterministically"""

from typing import Literal, Optional, Union
from typing import Literal

import numpy as np


def rref(
mat: np.array, round_ndigits: Optional[Union[Literal[False], int]] = None
mat: np.array, round_ndigits: Literal[False] | int | None = None
) -> np.array:
"""
Bring matrix ``mat`` to reduced row echelon form
Expand Down
14 changes: 7 additions & 7 deletions python/sdist/amici/cxxcodeprinter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""C++ code generation"""

import itertools
import os
import re
from typing import Optional
from collections.abc import Sequence
from collections.abc import Iterable

Expand Down Expand Up @@ -49,7 +49,7 @@ def __init__(self):
else:
self._fpoptimizer = None

def doprint(self, expr: sp.Expr, assign_to: Optional[str] = None) -> str:
def doprint(self, expr: sp.Expr, assign_to: str | None = None) -> str:
if self._fpoptimizer:
if isinstance(expr, list):
expr = list(map(self._fpoptimizer, expr))
Expand Down Expand Up @@ -124,7 +124,7 @@ def _get_sym_lines_symbols(
equations: sp.Matrix,
variable: str,
indent_level: int,
indices: Optional[Sequence[int]] = None,
indices: Sequence[int] | None = None,
) -> list[str]:
"""
Generate C++ code for where array elements are directly replaced with
Expand Down Expand Up @@ -230,8 +230,8 @@ def print_bool(expr) -> str:
def get_switch_statement(
condition: str,
cases: dict[int, list[str]],
indentation_level: Optional[int] = 0,
indentation_step: Optional[str] = " " * 4,
indentation_level: int | None = 0,
indentation_step: str | None = " " * 4,
):
"""
Generate code for a C++ switch statement.
Expand Down Expand Up @@ -296,8 +296,8 @@ def csc_matrix(
matrix: sp.Matrix,
rownames: list[sp.Symbol],
colnames: list[sp.Symbol],
identifier: Optional[int] = 0,
pattern_only: Optional[bool] = False,
identifier: int | None = 0,
pattern_only: bool | None = False,
) -> tuple[list[int], list[int], sp.Matrix, list[str], sp.Matrix]:
"""
Generates the sparse symbolic identifiers, symbolic identifiers,
Expand Down
49 changes: 25 additions & 24 deletions python/sdist/amici/de_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:py:func:`amici.sbml_import.SbmlImporter.sbml2amici` and
:py:func:`amici.petab_import.import_model`.
"""

from __future__ import annotations
import copy
import logging
Expand Down Expand Up @@ -1140,36 +1141,36 @@ def _write_model_header_cpp(self) -> None:
] = impl
continue

tpl_data[
f"{func_name.upper()}_DEF"
] = get_function_extern_declaration(
func_name, self.model_name, self.model.is_ode()
tpl_data[f"{func_name.upper()}_DEF"] = (
get_function_extern_declaration(
func_name, self.model_name, self.model.is_ode()
)
)
tpl_data[
f"{func_name.upper()}_IMPL"
] = get_model_override_implementation(
func_name, self.model_name, self.model.is_ode()
tpl_data[f"{func_name.upper()}_IMPL"] = (
get_model_override_implementation(
func_name, self.model_name, self.model.is_ode()
)
)
if func_name in sparse_functions:
tpl_data[
f"{func_name.upper()}_COLPTRS_DEF"
] = get_sunindex_extern_declaration(
func_name, self.model_name, "colptrs"
tpl_data[f"{func_name.upper()}_COLPTRS_DEF"] = (
get_sunindex_extern_declaration(
func_name, self.model_name, "colptrs"
)
)
tpl_data[
f"{func_name.upper()}_COLPTRS_IMPL"
] = get_sunindex_override_implementation(
func_name, self.model_name, "colptrs"
tpl_data[f"{func_name.upper()}_COLPTRS_IMPL"] = (
get_sunindex_override_implementation(
func_name, self.model_name, "colptrs"
)
)
tpl_data[
f"{func_name.upper()}_ROWVALS_DEF"
] = get_sunindex_extern_declaration(
func_name, self.model_name, "rowvals"
tpl_data[f"{func_name.upper()}_ROWVALS_DEF"] = (
get_sunindex_extern_declaration(
func_name, self.model_name, "rowvals"
)
)
tpl_data[
f"{func_name.upper()}_ROWVALS_IMPL"
] = get_sunindex_override_implementation(
func_name, self.model_name, "rowvals"
tpl_data[f"{func_name.upper()}_ROWVALS_IMPL"] = (
get_sunindex_override_implementation(
func_name, self.model_name, "rowvals"
)
)

if self.model.num_states_solver() == self.model.num_states_rdata():
Expand Down
4 changes: 3 additions & 1 deletion python/sdist/amici/de_model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
"""Symbolic differential equation model."""

from __future__ import annotations

import contextlib
import copy
import itertools
import re
from itertools import chain
from typing import Callable, TYPE_CHECKING
from typing import TYPE_CHECKING
from collections.abc import Callable
from collections.abc import Sequence

import numpy as np
Expand Down
Loading

0 comments on commit f59fed7

Please sign in to comment.