| Summary: | No support for deconstructing tuple in 'with' statement | ||
|---|---|---|---|
| Product: | [Developer tools] kdev-python | Reporter: | Nicolás Alvarez <nalvarez> |
| Component: | Language support | Assignee: | Francis Herne <mail> |
| Status: | RESOLVED FIXED | ||
| Severity: | normal | CC: | |
| Priority: | NOR | ||
| Version First Reported In: | 5.2.4 | ||
| Target Milestone: | --- | ||
| Platform: | Debian testing | ||
| OS: | Linux | ||
| Latest Commit: | https://commits.kde.org/kdev-python/d619a731dbcdc724630118d521583be41f0cf7d3 | Version Fixed/Implemented In: | |
| Sentry Crash Report: | |||
Yes, it's missing. The current handling is just wrong, and doesn't use __enter__() at all.
class Enterable:
def __enter__(self):
return "value"
def __exit__(self, *args):
pass
with Enterable() as foo:
print(foo) # got type 'Enterable' !!!
There are other weird cases that we don't handle, e.g.
my_list = [1, 2, 3]
with Enterable as my_list[1]:
# my_list should be `[1, "value", 3]`
Git commit d619a731dbcdc724630118d521583be41f0cf7d3 by Francis Herne. Committed on 10/10/2018 at 00:01. Pushed by flherne into branch '5.3'. Improve support for 'with' statements. The previous code didn't look at `__enter__()`, and assumed that all context-managers returned their own type. We also didn't account for targets other than simple names, e.g. `with Mgr() as (foo, bar):` Thanks to Nicolás Alvarez. Related: bug 399534 Differential Revision: https://phabricator.kde.org/D16085 M +1 -0 documentation_files/builtindocumentation.py M +15 -1 duchain/declarationbuilder.cpp M +18 -0 duchain/tests/pyduchaintest.cpp M +1 -1 parser/ast.h M +1 -1 parser/generated.h M +1 -1 parser/python36.sdef https://commits.kde.org/kdev-python/d619a731dbcdc724630118d521583be41f0cf7d3 |
PEP 343 introduces the 'with' statement, and says this about the 'as' clause: "with EXPR as VAR: Here, 'with' and 'as' are new keywords; EXPR is an arbitrary expression (but not an expression-list) and VAR is a single assignment target. It can not be a comma-separated sequence of variables, but it can be a parenthesized comma-separated sequence of variables." This means the context manager's __enter__ method can return a sequence with multiple elements, and you can deconstruct the sequence by putting a "parenthesized comma-separated sequence of variables" in the 'as' clause: class Mgr: def __enter__(self): return ('two', 'elements') def __exit__(self, *args): pass with Mgr() as (foo, bar): print(foo) print(bar) However, kdev-python doesn't support this. 'foo' and 'bar' will both appear as undefined variables, underlined as errors.