python / python/typing

Overlapping type

Open
#2,154 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: feature
Dominant language
Python
Stars
1.8k
Forks
302
Avg merge
23h
Merged PRs (30d)
8

Description

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) finds 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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the overlapping-type examples in the issue and review the recent discussion they reference. Define the intended semantics for an Overlapping type, including unions, literals, and non-overlapping types; done means the behavior and typing rules are agreed upon.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.