Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

with-as should defer type to __enter__ #340

Open
jakebailey opened this issue Nov 2, 2018 · 5 comments
Open

with-as should defer type to __enter__ #340

jakebailey opened this issue Nov 2, 2018 · 5 comments

Comments

@jakebailey
Copy link
Member

jakebailey commented Nov 2, 2018

Take this example:

@contextlib.contextmanager
def noop(x):
    yield x

with noop("foobar") as s:
    print(type(s))  # prints <class 'str'>

The type of s is str, because noop yields x, which is str.

Another example (this one will raise AttributeError, but that's not important):

with contextlib.closing("foobar") as s:
    print(type(s))  # also prints <class 'str'>

In both examples, we say that s is of type ContextManager, which isn't the case. It would be better to determine the type of what comes after as via checking the context manager's __enter__ function return type: https://docs.python.org/3/reference/datamodel.html#object.__enter__

@brettcannon
Copy link
Member

Is this blocking any with ... as ... block working?

with open('file.txt') as file:
    file.

@jakebailey
Copy link
Member Author

Not from what I can tell:

image

But my original example with the contextmanager decorator doesn't work (probably because we don't handle how the decorator operates for some reason).

@jakebailey
Copy link
Member Author

jakebailey commented Jul 11, 2019

Okay, maybe open isn't the best example, because we could be "cheating" and copy from the left to the right without looking at __enter__, which would work for anything open returns, but the code should have been written and working: https://github.com/microsoft/python-language-server/blob/master/src/Analysis/Ast/Impl/Analyzer/Handlers/WithHandler.cs

@brettcannon
Copy link
Member

Ah, stops working with this full snippet:

import contextlib
import typing

@contextlib.contextmanager
def cxt_manager() -> typing.Generator[str, None, None]:
    yield "hello"


# with cxt_manager() as ob:
#     ob

with open('a') as file:
    file.

@MikhailArkhipov
Copy link

B/c we don't specialize Generator in typing. Typing specializations are not complete, #535.

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

No branches or pull requests

3 participants