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

Functions without a return statement always type-check #57

Open
Kerl13 opened this issue Sep 26, 2018 · 4 comments
Open

Functions without a return statement always type-check #57

Kerl13 opened this issue Sep 26, 2018 · 4 comments

Comments

@Kerl13
Copy link
Contributor

Kerl13 commented Sep 26, 2018

The result of this function has type NoneType and not int

def f(x):
    """ int -> int """

assert f(0)==f(0)
assert f(1)==f(1)

=== Interprétation de : 'test.py' ===
==> Toutes les fonctions sont testées (bien)
==> le programme est bien typé (très bien)
==> Tous les 2 tests sont passés avec succès

(Mr Python version 06786b5)

@fredokun
Copy link
Collaborator

It's not that simple ... because I cannot be sure that the last instruction will be a "return None" because it's potential dead code ... I think I can still give a type error for non-NoneType functions when I do not encounter any return (but I cannot be 100% precise without a control-flow analysis...).

@fredokun
Copy link
Collaborator

A solution that is "acceptable", I think, has been push in commit c72b9e1
What do you think of this ?

@Kerl13
Copy link
Contributor Author

Kerl13 commented Sep 27, 2018

This is better but there are still some unhandled cases

def f(x):
    """ int -> int """
    if x > 5:
        return x

Moreover, I guess you forgot to remove some debugging print in the code:

==> Toutes les fonctions sont testées (bien)
==> le programme est bien typé (très bien)
Call(func=Name(id='f', ctx=Load()), args=[
Num(n=6),
], keywords=[])
Call(func=Name(id='f', ctx=Load()), args=[
Num(n=7),
], keywords=[])
==> Tous les 2 tests sont passés avec succès

@fredokun fredokun added the todo (pstl) special label for student projects label Feb 28, 2019
@OlivierNicole
Copy link

I concur. In this (nonsensical) code from a student, I noticed that when there is no unconditional return at the end, the ill-typed assignment res = L[i:j] is not reported:

def eval_chiffres(L):
    """list[int]->int"""

    if len(L)==0:
        return 0

    else:
        #res:int
        res=0
        #x:int
        x=L[0]
        #i:int
        for i in L:
            #j:int
            for j in L:
                if i>x:
                    i=L[0]
                res=L[i:j]
        return res

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

No branches or pull requests

3 participants