Skip to content

Commit

Permalink
Reformat code base and drop deprecated np.infty
Browse files Browse the repository at this point in the history
  • Loading branch information
daffidwilde committed Oct 24, 2024
1 parent 7881c93 commit 2a733d4
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repos:
- id: trailing-whitespace
name: Check for trailing whitespaces (auto-fixes)
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.3.5
rev: v0.7.0
hooks:
- id: ruff
args: [ --fix ]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ docs = [
"PyYAML>=6",
"quartodoc>=0.5.0; python_version>'3.8'",
]
lint = ["ruff<0.4"]
lint = ["ruff<1.0"]
dev = ["matching[docs,lint,test]", "pre-commit"]

[project.urls]
Expand Down
2 changes: 1 addition & 1 deletion src/matching/games/stable_marriage.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def solve(self, optimal="suitor"):
This method uses an extended version of the Gale-Shapley
algorithm that makes use of the inherent structures of SM
instances. The algorithm finds a unique, stable and optimal
matching is found for any valid set of suitors and reviewers.
matching for any valid set of suitors and reviewers.
The optimality of the matching is with respect to one party and
is subsequently the worst stable matching for the other party.
Expand Down
70 changes: 43 additions & 27 deletions tests/stable_marriage/test_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ def test_from_utilities(utilities):

suitor_utility, reviewer_utility = utilities

with mock.patch(
"matching.games.StableMarriage.check_input_validity"
) as validator, mock.patch("matching.convert.utility_to_rank") as ranker:
with (
mock.patch(
"matching.games.StableMarriage.check_input_validity"
) as validator,
mock.patch("matching.convert.utility_to_rank") as ranker,
):
effects = (suitor_utility.argsort(), reviewer_utility.argsort())
ranker.side_effect = list(effects)
game = StableMarriage.from_utilities(suitor_utility, reviewer_utility)
Expand Down Expand Up @@ -73,11 +76,12 @@ def test_from_preferences(preferences):

suitor_prefs, reviewer_prefs = preferences

with mock.patch(
"matching.games.StableMarriage.check_input_validity"
) as validator, mock.patch(
"matching.convert.preference_to_rank"
) as ranker:
with (
mock.patch(
"matching.games.StableMarriage.check_input_validity"
) as validator,
mock.patch("matching.convert.preference_to_rank") as ranker,
):
effects = (
np.array(list(suitor_prefs.values())),
np.array(list(reviewer_prefs.values())),
Expand Down Expand Up @@ -177,11 +181,14 @@ def test_check_input_validity(ranks):
suitor_ranks, reviewer_ranks = ranks
game = mocked_game(*ranks)

with mock.patch(
"matching.games.StableMarriage._check_number_of_players"
) as check_num_players, mock.patch(
"matching.games.StableMarriage._check_player_ranks"
) as check_player_ranks:
with (
mock.patch(
"matching.games.StableMarriage._check_number_of_players"
) as check_num_players,
mock.patch(
"matching.games.StableMarriage._check_player_ranks"
) as check_player_ranks,
):
game.check_input_validity()

check_num_players.assert_called_once_with()
Expand Down Expand Up @@ -227,13 +234,17 @@ def test_solve_valid_optimal(ranks, optimal, solution):

game = mocked_game(*ranks)

with mock.patch(
"matching.games.StableMarriage._invert_player_sets"
) as player_set_inverter, mock.patch(
"matching.games.StableMarriage._stable_marriage"
) as stable_marriage, mock.patch(
"matching.matchings.SingleMatching.invert"
) as matching_inverter:
with (
mock.patch(
"matching.games.StableMarriage._invert_player_sets"
) as player_set_inverter,
mock.patch(
"matching.games.StableMarriage._stable_marriage"
) as stable_marriage,
mock.patch(
"matching.matchings.SingleMatching.invert"
) as matching_inverter,
):
stable_marriage.return_value = solution
matching_inverter.return_value = "inverted_matching"
matching = game.solve(optimal)
Expand All @@ -260,13 +271,18 @@ def test_solve_invalid_optimal_raises(ranks, optimal):
game = mocked_game(*ranks)

match = "^Invalid choice for `optimal`."
with mock.patch(
"matching.games.StableMarriage._invert_player_sets"
) as player_set_inverter, mock.patch(
"matching.games.StableMarriage._stable_marriage"
) as stable_marriage, mock.patch(
"matching.matchings.SingleMatching.invert"
) as matching_inverter, pytest.raises(ValueError, match=match):
with (
mock.patch(
"matching.games.StableMarriage._invert_player_sets"
) as player_set_inverter,
mock.patch(
"matching.games.StableMarriage._stable_marriage"
) as stable_marriage,
mock.patch(
"matching.matchings.SingleMatching.invert"
) as matching_inverter,
pytest.raises(ValueError, match=match),
):
game.solve(optimal)

player_set_inverter.assert_not_called()
Expand Down

0 comments on commit 2a733d4

Please sign in to comment.