最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can python type var and type aliases be used to simplify function definitions with multiple generics - Stack Overflow

programmeradmin2浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论