Skip to content

Instantly share code, notes, and snippets.

@JakuWorks
Last active February 7, 2024 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JakuWorks/bca04ea6f88cae2c93d52f356409cdf8 to your computer and use it in GitHub Desktop.
Save JakuWorks/bca04ea6f88cae2c93d52f356409cdf8 to your computer and use it in GitHub Desktop.
TypeVars Attempt #2
from typing import TypeVar
from typing import Tuple
from dataclasses import dataclass
class A:
def __init__(self, a1): ...
@dataclass
class BBuilder:
foo: int
def build(self) -> tuple:
T = TypeVar('T', bound='B')
a1_i_dont_want_to_be_changeable = 123
class B(A):
def __init__(self, a2):
super().__init__(a1=a1_i_dont_want_to_be_changeable)
return (T, B)
my_b_builder = BBuilder(foo=456)
BBuilt: Tuple[TypeVar, ...] = my_b_builder.build()
T2 = TypeVar('T2', bound=BBuilt[0]) # Expected type expression but received "tuple[Unknown, ...]"
BBuiltForReal: T2 = BBuilt[1] # Type variable "T2" has no meaning in this context
my_b: T2 = BBuilt(a2='foo') # Type variable "T2" has no meaning in this context # Object of type "tuple[Unknown, ...]" is not callable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment