python / python/mypy

Add reference implementation of PEP 718

Open
#21,337 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature topic-calls topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

I think this is very much needed PEP, and using mypy for reference implementation is a good idea because mypy actually already (mostly) supports this under the hood.

The only non-trivial question is whether we should allow type applications only to references that are statically known to refer to a function, or do we want to support type application to an arbitrary expression that happen to have a callable-like type inferred? For example:

T = TypeVar("T")

class Callbacks(TypedDict):
    regular: Callable[[int], int]
    generic: Callable[[T], T]

def test(cb: Callbacks) -> None:
    cb["generic"][int]  # <- should we support this?

If we only want to support static references, then implementation is trivial, essentially:

  • Make this error conditional on Python version <3.15 and file being not a stub.
  • Update this helper to accept a Decorator.

However, if we want to support arbitrary expressions, it should be quite easy as well. Luckily, all the necessary infrastructure was added for TypeForm PEP support. We can use TypeCheckerAsSemanticAnalyzer adapter to re-interpret arbitrary expression as type during type checking.

cc @JukkaL @Gobot1234 @PabloRuizCuevas

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 reading the linked checks in mypy/checkexpr.py around lines 4948-4949 and the helper in mypy/semanal.py around line 8235, then compare them with PEP 718 and the existing TypeForm infrastructure. The work is done when the project has an agreed reference implementation and its supported type-application cases and Python-version behavior are covered by tests.

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
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.