[Python] python not enforcing generic type parameter constraints at runtime

Discussão em 'Python' iniciado por Stack, Janeiro 24, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying by all means get type errors by doing something like this:

    from typing import TypeVar

    class Faa:
    pass

    T = TypeVar("T")

    def foo(a: Type[T], b: T, c: T) -> None:
    pass

    foo(Faa, 1, "2") # this should not be allowed


    I'm trying to get an error using mypy or pyright but I just haven't been able to.

    Can anyone teach me how to enforce this kind of generic typing?

    Continue reading...

Compartilhe esta Página