Pyright: spurious unbound error for complicated closure

Created on 5 Dec 2019  路  3Comments  路  Source: microsoft/pyright

Describe the bug

There looks to be a spurious unbound error when doing some stuff with a complicated closure. I'm not sure if this is a typeshed issue or what, since the error will disappear if the code changes too much.

To Reproduce

from typing import Callable


def perform_request(build_req: Callable[[], str]) -> str:
    return "purr"


def make_api_request(auth: str) -> str:
    return "meow"


def testfunc() -> None:
    resp = open("test")
    auth = resp.read()

    def build_req():
        # switching out this line makes the error go away
        # def build_req() -> str:
        return make_api_request(auth=auth)

    resp = perform_request(build_req)
pyright --project misc\pyright_test\pyright_test20
Loading configuration file at c:\p\misc\pyright_test\pyright_test20\pyrightconfig.json
typingsPath c:\p\misc\pyright_test\pyright_test20\typings is not a valid directory.
Searching for source files
Found 1 source file
c:\p\misc\pyright_test\pyright_test20\test.py
  38:38 - error: 'auth' is unbound
1 error, 0 warnings
Completed in 1.246sec

Expected behavior

This code is a little suspicious (assigning a different type to resp for instance), but it seems likely that the error printed is not the correct one.

VS Code extension or command-line

pyright --version
pyright 1.1.8
addressed in next version bug

All 3 comments

mypy calls out the missing annotation as well as the overwritten value of resp:

mypy --strict misc\pyright_test\pyright_test20\test.py 
misc\pyright_test\pyright_test20\test.py:36: error: Function is missing a type annotation
misc\pyright_test\pyright_test20\test.py:41: error: Incompatible types in assignment (expression has type "str", variable has type "TextIO")

Thanks for reporting the bug. This will be fixed in the next version. I've also added your sample as a unit test to prevent regressions in the future.

This is now implemented in version 1.1.9, which I just published.

Was this page helpful?
0 / 5 - 0 ratings