Skip to content

Need plugin hooks for functions/methods #6760

Description

@asvetlov

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.

Activity

  1. ilevkivskyi commented on May 3, 2019

    @ilevkivskyi
    Member

    I like this idea, this will allow supporting some common dynamic features.

    This will likely require local lookup functions to the public API (currently it is there on SemanticAnalyzer but not on SemanticAnalyzerPluginInterface).

  2. blueyed commented on Jan 10, 2020

    @blueyed
    Contributor

    JFI/bump: I wonder if this is only about having get_function_signature_hook (in line with get_method_signature_hook) - but from a quick test it seems like get_method_signature_hook also only gets called for method calls, not definitions.
    I've also looked into this because of pytest.

  3. JukkaL commented on Jan 14, 2020

    @JukkaL
    Collaborator

    Brainstorming:

    • We could have a hook that gets called after semantic analysis and before type checking that can adjust the signature of each function/method.
    • We could have a signature adjustment hook that gets called before type checking the body of a function. However, this seems more complicated -- for consistency, we also need another hook that gets called for function calls. The benefit here would be that the hook could access inferred types of other functions and attributes.
    • We could have a hook that gets called for each module after semantic analysis, which would allow arbitrary changes to module namespaces. However, the implementation would have to be pretty tightly coupled with the mypy symbol table and AST implementation, so this might not the best option. Also, this might not work well with mypy daemon.
  4. MrGreenTea commented on May 15, 2020

    @MrGreenTea

    Brainstorming:

    * We could have a hook that gets called after semantic analysis and before type checking that can adjust the signature of each function/method.
    

    I was quite suprised this is not already the case, as I understand it exists for classes / methods with the get_method_signature_hook already.

    What's the reasoning behind that decision? What would be needed to add such a hook?

  5. sobolevn commented on Jun 30, 2020

    @sobolevn
    Member

    We could have a signature adjustment hook that gets called before type checking the body of a function. However, this seems more complicated -- for consistency, we also need another hook that gets called for function calls. The benefit here would be that the hook could access inferred types of other functions and attributes.

    This looks like exactly what I am looking for here: https://gitter.im/python/typing?at=5efa750ee0e5673398e1aa81

    TLDR:

    @kinded
    def map_functor(
        f: Kind[K, T],
        function: Callable[[T], V],
    ) -> Kind[K, V]:
        return f.map(function)

    This code currently does not typecheck, because it is not seem to be possible to tell what Kind is in this context.

  6. sobolevn commented on Mar 18, 2021

    @sobolevn
    Member

    Related #9925

  7. i386x commented on Jun 29, 2026

    @i386x

    JFI/bump: I wonder if this is only about having get_function_signature_hook (in line with get_method_signature_hook) - but from a quick test it seems like get_method_signature_hook also only gets called for method calls, not definitions. I've also looked into this because of pytest.

    Related to this, I found that get_function_signature_hook is not called in this case (when a function is used as a decorator):

    import functools
    
    @functools.cache
    def compute(x: int, y: int) -> int:
        return (x*x + y*y)/(x+y)

    but it is called in this case (when a function is used as a function):

    import functools
    
    def _compute(x: int, y: int) -> int:
        return (x*x + y*y)/(x+y)
    
    compute = functools.cache(_compute)

    Is this the expected behavior? IMHO get_function_signature_hook should be called also when a function is used as a decorator since decorators are syntactical sugar. Should this be considered a bug?

  8. ilevkivskyi commented on Jun 29, 2026

    @ilevkivskyi
    Member

    Should this be considered a bug?

    Yes, I think this is a bug (but to be clear fixing it will not solve the original issue).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions