Skip to content

False positive bad-specialization when class with AnyStr | Generic[AnyStr] parameter is passed as Callable[[AnyStr], T] #1959

@adamtheturtle

Description

@adamtheturtle

When a class constructor accepts AnyStr | SomeGeneric[AnyStr] and is passed to a function expecting Callable[[AnyStr], Result], pyrefly incorrectly reports that SomeGeneric[AnyStr] violates the AnyStr bound.

mypy and pyright accept this code.

from collections.abc import Callable, Iterable
from typing import AnyStr


class Box[T]:
    pass


class Result:
    def __init__(self, x: AnyStr | Box[AnyStr]) -> None:
        pass


def f(factory: str | Callable[[AnyStr], Result], lines: Iterable[AnyStr]) -> None:
    pass


f(factory=Result, lines=[""])

pyrefly 0.46.1 gives:

ERROR `Box[@2490] | @2490` is not assignable to upper bound `bytes | str` of type variable `AnyStr` [bad-specialization]
  --> example.py:31:2
   |
31 | f(factory=Result, lines=[""])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions