Skip to content

Totality Checks #388

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

Closed
mvcisback opened this issue Aug 16, 2014 · 3 comments
Closed

Totality Checks #388

mvcisback opened this issue Aug 16, 2014 · 3 comments
Labels
bug mypy got something wrong

Comments

@mvcisback
Copy link
Contributor

In a lot of typed languages, the compiler can be told to check if a function is total, handles all cases.

For Example:

def foo(x: Union[int, str]) -> str:
    if isinstance(x, int):
        return "bar"

Is only a partial function. If x is a str, then this function actually returns None.
Thus the output type should really be Union[str, NoneType]

I tested this snippet and mypy doesn't seem to catch this.
I'm not 100% sure, but I think if a function is partial, NoneType is a potential return type.

@mvcisback
Copy link
Contributor Author

An informal proof might be:

Suppose an input case is not handled, then either

  1. There is a type error, because the program is forcing the input down an incompatible code path.
  2. The input bypasses all control flow in the function (as in the foo example).

The first case (hopefully) doesn't pass the type checker.
The second case, returns NoneType (because of pythons implicit return None)

@JukkaL JukkaL added the bug label Aug 17, 2014
@JukkaL
Copy link
Collaborator

JukkaL commented Aug 17, 2014

This is related to #320. As pointed out by Guido, this is somewhat tricky to implement.

@gvanrossum
Copy link
Member

Let's call this a duplicate of #320.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

3 participants