Skip to content

Commit d8a3197

Browse files
authored
Make pytest happy to not throw warnings (#305)
* Treat warnings as error in pytest (except for pypy) * Declare strings with math syntax as raw strings * Fix tsp test case * free gurobi env only if loaded successfully * run numpy tests also for version 3.7 * Don't run pytest on mip itself * Include content of requirements.txt into pyproject.toml * add numpy to test settings * constraint priority typehint fix * import mip.constants in conflicts.py * add mip.constants to test_conflicts
1 parent f888867 commit d8a3197

File tree

10 files changed

+14
-21
lines changed

10 files changed

+14
-21
lines changed

Diff for: .github/workflows/github-ci.yml

+2-7
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,12 @@ jobs:
8282
path: ${{ steps.pip-cache.outputs.dir }}
8383
key: ${{ runner.os }}-${{ matrix.python-version }}-pythonpip
8484

85-
- name: Install dependencies from requirements.txt
86-
run: python -m pip install -r requirements.txt
87-
8885
- name: Install additional requirements (CPython)
8986
if: ${{ matrix.python-version != 'pypy3.9-v7.3.9' }}
9087
run: python -m pip install "matplotlib==3.5.2" "gurobipy==9.5.1"
9188

9289
- name: Install mip
93-
run: python -m pip install .
90+
run: python -m pip install .[test,numpy]
9491

9592
- name: list installed packages
9693
run: python -m pip list
@@ -99,10 +96,8 @@ jobs:
9996
if: ${{ matrix.python-version == 'pypy3.9-v7.3.9'}}
10097
run: |
10198
python -m pytest test --verbose --color=yes --doctest-modules --ignore="test/test_gurobi.py"
102-
python -m pytest mip --verbose --color=yes --doctest-modules --ignore="mip/gurobi.py"
10399
104100
- name: Run tests
105101
if: ${{ matrix.python-version != 'pypy3.9-v7.3.9'}}
106102
run: |
107-
python -m pytest test --verbose --color=yes --doctest-modules
108-
python -m pytest mip --verbose --color=yes --doctest-modules --ignore="mip/gurobi.py"
103+
python -m pytest test --verbose --color=yes --doctest-modules -Werror

Diff for: examples/plant_location.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Example with Special Ordered Sets (SOS) type 1 and 2.
1+
r"""Example with Special Ordered Sets (SOS) type 1 and 2.
22
Plant location: one industry plans to install two plants in two different
33
regions, one to the west and another to the east. It must decide also the
44
production capacity of each plant and allocate clients to plants

Diff for: mip/conflict.py

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from enum import Enum
33

44
import mip
5+
import mip.constants
56

67
logger = logging.getLogger("conflict")
78

Diff for: mip/entities.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -516,12 +516,12 @@ def name(self) -> str:
516516
return self.__model.solver.constr_get_name(self.idx)
517517

518518
@property
519-
def priority(self) -> mip.constants.ConstraintPriority:
519+
def priority(self) -> "mip.constants.ConstraintPriority":
520520
"""priority value"""
521521
return self.__priority
522522

523523
@priority.setter
524-
def priority(self, priority: mip.constants.ConstraintPriority):
524+
def priority(self, priority: "mip.constants.ConstraintPriority"):
525525
self.__priority = priority
526526

527527

@@ -752,7 +752,7 @@ def idx(self) -> int:
752752

753753
class ConflictGraph:
754754

755-
"""A conflict graph stores conflicts between incompatible assignments in
755+
r"""A conflict graph stores conflicts between incompatible assignments in
756756
binary variables.
757757
758758
For example, if there is a constraint :math:`x_1 + x_2 \leq 1` then

Diff for: mip/gurobi.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ def __init__(self, model: Model, name: str, sense: str, modelp: CData = ffi.NULL
356356
self._model = ffi.NULL
357357
self._callback = None
358358
self._ownsModel = True
359+
self._venv_loaded = False
359360
self._nlazy = 0
360361

361362
if modelp == ffi.NULL:
@@ -397,6 +398,7 @@ def __init__(self, model: Model, name: str, sense: str, modelp: CData = ffi.NULL
397398
self._model = modelp
398399
self._env = GRBgetenv(self._model)
399400

401+
self._venv_loaded = True
400402
# default number of threads
401403
self.__threads = 0
402404

@@ -429,7 +431,7 @@ def __del__(self):
429431
if self._ownsModel:
430432
if self._model:
431433
GRBfreemodel(self._model)
432-
if self._env:
434+
if self._env and self._venv_loaded:
433435
GRBfreeenv(self._env)
434436

435437
def add_var(

Diff for: pyproject.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ dynamic = ["version"]
3535
dependencies = ["cffi==1.15.0"]
3636

3737
[project.optional-dependencies]
38-
numpy = ["numpy==1.23.1; python_version >= '3.8'", "numpy==1.21.6; python_version == '3.7'"]
38+
numpy = ["numpy==1.24.1; python_version >= '3.8'", "numpy==1.21.6; python_version == '3.7'"]
3939
gurobi = ["gurobipy>=8"]
40+
test = ["pytest==7.2.0", "networkx==2.6.3"]
4041

4142
[project.urls]
4243
"Homepage" = "https://www.python-mip.com"

Diff for: requirements.txt

-5
This file was deleted.

Diff for: test/mip_test.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def test_tsp(solver: str):
281281
class SubTourCutGenerator(ConstrsGenerator):
282282
"""Class to generate cutting planes for the TSP"""
283283

284-
def generate_constrs(self, model: Model):
284+
def generate_constrs(self, model: Model, depth: int = 0, npass: int = 0):
285285
G = nx.DiGraph()
286286
r = [(v, v.x) for v in model.vars if v.name.startswith("x(")]
287287
U = [v.name.split("(")[1].split(",")[0] for v, f in r]

Diff for: test/numpy_test.py

-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import sys
1010

1111

12-
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
1312
def test_numpy():
1413
model = Model()
1514
N = 1000
@@ -33,7 +32,6 @@ def test_numpy():
3332
assert result == OptimizationStatus.OPTIMAL
3433

3534

36-
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
3735
def test_LinExprTensor():
3836
model = Model()
3937
x = model.add_var_tensor(shape=(3,), name="x")

Diff for: test/test_conflict.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import pytest
22
import mip
3+
import mip.constants
34
from mip.conflict import ConflictFinder, IISFinderAlgorithm, ConflictRelaxer
45
import random
56
import numpy as np

0 commit comments

Comments
 (0)