Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

225
Views
Union of generic types that is also generic

Say I have two types (one of them generic) like this

from typing import Generic, TypeVar
T = TypeVar('T')
class A(Generic[T]): pass
class B: pass

And a union of A and B like this

C = A|B

Or, in pre-Python-3.10/PEP 604-syntax:

C = Union[A,B]

How do I have to change the definition of C, so that C is also generic? e.g. if an object is of type C[int], it is either

  • of type A[int] (type parameter is passed down) or
  • of type B (type parameter is ignored)
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Rereading the mypy documentation I believe I have found my answer:

Type aliases can be generic. In this case they can be used in two ways: Subscripted aliases are equivalent to original types with substituted type variables, so the number of type arguments must match the number of free type variables in the generic type alias. Unsubscripted aliases are treated as original types with free variables replaced with Any

So, to answer my question:

C = A[T]|B

should do the trick. And it does!

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!