79365896

Date: 2025-01-17 19:23:17
Score: 1
Natty:
Report link

There is an alternative answer to @Eddie Bergman that recives a dict instead of named parameters. Using .create with a dict still keeps auto completion.

from typing import TypedDict

class MyDict(TypedDict):
    a: str
    b: int
    c: list[float]

    @staticmethod
    def default_values() -> 'MyDict':
        return {
            "a": "",
            "b": 0,
            "c": [],
        }

    @staticmethod
    def create(values: 'MyDict') -> 'MyDict':
        instance = MyDict.default_values()
        instance.update(values)
        return instance

my_variable = MyDict.create({"a": "Hello World"})
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @Eddie
  • Low reputation (1):
Posted by: VictorG-028