Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Spurious error on bare yield in a context manager under strict-optional #1958

Closed
gnprice opened this issue Jul 29, 2016 · 4 comments
Closed

Comments

@gnprice
Copy link
Collaborator

gnprice commented Jul 29, 2016

Running mypy --strict-optional on the mypy codebase itself, we get this error:

mypy/build.py: note: In member "wrap_context" of class "State":
mypy/build.py:1170: error: Yield value expected

The function in question looks like this:

    @contextlib.contextmanager
    def wrap_context(self) -> Iterator[None]:
        save_import_context = self.manager.errors.import_context()
        self.manager.errors.set_import_context(self.import_context)
        try:
            yield
        except CompileError:
            raise
        except Exception as err:
            report_internal_error(err, self.path, 0)
        self.manager.errors.set_import_context(save_import_context)
        self.check_blockers()

This is pretty sensible idiomatic code. Arguably there might be some other appropriate annotation to write for the return type; bracketing that question, it looks to be perfectly well-typed code that shouldn't produce an error. It's also not immediately clear to me how to work around it, except I guess with a # type: ignore.

I suspect this is related to the complicated treatment we give in some places to None as a return type -- cf #1509, #299, #359 -- and that treating None in a more regular way as discussed in #1278 and #1847 would fix it.

It may also be related to #1933, but I haven't yet read through that discussion to understand what's going on there.

@ddfisher
Copy link
Collaborator

ddfisher commented Aug 4, 2016

This no longer appears to be a problem. I think this was fixed by #1980.

@ddfisher ddfisher closed this as completed Aug 4, 2016
@ambv
Copy link
Contributor

ambv commented Jun 14, 2017

This is fixed for the function syntax, e.g. this doesn't return an error:

@contextmanager
def noop_ctxmgr():
    yield

However, this still does:

noop_ctxmgr = contextmanager(lambda: (yield))

Error:

f.py:11: error: Cannot infer type argument 1 of "contextmanager"
f.py:11: error: Yield value expected

@gvanrossum
Copy link
Member

Can you open a new issue?

@ambv
Copy link
Contributor

ambv commented Jun 14, 2017

Will do.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

4 participants