Skip to content

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

@laurentVeliscek

Description

@laurentVeliscek

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions