Skip to content

Overlapping type #2154

@srittau

Description

@srittau

This is an rough idea from the recent discussion of whether to use object/Any or _KT for arguments of certain methods like pop or __sub__. E.g.:

class MyPoppable[KT, VT]:
    def pop(self, x: X) -> VT: ...

What should X be? Using KT (or KT | None) find type errors like the following:

poppable: MyPoppable[str]
poppable.pop(123)

But has a problem with overlapping types:

def foo(x: int | str):
    poppable.pop(x)

On the other hand, using object doesn't catch type errors like poppable.pop(123).

The solution would be able to specify that any partially overlapping type would be accepted:

class MyPoppable[KT, VT]:
    def pop(self, x: Overlapping[KT]) -> VT: ...

MyPoppable[str]().pop() would accept str, str | None, Literal["x"], str | int, etc., but not e.g. plain int.

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: featureDiscussions about new features for Python's type annotations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions