Skip to content

[Bug Report] Generic Type Parameter Incorrectly Inferred as NoneType with Lambda Callable #712

@MetalOxideSemi

Description

@MetalOxideSemi

Environment

  • Codon version: v0.19.3
  • OS: Debian Bullseye

Description

Codon fails to correctly infer generic type parameters when a class uses CallableTrait with a lambda function. The type parameter is incorrectly inferred as NoneType instead of the actual return type of the callable, causing a compilation error.

Reproduction Code

from collections import Counter

class Item:
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z

class Count:
    Key: type

    def __init__(self, project: CallableTrait[[Item], Key]):
        self.project = project
        self.counter = Counter[Key]()

    def visit(self, item: Item):
        self.counter[self.project(item)] += 1

items = [Item(1, 2, 3), Item(1, 2, 4), Item(1, 2, 5)]
count = Count(lambda item: item.z)  # Lambda returns int
for item in items:
    count.visit(item)
print(count)

Compilation fails with the following error:

crash_on_filter.py:15 (9-13): error: 'Count[NoneType]' object has no attribute 'project'
╰─ crash_on_filter.py:23 (9-35): error: during the realization of __init__(self: Count[NoneType], project: %_lambda_95(...))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions