You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using MrPython in student mode, I discovered something that looks like a bug.
here's the code :
def functionWithAbs(a:int)->int:
""" using abs() with an integer should output an integer !..."""
return abs(a)
assert functionWithAbs(2)==2
I get the error "return value is a float":
Erreur: ligne 3
==> Type de retour erroné: Le type de retour déclaré pour la fonction 'functionWithAbs' est 'int' mais l'expression du `return` est de type incompatible: float
But in Python, applying abs() to an integer always gives an integer. So this error message a false positive.
(I got crazy when I tried to fix this error)
I noticed that when I tried to force "int" type
# doesn't work
return int(abs(a) // 2)
# but this works... (?)
return int(abs(a)) // 2
By the way, still in student mode, is it normal that you cannot use the type() inside a function ?
type() works fine out of a function body but
def useType(a:int)-> str:
""" test type"""
return type(a)
assert useType(2)=="int"
triggers an error: "type() is un unknown function" :
Erreur: ligne 5
==> Problème d'appel: je ne connais pas de fonction dont le nom est 'type'
Notice that I can evaluate "useType(2)" with no problem, even if I get this error...
It's annoying because type() can be useful for debugging purpose.
Thanks for your help.
The text was updated successfully, but these errors were encountered:
There are two questions in one.
For the abs() function, it is currently types float -> float for generality.
However, if I want to use it in an integer position, as e.g. in :
k : int=abs(x)
The typechecker will complain because potentially abs(x) can be a float, hence to avoir any problem
you might write instead :
k : int=int(abs(x))
Of course, you might expect that if x is of type int the conversion would not
be required... But the type-system is not capable of explaining that the
type of abs(x) to be int if x is typed int, or float instead.
One thing I could do is to make abs() a primitive operator similar to +, -, etc.
This is consistent with the fact that it is a primitive function in python.
But be aware that we cannot define such a function "by hand", it must be hardcoded in the typechecker.
Finally, the type() primitive returns an internal representation and not an actual type from the typing module,
and moreover I don't know how to represent types in the typechecker, so it should be phased out in student mode
(don't forget that expert mode is available for this kind of need).
Hi !
Using MrPython in student mode, I discovered something that looks like a bug.
here's the code :
I get the error "return value is a float":
But in Python, applying abs() to an integer always gives an integer. So this error message a false positive.
(I got crazy when I tried to fix this error)
I noticed that when I tried to force "int" type
By the way, still in student mode, is it normal that you cannot use the type() inside a function ?
type() works fine out of a function body but
triggers an error: "type() is un unknown function" :
Notice that I can evaluate "useType(2)" with no problem, even if I get this error...
It's annoying because type() can be useful for debugging purpose.
Thanks for your help.
The text was updated successfully, but these errors were encountered: