Skip to content

Check code after inferred UninhabitedType #4059

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

Merged
merged 2 commits into from
Oct 20, 2017

Conversation

ilevkivskyi
Copy link
Member

@ilevkivskyi ilevkivskyi commented Oct 4, 2017

Fixes #3994

Currently code after inferred UninhabitedType is silently skipped, for example no errors are shown in this code:

T = TypeVar('T')
def f(x: List[T]) -> T:
    ...

class C:
    x = f([])
    def m(self) -> str:
        return 42 # No error!

if bool():
    f([])
    1 + ''  # No error!

There are other scenarios in tests. With this PR errors are shown as expected, since UninhabitedTypes resulting from ambiguous inference don't influence binder.

@JukkaL JukkaL self-assigned this Oct 11, 2017
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this! It's important to fix false negatives, since we want users to be able to trust type checking results. I have just one small comment.

mypy/types.py Outdated
@@ -357,6 +357,7 @@ class UninhabitedType(Type):
can_be_true = False
can_be_false = False
is_noreturn = False # Does this come from a NoReturn? Purely for error messages.
ambiguous = False # Is this a result of inference for a variable without constraints?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also mention why we care about this and where this makes a difference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also mention why we care about this and where this makes a difference.

I added a short discussion.

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 11, 2017

This generates a bunch of new errors in Dropbox internal codebases. Since this fixes false negatives, they may just be issues in our code, but I'll have to verify it.

@ilevkivskyi
Copy link
Member Author

This generates a bunch of new errors in Dropbox internal codebases. Since this fixes false negatives, they may just be issues in our code, but I'll have to verify it.

I think this PR shouldn't bring anything false positive, but will be interested to see your findings.

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 12, 2017

This PR seems to cause an invalid type to be inferred for the following code:

from typing import Any

class C(dict):
    def f(self, x: Any) -> None:
        y = super(C, x).__new__(x)
        reveal_type(y)  # None, which is unexpected

Previously the reveal_type line was skipped. The underlying issue doesn't seem new though, and similar problems also happen on master.

This works unexpectedly also on master (even when using --strict-optional):

from typing import TypeVar

T = TypeVar('T')

def f() -> T: pass

x = f()
reveal_type(x)  # None

The issue seems rare enough that it doesn't block merging this, but it would be nice to at least understand what is going on.

@ilevkivskyi
Copy link
Member Author

The issue seems rare enough that it doesn't block merging this, but it would be nice to at least understand what is going on.

I don't know if it is related or not, but I have seen some problems in meet.py with None and UninhabitedType. Another possible suspect is in partial None types, I remember looking at it recently I didn't really like how NoneTyp vs UninhabitedType is treated, I will check tomorrow morning if this can be fixed quickly, if not, then I propose to merge this and take a look at this problem later (you already opened an issue about this).

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 12, 2017 via email

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 13, 2017

Now that #4112 was merged, the motivating example generates an error (though not all errors that we'd like) so this seems a little less urgent now. I may postpone this until the next release.

@JukkaL JukkaL merged commit 1673e27 into python:master Oct 20, 2017
JukkaL added a commit that referenced this pull request Oct 20, 2017
JukkaL added a commit that referenced this pull request Oct 20, 2017
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

Successfully merging this pull request may close these issues.

2 participants