python / python/mypy

What if duck types are unions of all the ducklings

Open
#11,516 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-2-low
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

If the duck typed types were actually unions of all the ducklings then it would fix a bunch of edge cases that arise.
And would make this feature more easily understood and discovered.

def func1(c: complex):
    reveal_type(c)  # float | int | complex

def func2(f: float):
    reveal_type(f)  # float | int

def func3(b: bytes):
    reveal_type(b)  # bytes | bytearray | memoryview

def func4(u: unicode):  # python 2 moment
    reveal_type(u)  # unicode | str

edge cases:

def func(f: float):
    f.is_integer()  # error: int has no member "is_integer"
def func(f: float):
    if not isinstance(f, float):
        print("hi")  # no 'unreachable code' error
        reveal_type(f)  # int | complex

It would also be useful to have type types for float and bytes, when you want exactly float and don't want any stinking ints.

Related #11511, #11145

Contributor guide

Open the contributing guide

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 examples in this issue and compare the proposed union semantics with related issues #11511 and #11145. Resolve how duck-typed types should behave, including exact float and bytes types and isinstance narrowing; done requires an agreed design and corresponding type-checker behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.