Skip to content

Self type is not expanded correctly when accessed on a type variable with union bound #19584

@jorgefandinno

Description

@jorgefandinno
from typing import Self, TypeVar, reveal_type

class A:
    def f(self,) -> Self:
        return self

class B:
    def f(self,) -> Self:
        return self

T = TypeVar('T', bound=A | B)

def g(arg: T) -> T:
    result = arg.f()
    reveal_type(result)
    return result

produces

narrow_self.py:15: note: Revealed type is "narrow_self.A | narrow_self.B"
narrow_self.py:16: error: Incompatible return value type (got "A | B", expected "T")  [return-value]

This should be safe because arg.f() returns Self which is T in this case.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions