coin-or / pulp

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

static type checks with `mypy` #650

Open AngryMaciek opened 1 year ago

AngryMaciek commented 1 year ago

Describe the new feature

Is there any way to build/install package stubs for pulp?
I was running static type check analysis with mypy but these seem missing:

file.py:19: error: Skipping analyzing "pulp": module is installed, but missing library stubs or py.typed marker  [import]

Additional info

Please answer these questions before submitting your feature request.

Is your feature request related to an issue? Please include the issue number.

No, I have checked other issues, nothing seems related to this right now.

Does this feature exist in another product or project? Please provide a link.

Some project already have type stubs included inside (ex: numpy), for some others there are external packages, like pandas: https://pypi.org/project/pandas-stubs/

pchtsp commented 7 months ago

this is something that would be nice to have, even just to make the IDE experience more friendly. We're slowly getting docstrings and type annotations. We're a long way drom having stubs. If you feel like having a PR on any of this, it will be very welcomed.

CubeSugarCheese commented 3 months ago

Is there any latest progress on this issue? The infomation about type hint are here: https://peps.python.org/pep-0484/ and https://docs.python.org/3/library/typing.html Before adding type hints, we need to answer some questions.

  1. Embedded or External? We can use external type hint (in *.pyi file), it will add some maintenance costs to handle seperate declarations. Or we can directly add to py files, it will force subsequent contributors to handle it.

  2. what's the lowest compatible python version? Type hint having gone through multiple python versions to improve. For example, ParamSpec and Concatenate added in python 3.10, they are use for decorator, and we can use A | B instead of typing.Union[A, B]. Python 3.11 added TypeVarTuple for variadic generic support. Python 3.9 added builtin collection subscripting, we can use list[int] instead of typing.List[int].

Note that readme described "PuLP requires Python 3.7 or newer.". The target verion is 3.7 ?

CubeSugarCheese commented 3 months ago

Is there any latest progress on this issue? The infomation about type hint are here: https://peps.python.org/pep-0484/ and https://docs.python.org/3/library/typing.html Before adding type hints, we need to answer some questions.

  1. Embedded or External? We can use external type hint (in *.pyi file), it will add some maintenance costs to handle seperate declarations. Or we can directly add to py files, it will force subsequent contributors to handle it.
  2. what's the lowest compatible python version? Type hint having gone through multiple python versions to improve. For example, ParamSpec and Concatenate added in python 3.10, they are use for decorator, and we can use A | B instead of typing.Union[A, B]. Python 3.11 added TypeVarTuple for variadic generic support. Python 3.9 added builtin collection subscripting, we can use list[int] instead of typing.List[int].

Note that readme described "PuLP requires Python 3.7 or newer.". The target verion is 3.7 ?

I'd be glad to adding this type hints.

pchtsp commented 3 months ago

Thanks for volunteering to help. I answer your questions:

  1. Embedded.
  2. Target version is 3.7.
CubeSugarCheese commented 3 months ago

Thanks for volunteering to help. I answer your questions:

  1. Embedded.
  2. Target version is 3.7.

Noted that AffineExpression inheritaged from dict, it's hard to typing, can I change the impl to inheritage from MutableMapping[KT, KV] and impl method on manual with and inner dict like __inner_dict: Dict[KT, VT]

Or we can remove those code. git log shows that they are from 11 years ago.

_DICT_TYPE = dict
if sys.platform not in ["cli"]:
    # iron python does not like an OrderedDict
    try:
        from odict import OrderedDict

        _DICT_TYPE = OrderedDict
    except ImportError:
        pass
    try:
        # python 2.7 or 3.1
        from collections import OrderedDict

        _DICT_TYPE = OrderedDict
    except ImportError:
        pass
class LpAffineExpression(_DICT_TYPE):

become

class LpAffineExpression(Dict[KT, VT]):