Skip to content

Method-bound type-parameter can "leak" through a Protocol #10976

@jorenham

Description

@jorenham

I encountered this while working on python/typeshed#14786:

from typing import Any, Protocol, final

@final
class Box[T]:
    _value: T

    def __init__(self, value: T, /) -> None:
        self._value = value

    def __replace__[VT](self, value: VT) -> Box[VT]:
        return Box(value)

class CanReplace[RT](Protocol):
    def __replace__(self, /, *args: Any, **kwargs: Any) -> RT: ...

def replace[RT](b: CanReplace[RT], /, **kwargs: Any) -> RT:
    return b.__replace__(**kwargs)

reveal_type(replace(Box(42), value="spam"))  # Box[VT@__replace__]

Change Box.__replace__ to (self, *, value: VT) and it (correctly) reveals Box[Any].

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions