Python mypy: float and int are incompatible types with numbers.Real

2024/11/18 4:50:08

I am new to Python's static typing module mypy. I am trying to append ints and floats to an array, which I typed statically to be Real. But mypy says that they are incompatible types with Real. I thought ints and floats are a subtype of Real?

from typing import List
from numbers import Realdata : List[Real] = []
with open(path, 'r') as file:for line in file:line = line.strip()if subject == 'time':data.append(float(line))else:data.append(int(line))

Error message:

graph.py:56: error: Argument 1 to "append" of "list" has incompatible type "float"; expected "Real"
graph.py:58: error: Argument 1 to "append" of "list" has incompatible type "int"; expected "Real"
Answer

As AChampion says you can just use float in place of numbers.Real. Pep 0484 (specifically here) basically says that in the context of type-checking the hierarchy complex > float > int is respected and that numbers.* doesn't need to be used.

This is a known 'issue' with mypy and was raised and discussed here. I haven't read the entire discussion but the tldr seems to be that Pep 0484 is enough to make this a low priority issue (and it hasn't been resolved in 5 years).

I will say that (as the issue discussion mentioned) it would be nice if mypy gave some indication of this as a known issue in it's error message, which it currently (mypy 0.991) doesn't.

https://en.xdnf.cn/q/71101.html

Related Q&A

Search/Find functionality in QTableView

I have a QWidget and inside that, there is a QTableView. I need to have a find functionality on the first column of the table, so when I click on Ctrl+F, a find dialog will pop-up.class Widget(QWidget)…

How does searching with pip work?

Yes, Im dead serious with this question. How does searching with pip work?The documentation of the keyword search refers to a "pip search reference" at https://pip.pypa.io/en/stable/user_gui…

keras LSTM feeding input with the right shape

I am getting some data from a pandas dataframe with the following shapedf.head() >>> Value USD Drop 7 Up 7 Mean Change 7 Change Predict 0.06480 2.0 4.0 -0.000429 …

Problems with a binary one-hot (one-of-K) coding in python

Binary one-hot (also known as one-of-K) coding lies in making one binary column for each distinct value for a categorical variable. For example, if one has a color column (categorical variable) that ta…

How to hide the title bar in pygame?

I was wondering does anyone know how to hide the pygame task bar?I really need this for my pygame program!Thanks!

Deleting existing class variable yield AttributeError

I am manipulating the creation of classes via Pythons metaclasses. However, although a class has a attribute thanks to its parent, I can not delete it.class Meta(type):def __init__(cls, name, bases, dc…

Setting global font size in kivy

What is the preferred way, whether through python or the kivy language, to set the global font size (i.e. for Buttons and Labels) in kivy? What is a good way to dynamically change the global font size…

What is the difference between load name and load global in python bytecode?

load name takes its argument and pushes onto the stack the value of the name stored by store name at the position indicated by the argument . load global does something similar, but there appears to …

porting Python 2 program to Python 3, random line generator

I have a random line generator program written in Python2, but I need to port it to Python3. You give the program the option -n [number] and a file argument to tell it to randomly output [number] numbe…

Symbol not found, Expected in: flat namespace

I have a huge gl.pxd file with all the definitions of gl.h, glu.h and glut.h. For example it has these lines:cdef extern from <OpenGL/gl.h>:ctypedef unsigned int GLenumcdef void glBegin( GLenum m…