Skip to content

Commit

Permalink
Make pytest happy to not throw warnings (#305)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sebheger authored Jan 4, 2023
1 parent f888867 commit d8a3197
Show file tree
Hide file tree
Showing 10 changed files with 14 additions and 21 deletions.
9 changes: 2 additions & 7 deletions .github/workflows/github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@ jobs:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-${{ matrix.python-version }}-pythonpip

- name: Install dependencies from requirements.txt
run: python -m pip install -r requirements.txt

- name: Install additional requirements (CPython)
if: ${{ matrix.python-version != 'pypy3.9-v7.3.9' }}
run: python -m pip install "matplotlib==3.5.2" "gurobipy==9.5.1"

- name: Install mip
run: python -m pip install .
run: python -m pip install .[test,numpy]

- name: list installed packages
run: python -m pip list
Expand All @@ -99,10 +96,8 @@ jobs:
if: ${{ matrix.python-version == 'pypy3.9-v7.3.9'}}
run: |
python -m pytest test --verbose --color=yes --doctest-modules --ignore="test/test_gurobi.py"
python -m pytest mip --verbose --color=yes --doctest-modules --ignore="mip/gurobi.py"
- name: Run tests
if: ${{ matrix.python-version != 'pypy3.9-v7.3.9'}}
run: |
python -m pytest test --verbose --color=yes --doctest-modules
python -m pytest mip --verbose --color=yes --doctest-modules --ignore="mip/gurobi.py"
python -m pytest test --verbose --color=yes --doctest-modules -Werror
2 changes: 1 addition & 1 deletion examples/plant_location.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Example with Special Ordered Sets (SOS) type 1 and 2.
r"""Example with Special Ordered Sets (SOS) type 1 and 2.
Plant location: one industry plans to install two plants in two different
regions, one to the west and another to the east. It must decide also the
production capacity of each plant and allocate clients to plants
Expand Down
1 change: 1 addition & 0 deletions mip/conflict.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from enum import Enum

import mip
import mip.constants

logger = logging.getLogger("conflict")

Expand Down
6 changes: 3 additions & 3 deletions mip/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,12 @@ def name(self) -> str:
return self.__model.solver.constr_get_name(self.idx)

@property
def priority(self) -> mip.constants.ConstraintPriority:
def priority(self) -> "mip.constants.ConstraintPriority":
"""priority value"""
return self.__priority

@priority.setter
def priority(self, priority: mip.constants.ConstraintPriority):
def priority(self, priority: "mip.constants.ConstraintPriority"):
self.__priority = priority


Expand Down Expand Up @@ -752,7 +752,7 @@ def idx(self) -> int:

class ConflictGraph:

"""A conflict graph stores conflicts between incompatible assignments in
r"""A conflict graph stores conflicts between incompatible assignments in
binary variables.
For example, if there is a constraint :math:`x_1 + x_2 \leq 1` then
Expand Down
4 changes: 3 additions & 1 deletion mip/gurobi.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ def __init__(self, model: Model, name: str, sense: str, modelp: CData = ffi.NULL
self._model = ffi.NULL
self._callback = None
self._ownsModel = True
self._venv_loaded = False
self._nlazy = 0

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

self._venv_loaded = True
# default number of threads
self.__threads = 0

Expand Down Expand Up @@ -429,7 +431,7 @@ def __del__(self):
if self._ownsModel:
if self._model:
GRBfreemodel(self._model)
if self._env:
if self._env and self._venv_loaded:
GRBfreeenv(self._env)

def add_var(
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ dynamic = ["version"]
dependencies = ["cffi==1.15.0"]

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

[project.urls]
"Homepage" = "https://www.python-mip.com"
Expand Down
5 changes: 0 additions & 5 deletions requirements.txt

This file was deleted.

2 changes: 1 addition & 1 deletion test/mip_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ def test_tsp(solver: str):
class SubTourCutGenerator(ConstrsGenerator):
"""Class to generate cutting planes for the TSP"""

def generate_constrs(self, model: Model):
def generate_constrs(self, model: Model, depth: int = 0, npass: int = 0):
G = nx.DiGraph()
r = [(v, v.x) for v in model.vars if v.name.startswith("x(")]
U = [v.name.split("(")[1].split(",")[0] for v, f in r]
Expand Down
2 changes: 0 additions & 2 deletions test/numpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import sys


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


@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
def test_LinExprTensor():
model = Model()
x = model.add_var_tensor(shape=(3,), name="x")
Expand Down
1 change: 1 addition & 0 deletions test/test_conflict.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
import mip
import mip.constants
from mip.conflict import ConflictFinder, IISFinderAlgorithm, ConflictRelaxer
import random
import numpy as np
Expand Down

0 comments on commit d8a3197

Please sign in to comment.