Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Errors using abs() with an integer and using type() in student mode #140

Open
laurentVeliscek opened this issue Nov 3, 2020 · 1 comment

Comments

@laurentVeliscek
Copy link

laurentVeliscek commented Nov 3, 2020

Hi !

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.

@fredokun
Copy link
Collaborator

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

@fredokun fredokun added the todo (pstl) special label for student projects label Feb 5, 2021
@fredokun fredokun removed the todo (pstl) special label for student projects label Feb 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants