Skip to content

Bidirectional inference assigning symbols in other modules #10539

@Andrej730

Description

@Andrej730

pyright 1.1.401

Consider the snippet below - it seems bidirectional inference doesn't work when assigning symbols from other module.

In theory in the example below xxx.callback = lambda x: x.bit_count() should work out similar to callback = lambda x: x.bit_count() - by bydirectional inference, the type of lambda x: x.bit_count() is recognized as type of xxx.callback (Callable[[int], int]), x type to be recognized as int automatically and xxx.callback should keep it's original type after the assignment.

Same for xxx.lst = [].

xxx.py

from typing import Callable

callback: Callable[[int], int] = lambda x: x.bit_count()
lst: list[int] = []

Main script:

from typing import Callable
import xxx

class A:
    a: Callable[[int], int]

    @staticmethod
    def test(a: Callable[[int], int]): ...


callback: Callable[[int], int] = lambda x: x.bit_count()

# OK
A.test(lambda x: x.bit_count())
# OK
A.a = lambda x: x.bit_count()
# OK
callback = lambda x: x.bit_count()

# Bydirectional inference doesn't work.
# Type of "xxx.callback" is "(int) -> int"
reveal_type(xxx.callback)
xxx.callback = lambda x: x.bit_count()
# Type of "xxx.callback" is "(x: Unknown) -> Unknown"
reveal_type(xxx.callback)

# Type of "xxx.lst" is "list[int]"
reveal_type(xxx.lst)
xxx.lst = []
# Type of "xxx.lst" is "list[Unknown]"
reveal_type(xxx.lst)

Update: confirm issue as resolved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    addressed in next versionIssue is fixed and will appear in next published versionbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions