[Python] How can python type var and type aliases be used to simplify function definitions...

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    Say I have a definition:

    def foo[A: AMin, B: BMin, C: CMin](d: D[A, B, C], e: E[A, B]) -> F[C]: ...


    Is there a way (in python 3.13) to simplify (by type vars/aliases) to make this more condense, while keeping typing checkers like pyright happy?

    E.g. import the A, B, C as bound TypeVar from a common module. As well as as type definitions like type DType = ...

    I tried following with the simplicity (in foo/foo2 defs) I hoped to achieve:

    from typing import TypeVar

    class AMin: pass
    class BMin: pass
    class CMin: pass
    class D: pass
    class E: pass
    class F: pass

    A = TypeVar('A', bound=AMin)
    B = TypeVar('B', bound=BMin)
    C = TypeVar('C', bound=CMin)

    type DType = D[A, B, C]
    type EType = E[A, B]
    type FType = F[C]

    def foo(d: DType, e: EType) -> FType: ...

    def foo2(d: DType) -> C: ...


    But that still gives many typing errors.

    Continue reading...

Compartilhe esta Página