Inference on Literal Types

It definitely is. Lets look at it:

b: B = C()
a = [b]
reveal_type(a)  # mypy list[B]  pyright list[C]

The developer intent here is creating an object that will be treated as B and must be consistent with B. This line is fine.

The next line is creating a list with that as it’s only element. mypy keeps the developer intent, and this is fine so long as it continues only being treated as list[B]

pyright ignores the developer intent and changes it to list[C], this surprises the user, and makes it incompatible with their own annotation.

to hammer it home:

def make_b() -> B:
    return C()

b: B = make_b()
a = [b]
reveal_type(a)  # mypy list[B]  pyright list[B]

You can’t argue this is unsafe to allow the developer intent to be what is prioritized for inference here, this is a valid statement of developer intent that is within the type system.

3 Likes