Pyright: Possibly unbound when using `typing.TYPE_CHECKING`

Created on 12 Nov 2019  路  7Comments  路  Source: microsoft/pyright

Describe the bug
All refererence to types that imported under typing.TYPE_CHECKING block will cause possibly unbound problem.

To Reproduce

import typing
if typing.TYPE_CHECKING:
    import collections

def foo(a: 'collections.OrderedDict'):
    pass

Expected behavior
No problem reported

Screenshots or Code
image

VS Code extension or command-line
VS Code extension: 1.0.84

Additional context

addressed in next version bug

Most helpful comment

Ah, my bad. My fix works if you use TYPE_CHECKING, but not if you use typing.TYPE_CHECKING. I'll expand my fix so it works with both. In the meantime, you can make it work by changing your code to the following:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    import collections

def foo(a: 'collections.OrderedDict'):
    pass

All 7 comments

Thanks for the bug report. This will be fixed in the next version.

This is now fixed in 1.1.0, which I just published.

I am still occuring this problem on 1.1.0 and 1.1.3 .
@erictraut ?
image

Ah, my bad. My fix works if you use TYPE_CHECKING, but not if you use typing.TYPE_CHECKING. I'll expand my fix so it works with both. In the meantime, you can make it work by changing your code to the following:

from typing import TYPE_CHECKING
if TYPE_CHECKING:
    import collections

def foo(a: 'collections.OrderedDict'):
    pass

This is now fixed in 1.1.4, which I just published.

Howdy! Just wanted to add that the fixes don't work if you've aliased the typing import to something else. We typically use t, e.g. import typing as t, because we find it cleaner than either importing a ton of symbols from typing or having to type typing.List or whatever everywhere.

Currently working around this by importing TYPE_CHECKING on its own, in addition to the import of typing

Thanks!

Thanks for pointing that out. It will be addressed in the next release.

Was this page helpful?
0 / 5 - 0 ratings