Skip to content

COMPAT: support pulp 4 in spatial matching - #960

Open
lyn1dev wants to merge 1 commit into
pysal:mainfrom
lyn1dev:pulp4-matching
Open

lyn1dev wants to merge 1 commit into
pysal:mainfrom
lyn1dev:pulp4-matching

Conversation

@lyn1dev

@lyn1dev lyn1dev commented Nov 22, 2026

Copy link
Copy Markdown
  1. Tests were run locally with pytest on these changes (see below).
  2. This pull request is directed to the default branch.
  3. The change is covered by a new test.
  4. Reviewer assignment and labels need to be done by a maintainer (no write access).
  5. The justification for this PR is:

Graph.build_spatial_matches raises pulp.constants.PulpError: No solver available or AttributeError: type object 'LpVariable' has no attribute 'dicts' with pulp 4 installed, as reported in #944.

pulp 4 changed the API that the spatial matching implementation relies on:

  • pulp.LpVariable.dicts is gone; pulp.LpProblem.add_variable_dict (already present in 3.3) replaces it.
  • The pulp.LpConstraint(expression, sense=..., rhs=...) constructor is gone; constraint comparison operators are supported by both versions.
  • LpProblem.solve returns a pulp.LpSolveStats object instead of an int, and pulp.LpStatus no longer exists, so the warning message and the status exposed on the returned problem are read from the returned object.
  • No solver is selected by default any more, so solver=None now falls back to the first solver reported as available.

An infeasible problem no longer raises when no match was selected: the empty arrays are returned instead of failing to unpack the (empty) edge triple. That path was previously reached with pulp 3 and CBC leaving solution values behind, but HiGHS reports zeros, which is what surfaced the unpacking error.

The pulp<4 pins in the plus extra and in the latest CI environments are relaxed so pulp 4 is actually exercised; highspy supplies a solver there. The oldest environment keeps pulp<4, so backwards compatibility with pulp 3 stays covered by CI.

Testing

With pulp 4.3.1 and highspy 1.16.0:

$ python -m pytest libpysal/graph -q
479 passed, 28 skipped

The new test in libpysal/graph/tests/test_matching.py fails on unmodified main with pulp 4 (AttributeError: type object 'LpVariable' has no attribute 'dicts') and passes with the change. libpysal/graph/tests/test_matching.py also passes with pulp 3.3.2 and with pulp 3.0.0, which lacks add_variable_dict and therefore runs the LpVariable.dicts fallback. ruff check and ruff format --check pass on the changed files.

Fixes #944

AI was used to write this code.

pulp 4 removed `LpVariable.dicts`, changed the `LpConstraint` constructor
and made `LpProblem.solve` return an `LpSolveStats` object instead of an
int, which broke `Graph.build_spatial_matches` with a `No solver
available` or `AttributeError` when pulp 4 is installed.

Variables and constraints are now created through APIs implemented by
pulp 3 and 4 alike, the numeric status is read from whichever object
`solve` returns and is still exposed as `sol_status` on the problem
returned with `return_mip=True`. pulp 4 has no default solver, so the
first available solver is used when none is passed. An infeasible
problem that leaves no selected match now returns empty arrays instead
of raising.

The pin in the `plus` extra and in the latest CI environments is relaxed
to pick up pulp 4, with highspy providing the solver. pulp 3 stays
covered by the oldest environment.
@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.14286% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.0%. Comparing base (ce7bb98) to head (0280daa).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
libpysal/graph/_matching.py 96.8% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main    #960     +/-   ##
=======================================
+ Coverage   86.0%   86.0%   +0.1%     
=======================================
  Files        151     151             
  Lines      16570   16675    +105     
=======================================
+ Hits       14243   14347    +104     
- Misses      2327    2328      +1     
Files with missing lines Coverage Δ
libpysal/graph/tests/test_matching.py 98.4% <100.0%> (+0.1%) ⬆️
libpysal/graph/_matching.py 96.1% <96.8%> (+0.1%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@martinfleis
martinfleis requested a review from ljwolf November 23, 2026 06:17
@martinfleis martinfleis changed the title BUG: support pulp 4 in spatial matching COMPAT: support pulp 4 in spatial matching Nov 23, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pulp v4 not compatible

1 participant