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=[""])