Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Reference to decorated function above its definition fails #613

Closed
JukkaL opened this issue Mar 21, 2015 · 2 comments
Closed

Reference to decorated function above its definition fails #613

JukkaL opened this issue Mar 21, 2015 · 2 comments
Labels
bug mypy got something wrong

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 21, 2015

Mypy complains about code like this:

from contextlib import contextmanager

def foo() -> None:
    with bar():  # Error: Cannot determine type of 'bar'
        print('here')

@contextmanager
def bar():
    print('before')
    yield
    print('after')

The reason is that when type checking foo, we haven't run type inference for bar yet. More general multi-pass type inference (#481) would fix this (and other related issues), but that's somewhat tricky to implement. A quick fix would be to support just a few cases such as the above, at least for common kinds of decorators.

@JukkaL JukkaL added bug mypy got something wrong priority labels Mar 21, 2015
@JukkaL JukkaL closed this as completed in d30137c Nov 26, 2015
@gvanrossum gvanrossum reopened this Nov 26, 2015
@gvanrossum
Copy link
Member

It seems fixed for PY3 only -- the example script is now clean in PY3 mode but still gives the same error with --py2:

/Users/guido/mypy_tests/mypy_ctx.py: note: In function "foo":
/Users/guido/mypy_tests/mypy_ctx.py:4: error: Cannot determine type of 'bar'

@gvanrossum
Copy link
Member

Sorry... that was because I had a different contextlib stub for PY2 but not for PY3. If I roll back my changes to contextlib.pyi this passes both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants