python / python/mypy

Need plugin hooks for functions/methods

Open
#6,760 8 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature priority-1-normal topic-plugins topic-usability
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

For some frameworks like pytest it would be useful to have a plugin that works with functions.
Currently mypy has decorator hook for classes.

Some ideas for possible hooks:

  1. update function namespace to inject some names
  2. modify function signature
  3. generate function signature (types)

For example pytest has the following fixture

from pathlib import Path

@pytest.fixture
def db_path(tmp_path: Path) -> Path:
    return tmp_path / 'test.db'

The fixture is used by name:

def test_a(db_path) -> None:
    ....

db_path argument here is a calucated the result of db_path() fixture call.
It is bound by parameter name which is the same as the fixture name.

Adding a type to db_path argument is tedious, especially if a fixture returns some complex type like Callable[[str, int], Awaitable[web.Response]].

Technically all needed information can be extracted from fixture definition if the definition is type annotated.

A plugin for pytest would be awesome but as @ilevkivskyi said mypy needs a new hook to support it.

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 mypy's existing decorator hook for classes and the plugin API concepts described in the issue. Determine how function namespace injection, signature modification, and generated signatures should work for pytest-style fixtures, then define tests that show fixture parameters receive the inferred return type.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.