Skip to content

Instantly share code, notes, and snippets.

@JakuWorks
Created February 7, 2024 16:25
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/4f1479950d8ba366c535b61cd73cc738 to your computer and use it in GitHub Desktop.
Save JakuWorks/4f1479950d8ba366c535b61cd73cc738 to your computer and use it in GitHub Desktop.
TypeVarsAttempt-#1.py
from dataclasses import dataclass
from typing import TypeVar
T = TypeVar('T', bound=B) # "B" is not defined
class A:
def __init__(self, a1): ...
@dataclass
class BBuilder:
foo: int
def build(self) -> T: # TypeVar "T" appears only once in generic function signature  Use "Unknown" instead
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)
# also tried writing `T = TypeVar('T', bound=B)` here in attempt 3
return B # Expression of type "type[B]" cannot be assigned to return type "T@build" Type "type[B]" cannot be assigned to type "T@build"
my_b_builder: BBuilder = BBuilder(foo=456)
BBuilt: T = my_b_builder.build() # Type variable "T" has no meaning in this context
my_b = BBuilt(a2='foo') # my_b: WhatTypeHintDoIPutHere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment