Skip to content

Casting list of union type to list[Base] #2141

@praskr-wisdom

Description

@praskr-wisdom

Describe the Bug

Summary

Pyrefly reports a bad-return error when returning a list containing instances of subclasses, even when the return type is explicitly annotated as list[Base]. The inferred type list[A | B] should be assignable to list[Base] when both A and B inherit from Base.

Minimal Reproducible Example

from abc import ABC, abstractmethod
from typing import reveal_type

class Base(ABC):

    @abstractmethod
    def foo(self, x: int) -> None: ...


class A(Base):

    def foo(self, x: int) -> None:
        print(x)

class B(Base):

    def foo(self, x: int) -> None:
        pass

def return_object(name: str) -> list[Base]:
    return [A()] + [B()]


def return_object_work_for_non_list(name: str) -> Base:
    o = None
    if name == "a":
        o = A()
    else:
        o = B()
    reveal_type(o)
    return o

Actual Behavior

ERROR sandbox.py:21:12-23: Returned type `list[A | B]` is not assignable to declared return type `list[Base]` [bad-return]

Expected Behavior

No error. Since A and B are both subclasses of Base, the union A | B is a subtype of Base. While list is technically invariant, the list is created locally and immediately returned—there's no opportunity for type-unsafe mutation.

Mypy accepts this code without error.

Can we do something like if T is assignable to V then list[T] is assignable to list[V]?

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEq2AxnRDcbpQC50CCAQgGEANA2xwulVEy40YXABa5MAHXRqmUVHDh1%2B2mAAoBggJSI1autboABRhKky5i5VZuYYYOmFy5DcDBQYKL4iKzoXKZ0ALQAfHQAcrjoMOGEGZbq6JrauryG%2BoHmWTZ0nt6%2B-oHBoeEQkdHxSSlp7mXWxJQNXIb4plm5OnqFBiXZZRU%2BfgFBIXRhEVGxCcmpFugdNsR5WVOU8gCulOgA%2BqhQUKe42ABWMDJwhk0JUBASANpFMAC6G2UXK43e6POgAXjoHwKph%2BdAA1JD%2BM8fu06AcuMdNoDrncHlw4GoQMIQIcuNA4CRyIgqLQxCw2BxuHwhKIHJJpLJ5EpVOgwNR6FxSMQGgBzVjsTg8A4ANxgF1OguIMEGWmG32MQnGqPs4nZzi5bk2Hi802qczqSxeLXWdAyhCyGlV%2BVGxQ2qKmVVmtQW9UaK2tbSNWy6PT6A2yQ10SO%2BWqD5RNnpq80WPSta0DW06u2y%2ByOJxxIN66FQcnCjitb0%2B3z%2BqPRmMh0NhCK%2ByIdWBNdfzwLxpwA7pwANanXyUU7oFKnStFktpOjl-3ff42XDggOoiDeYtycEQlQgVB7pdbFcQ6GooKBI8dE8jcNlGVyq6Koy4O82TubXCE4lkA5gKCkIQXC0FAFAAMR0AACqQf4AXOGA4AQdBMCkkAiscqBkik9roBBADKMAwHQChcFwxBwIgAD0lG-l4AGEJwIqUTA6CUZguBMHAlEobyEDoVIWGsdMlAMNKqDQIwsDIahfEYYJdC4MQgkUmoZCuOgMSypQcAQCkq57gAzIQACMABMe6EgAvsSHIQLKABi0AwBQpLkpSICWUAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions