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

Full yield support, including yield expressions #498

Closed
rockneurotiko opened this issue Nov 12, 2014 · 2 comments · Fixed by #1137
Closed

Full yield support, including yield expressions #498

rockneurotiko opened this issue Nov 12, 2014 · 2 comments · Fixed by #1137
Labels

Comments

@rockneurotiko
Copy link
Contributor

In python 3.X yield is a statement (currently supported) but also an expression.
You can do something like this:

from typing import Iterator

def test_yield() -> Iterator[int]:
    value = 1
    try:
        value = yield value  # Here is the yield expression
        yield value
        yield value + 1
    except TypeError as e:
        print(e)

gen = test_yield()
x1 = next(gen)
x2 = gen.send(2)
print(x1)  # 1
print(x2)  # 2
gen.throw(TypeError, 'ERROR')
gen.close()

Right now, mypy complains in the expression:

_program.py: In function "test_yield":
_program.py, line 6: Parse error before "value"

And, also, the Iterator type need the functions send, throw and close, but I think this go in typing project:

_program.py, line 14: Iterator[int] has no attribute "send" 
_program.py, line 17: Iterator[int] has no attribute "throw" 
_program.py, line 18: Iterator[int] has no attribute "close"

The reference is here: https://docs.python.org/3.4/reference/expressions.html#yieldexpr

@rockneurotiko
Copy link
Contributor Author

Also, you can do a return inside a Iterator, and get in with the yield from expression.

This works:

from typing import List, Iterator, Any
trace = []  # type: List[Any]

def inner() -> Iterator[int]:
    try:
        yield 1
    except ValueError:
        trace.append("inner caught ValueError")
    return 2

def outer():
    v = yield from inner()
    trace.append("inner returned %r to outer" % v)
    yield v

g = outer()
trace.append(next(g))
trace.append(g.throw(ValueError))
assert(trace == [
    1,
    "inner caught ValueError",
    "inner returned 2 to outer",
    2,
])

@JukkaL JukkaL added the feature label Nov 21, 2014
@JukkaL JukkaL changed the title Full yield support. Full yield support, including yield expressions Dec 8, 2014
@JukkaL JukkaL added the priority label Dec 8, 2014
@JukkaL JukkaL removed the priority label Mar 29, 2015
@o11c
Copy link
Contributor

o11c commented Aug 8, 2015

Ugh, I just hit this bug while reimplementing the parser.

Obviously the new parser supports it (though I'm not touching semanal), but I need the tests to pass before switching everything over to actually use the new parser.

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

Successfully merging a pull request may close this issue.

3 participants