Skip to content

Commit

Permalink
fix warnings, use ruff to isort
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasr8 committed Mar 24, 2024
1 parent 4a36053 commit 1e633f0
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ jobs:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: python -m pip install -e .[dev]
- name: Run ruff
run: ruff check --verbose
- name: Run linters
- name: Run linters & formatters
run: pre-commit run --all-files
- name: Test with pytest
run: pytest
9 changes: 3 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ repos:
hooks:
# Run the linter.
- id: ruff
args: [--fix, --verbose]
args: [--fix]
# Run the formatter.
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
name: isort (python)
23 changes: 17 additions & 6 deletions pivotal/simplex.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
import math
import warnings
from collections.abc import Callable
from enum import Enum, auto
from typing import Literal
from typing import Literal, TypeVar

import numpy as np

from pivotal.errors import Infeasible, Unbounded
from pivotal.expressions import (Constraint, Equal, Expression, GreaterOrEqual, LessOrEqual, get_variable_coeffs,
get_variable_names)
from pivotal.expressions import (
Constraint,
Equal,
Expression,
GreaterOrEqual,
LessOrEqual,
get_variable_coeffs,
get_variable_names,
)


class ProgramType(Enum):
MIN = auto()
MAX = auto()


def suppress_divide_by_zero_warning(fn) -> None:
def _fn_suppressed(*args, **kwargs):
_T = TypeVar("_T")


def suppress_divide_by_zero_warning(fn: Callable[..., _T]) -> None:
def _fn_suppressed(*args, **kwargs) -> _T:
with warnings.catch_warnings():
warnings.simplefilter("ignore", RuntimeWarning)
return fn(*args, **kwargs)
Expand Down Expand Up @@ -71,7 +82,7 @@ def __repr__(self) -> str:


class Tableau:
def __init__(
def __init__( # noqa: PLR0913
self, A: np.ndarray, b: np.ndarray, c: np.ndarray, pivots: Pivots | None = None, *, tolerance: float = 1e-6
) -> None:
self.M = np.block([[np.atleast_2d(c), np.atleast_2d(0)], [A, np.atleast_2d(b).T]])
Expand Down
12 changes: 7 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,12 @@ dependencies = ["numpy"]
dynamic = ["version"]

[project.optional-dependencies]
dev = ["pytest", "ruff", "isort", "pre-commit"]
dev = ["pytest", "ruff", "pre-commit"]

[project.urls]
Homepage = "https://github.com/tomasr8/pivotal"
Github = "https://github.com/tomasr8/pivotal"

[tool.isort]
line_length = 120
lines_after_imports = 2

[tool.ruff.lint]
select = ["ALL"]
ignore = [
Expand All @@ -58,9 +54,15 @@ ignore = [
"S101",
]

[tool.ruff.lint.per-file-ignores]
"tests/*" = ["ANN001", "ANN201", "PLR2004"]

[tool.ruff]
line-length = 120

[tool.ruff.lint.isort]
lines-after-imports = 2

[tool.setuptools]
packages = ["pivotal"]

Expand Down
Empty file added tests/__init__.py
Empty file.
4 changes: 2 additions & 2 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def assert_solution_almost_equal(expected, actual):
__traceback_hide__ = True # noqa: F841
assert np.isclose(expected[0], actual[0])
assert_allclose(expected[1], [actual[1][name] for name in sorted(list(actual[1].keys()))], atol=1e-8)
assert_allclose(expected[1], [actual[1][name] for name in sorted(actual[1].keys())], atol=1e-8)


def assert_equal(a, b):
Expand All @@ -19,7 +19,7 @@ def assert_equal(a, b):
def assert_array_equal(a, b):
__traceback_hide__ = True # noqa: F841
assert len(a) == len(b)
for x, y in zip(a, b):
for x, y in zip(a, b, strict=True):
assert_equal(x, y)


Expand Down
4 changes: 2 additions & 2 deletions tests/test_simplex.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def test_program_1():
value, X = _solve(Tableau(A, b, c, Pivots({})), max_iterations=math.inf, tolerance=1e-6)
X_true = [0.81818182, 1.72727273]
assert value == pytest.approx(4.27272727)
for x, xt in zip(X, X_true):
for x, xt in zip(X, X_true, strict=True):
assert x == pytest.approx(xt)


Expand All @@ -208,5 +208,5 @@ def test_program_2():
value, X = _solve(Tableau(A, b, c, Pivots({})), max_iterations=math.inf, tolerance=1e-6)
X_true = [1, 0, 7]
assert value == pytest.approx(-300)
for x, xt in zip(X, X_true):
for x, xt in zip(X, X_true, strict=True):
assert x == pytest.approx(xt)

0 comments on commit 1e633f0

Please sign in to comment.