python / python/mypy

Function signature hook returning a union

Open
#15,457 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Currently the function/method signature hooks must return a CallableType. This precludes cases where a union of CallableTypes would be more appropriate.

For example, given

@attrs.define
class C1:
  foo: int

@attrs.define
class C2:
  bar: int

c: C1 | C2

then the signature of attrs.evolve for c could be returned by a function signature hook to be
(c: C1, foo: int) -> C1 | (c: C2, bar: int) -> C2.

Today #14849 performs an ad-hoc "meet" operation which results in a single callable type, which could instead be left to the existing functionality that "meet"s a union's types.

p.s. that being said, the current behavior for a union of callables is not friendly, e.g.
in this playground, the given union of callables can accept a bytes arg, but if you give them the mutually incompatible int or str, it simply says that it must be the other one, instead of saying "it's compatible with item 1 of the union but incompatible with item 2", etc.

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 by reviewing the ad-hoc meet operation from #14849 and the existing union-of-callables behavior, including the linked mypy-play example. The work is complete when function signature hooks can return a union of callable types and the resulting signatures retain the alternatives described in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.