Bug 399534

Summary: Wrong type for value returned by context manager
Product: [Developer tools] kdev-python Reporter: Nicolás Alvarez <nalvarez>
Component: Language supportAssignee: Sven Brauch <mail>
Status: RESOLVED FIXED    
Severity: normal CC: mail
Priority: NOR    
Version First Reported In: 5.2.4   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed/Implemented In: 5.3.0
Sentry Crash Report:

Description Nicolás Alvarez 2018-10-09 03:51:50 UTC
In the Python 'with' statement, an 'as' clause can be used to store the value returned by the context manager:

class Mgr:
    def __enter__(self):
        return "text"
    def __exit__(self, *args):
        pass

with Mgr() as asd:
    print(asd)

However, kdev-python incorrectly marks 'asd' as having type 'Mgr', when the correct type here should be 'str'. This happens to work for the common pattern of "with open(...) as f:" because file objects return themselves in __enter__, but other context managers don't.
Comment 1 Francis Herne 2018-10-10 00:02:36 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 399533

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