Skip to content

Instantly share code, notes, and snippets.

@JakuWorks
Last active February 7, 2024 17: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/f7be66ac2d2f6128a2bbc5fa0bbf4d13 to your computer and use it in GitHub Desktop.
Save JakuWorks/f7be66ac2d2f6128a2bbc5fa0bbf4d13 to your computer and use it in GitHub Desktop.
This is not on the topic of the stack overflow question. **This is on the topic of my specific problem.**
# Check comments to see mypy/pylance complaints
# Failed proof-of-concept code
from typing import Type
from dataclasses import dataclass
from dataclasses import field
class A:
def __init__(self, args: 'BBuilderArgs'):
self.a1: int = args.a1
self.a2: str = args.a2
@dataclass
class BBuilderArgs:
a1: int
a2: str = field(init=False, default="this is some parameter I don't want to change while initializing")
class BBuilder:
def __init__(self, args: BBuilderArgs):
class B(A):
def __init__(self):
super().__init__(args=args)
def method_that_A_does_not_have(self) -> None: ...
self.B: Type[B] = B
def new_b_instance(self) -> B: # "B" is not defined <----------
return self.B()
my_b_args: BBuilderArgs = BBuilderArgs(
a1 = 123
)
my_b_builder: BBuilder = BBuilder(args=my_b_args)
my_b: my_b_builder.B = my_b_builder.new_b_instance() # Variable not allowed in type expression <----------
# I guess making a Class Builder (this is probably not a real term) in a way that satisfies mypy and pylance is way harder than I thought
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment