microsoft / pyright

Static Type Checker for Python
Other
13.18k stars 1.41k forks source link

Type check for lambda fails with NamedTuple #9110

Open Zaikaba opened 3 hours ago

Zaikaba commented 3 hours ago

Environment data

Code Snippet

from collections.abc import Callable
from typing import NamedTuple

class Bar1:

    def __init__( self, val: bool, fun: Callable[ [ bool ], bool ] ):
        self.val = val
        self.fun = fun

# fine: "x: bool"
bar1 = Bar1( True, lambda x: x )

class Bar2( NamedTuple ):

    val: bool
    fun: Callable[ [ bool ], bool ]

# Type of parameter "x" is unknown
bar = Bar2( True, lambda x: x )

error

Expected behavior

Type check should derive (bool)->bool in both cases.

Actual behavior

NamedTuple version fails, x is unknown.

Logs

erictraut commented 3 hours ago

Could someone from the pylance team please transfer this to the pyright project since this is a core type checker issue? Thanks!