Skip to content

type checking in lambdas stops working when using Iterable instead of list #20163

Closed

Description

@Tinche

Say we have a function like this:

from collections.abc import Callable, Iterable

def f[T](a: Callable[[T], Iterable[list[T]]]) -> None: ...

This generates no errors:

from typing import assert_never

f(lambda a: [assert_never(True)])

It would appear there's no checking in the lambda body.

However if I change the Iterable to list:

def f[T](a: Callable[[T], list[list[T]]]) -> None: ...

Then it works, and an error is shown as expected.

Activity

  1. A5rocks commented on Nov 2, 2025

    @A5rocks
    Collaborator

    For some reason this fails:

    from collections.abc import Callable, Iterable
    
    def f[T](a: Callable[[T], Iterable[list[T]]]) -> None: ...
    
    from typing import assert_never
    
    f(lambda a: reveal_type([assert_never(True)]))

    with:

    main.py:7: note: Revealed type is "builtins.list[builtins.list[T`469]]"
    main.py:7: error: Argument 1 to "assert_never" has incompatible type "bool"; expected "Never"  [arg-type]
    Found 1 error in 1 file (checked 1 source file)
    

    Maybe there's something that's hiding the error messages (maybe we eagerly evaluate for type context and don't want possibly wrong output?) but then we're forgetting to actually show the error messages later?

  2. added a commit that references this issue on Nov 3, 2025
    08a0b07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-contextType context / bidirectional inference

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions