-
-
Save JakuWorks/4f1479950d8ba366c535b61cd73cc738 to your computer and use it in GitHub Desktop.
TypeVarsAttempt-#1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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