coin-or / coin-or/python-mip

"numbers.Real" makes it really hard to work with typechecking

Open
#173 7 comments 9 reactions 1 assignee Claimed by @sebheger View on GitHub
mip
Dominant language
Linear Programming
Stars
600
Forks
108
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
When working with `mypy` arguments of type `numbers.Real` cannot accept regular `float` or `int` values. Making working with them very unproductive due to having to `typing.cast(numbers.Real, value)` all over the code.

**To Reproduce**
Run mypy on this file for example:
```python
import numbers
import typing

import mip

model = mip.Model("Model")

x = 1
model.add_var("var1", lb=x) # "Literal[1]" is incompatible with "Real"

y = 1.0
model.add_var("var2", lb=y) # "float" is incompatible with "Real"

z: numbers.Real = 5.6 # "float" is incompatible with "Real"
model.add_var("var3", lb=z)

z = typing.cast(numbers.Real, 7.8) # have to do this everytime
model.add_var("var4", lb=z) # no problem
```

**Edit 1:** Additional steps are needed to generate the stub for the library.

```
export MYPYPATH=./out

stubgen -p mip
mypy main.py
```

**Expected behavior**
I understan reasoning for using `number.Real` however even the [PEP 484](https://www.python.org/dev/peps/pep-0484/#the-numeric-tower) suggests using float instead:

> Rather than requiring that users write import numbers and then use numbers.Float etc., this PEP proposes a straightforward shortcut that is almost as effective: when an argument is annotated as having type float, an argument of type int is acceptable; similar, for an argument annotated as having type complex, arguments of type float or int are acceptable. This does not handle classes implementing the corresponding ABCs or the fractions.Fraction class, but we believe those use cases are exceedingly rare.

**Desktop (please complete the following information):**
- Operating System, version: Windows 10 64bit
- Python version: 3.9
- Python-MIP version (we recommend you to test with the latest version): 1.13

**Additional context**
I know this isn't a proper bug, however it has been very frustrating to work with.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.