Pyright: unable to forward reference a type within Callable assigned to an alias

Created on 30 Apr 2020  路  3Comments  路  Source: microsoft/pyright

Describe the bug
I cannot forward reference a type inside a Callable and assign it to an alias.

To Reproduce

  1. create a file a.py with the following content:
class A():
    pass
  1. create another file b.py which imports A and tries a Callable forward reference on A.
from typing import Callable
from a import A

# Foo = List["A"]                       - works
Foo = Callable[["A"], None]

# def bar(a: Callable[["A"], None]):    - works
#     a(A())

def func(a: Foo):
    a(A())                              - error

produces:

Argument of type "A" cannot be assigned to parameter "p0" of type "Literal['A']" "A" cannot be assigned to "Literal['A']"Pyright (reportGeneralTypeIssues)

Expected behavior
I would expect that A can also passed by forward reference into the Callable and not recognized as Literal.

VS Code extension or command-line
_Are you running pyright as a VS Code extension or a command-line tool?_
VS Code extension
_Which version?_
1.1.34

addressed in next version bug

All 3 comments

Thanks for the bug report. I was able to repro the problem, and it's an easy fix. The type checker needs to interpret string literals differently in different contexts. In the case of the parameter clause of a Callable, it wasn't correctly interpreting string literals as forward reference types.

This will be fixed in the next release of Pyright.

This is now addressed in version 1.1.35, which I just published.

Thank you for the fast fix!

Was this page helpful?
0 / 5 - 0 ratings