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

False positive error on return in generator #16

Closed
JelleZijlstra opened this issue May 12, 2018 · 3 comments
Closed

False positive error on return in generator #16

JelleZijlstra opened this issue May 12, 2018 · 3 comments

Comments

@JelleZijlstra
Copy link
Contributor

If you use return in a generator, an incorrect TypeError is generated:

$ cat srcdir/genreturn.py 
from typing import Iterable

def gen(x: int) -> Iterable[int]:
    for i in range(x):
        if i > 10:
            return
        else:
            yield i
$ pyre --source-directory srcdir/ check
 ƛ Setting up a .pyre_configuration file may reduce overhead.
 ƛ Found 1 type error!
srcdir/genreturn.py:6:12 Incompatible return type [7]: Expected `Iterable[int]` but got `None`.
$ python --version
Python 3.7.0b4
$ pip freeze | grep pyre  # related minor bug: "pyre --version" throws a TypeError
pyre-check==0.0.5
@dkgi
Copy link
Contributor

dkgi commented May 12, 2018

Good catch. I remember implementing something to handle returns inside generators at some point. I'll take a look at this.

@dkgi dkgi self-assigned this May 12, 2018
@JelleZijlstra
Copy link
Contributor Author

I actually looked into fixing this myself. There is indeed a check for (Type.is_none actual && Type.is_generator expected) in analysisTypeCheck.ml that suppresses the error I reported. However, this check simply looks for typing.(Async)Generator in the return type, which is wrong. Instead, we should check whether the function is a generator by looking for yields inside the function. I started writing an is_generator method in astStatement.ml, but I think we'll need some change in data structures to get access to this information in analysisTypeCheck.ml.

@dkgi
Copy link
Contributor

dkgi commented May 12, 2018

Oh, in this case what you probably want something like a Define.is_generator function that looks at all the statements in the function body. The one complication I see here is that you probably want to run a visitor to find the yield statement in the body. We can't really use Visitor in astStatement because that would introduce a circular dependency. You might have to do it in annotatedDefine.ml instead.

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

2 participants