coin-or / pulp

A python Linear Programming API
http://coin-or.github.io/pulp/
Other
2.01k stars 376 forks source link

add type hints (Experimental) #755

Open CubeSugarCheese opened 1 month ago

CubeSugarCheese commented 1 month ago

650

This pr still incomplete, some functions are hard to type, like LpVariable.matrix and LpVariable.dicts. And lots of usage of cast and # type: ignore to adapt old code base (some replace by not None assert).

CLAassistant commented 1 month ago

CLA assistant check
All committers have signed the CLA.

CubeSugarCheese commented 2 days ago

Thanks for your review.

At first, I think I can typed this lib suitably. But after some research, I'm got a discouraged. There many imprecision in old code, for example.

  1. Some place use implicit cast to bool to check None, it cant be understand by typechecker. And many places use literal 1 and 0 as bool, and some place use bool literal in the same time. they all come from [803c3d3], a 12 years ago commit. I dont know why, after search this keyword in all place just use as bool, I replace it as bool.
  2. Some place check None and some place not check in the same object/attr, even they init by None.
  3. Some recursive algorithm hard to typed.
  4. LpElement is inherited by LpVariable and LpConstraintVar, but almost all code assume a LpElement object must be LpVariable or LpConstraintVar, it violating the rules of the subtype system, so all LpElement replaced by Union[LpVariable, LpConstraintVar].

Sorry, the workload for this task far exceeds my estimation. In contrast, the benefits obtained from typed are too low. Now I no longer have courage and energy to modify these codes. Perhaps the only feasible way to type this lib is to start writing from scratch. So that we can let go of the burden of compatibility.

You can close this pr. Sorry again.

pchtsp commented 2 days ago

Thank you for your efforts though! With your permission I will branch your code and I will try to make it compatible. I understand the pains of dealing with legacy code. I was not in this project at the time and it's definitely a challenge also for me.

I'm confident that I can make something useful with the work you have. Thanks again for the help!