Pyright: Dataclass unbound in type annotation

Created on 4 Oct 2019  路  2Comments  路  Source: microsoft/pyright

Describe the bug
I'm using type annotations in a dataclass. While other annotations work as expected, pyright is raising a problem when an annotation is used with the same type as the dataclass

To Reproduce
See screenshot

Expected behavior
No problem should be raised.

Screenshots or Code
Screenshot 2019-10-04 at 08 53 37

VS Code extension or command-line
I am running pyright 1.0.69 as a VS Code extension

as designed

Most helpful comment

That's the correct behavior. If you try to execute this code in the python interpreter, it will generate an exception because the TimeWindow class is considered a forward declaration in this context. It's not yet defined when these annotations are encountered.

The correct workaround is to enclose the forward reference in quotes, as indicated in PEP 484. The python interpreter will then treat the annotation as a string literal value and won't generate an exception. The type checker is smart enough to parse the contents of the string literal.

All 2 comments

That's the correct behavior. If you try to execute this code in the python interpreter, it will generate an exception because the TimeWindow class is considered a forward declaration in this context. It's not yet defined when these annotations are encountered.

The correct workaround is to enclose the forward reference in quotes, as indicated in PEP 484. The python interpreter will then treat the annotation as a string literal value and won't generate an exception. The type checker is smart enough to parse the contents of the string literal.

Thanks Eric, I wasn't aware of that. I put the forward reference in quotes and now I don't see any warning so I assume it works.

Was this page helpful?
0 / 5 - 0 ratings