Skip to content

Generics type checked but fail at runtime #8366

@gricha

Description

@gricha

Hey - I have run into an issue where mypy says the code is correct, but then in runtime, if you try to import this code (and not even use it) it will fail with an error.

I'm using mypy 0.761, python 3.8.0 and mypy is in strict mode (can provide exact config if necessary).

Here is the minimal code that replicates it:

from typing import Callable, TypeVar, Generic

T = TypeVar("T")
U = TypeVar("U")
V = TypeVar("V")

Flatmap = Callable[[T], "Foo[V, U]"]


class Foo(Generic[T, U]):
    def flatmap(self, mapper: Flatmap[T, U, V]) -> "Foo[V, U]":
        ...

and here is the error that will show up if you try to import a file that has that code:

raise TypeError(f"Too {'many' if alen > elen else 'few'} parameters for {cls};"
E   TypeError: Too many parameters for typing.Callable[[~T], ForwardRef('Foo[V, U]')]; actual 3, expected 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions