question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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:

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 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.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
sebhegercommented, May 7, 2022

I am going to add mypy to CI. See #270. There I started to refactor the numbers.Real for typing to make it mypy compatible.

0reactions
positacommented, Dec 15, 2021

I don’t know if you saw, but I pulled out a lot of my own number typing work-arounds into their own package called numerary. If this journey lines up with your own, numerary could be an intermediary solution until python/mypy#3186 is fixed. (Alternatively, the techniques used therein might provide inspiration if you can’t take a dependency.) Happy to consult here, if helpful. Apologies if this is a distraction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python Type Checking (Guide) - Real Python
In this guide, you'll look at Python type checking. Traditionally, types have been handled by the Python interpreter in a flexible but implicit...
Read more >
python 3.x - mypy with numbers.Number - Stack Overflow
First thing to note: issubclass(int, Number) and issubclass(float, Number) both evaluate to True. This is very surprising type-checking ...
Read more >
How hard is type checking in X? : r/programming - Reddit
Maybe in theory, but for actual code it could be very hard. Take a function like ioctl or even printf where the parameter...
Read more >
Python Type Checking - TestDriven.io
Python is a strongly typed, dynamic programming language. ... There are a number of tools out there that use type hints for static...
Read more >
Type Checking in Compiler Design - GeeksforGeeks
Whatever the compiler we use, while it is compiling the program, it has to follow the type rules of the language. Every language...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found