Bug 399533 - No support for deconstructing tuple in 'with' statement
Summary: No support for deconstructing tuple in 'with' statement
Status: RESOLVED FIXED
Alias: None
Product: kdev-python
Classification: Developer tools
Component: Language support (show other bugs)
Version: 5.2.4
Platform: Debian testing Linux
: NOR normal
Target Milestone: ---
Assignee: Francis Herne
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-10-09 03:20 UTC by Nicolás Alvarez
Modified: 2018-10-10 00:02 UTC (History)
1 user (show)

See Also:
Latest Commit:
Version Fixed In:
Sentry Crash Report:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Nicolás Alvarez 2018-10-09 03:20:18 UTC
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.
Comment 1 Francis Herne 2018-10-09 13:19:57 UTC
Yes, it's missing.
Comment 2 Francis Herne 2018-10-09 14:30:45 UTC
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]`
Comment 3 Francis Herne 2018-10-10 00:02:35 UTC
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