Dynamically building ParamSpecs from callables

PEP 692: Using TypedDict for more precise **kwargs typing The best use case for this pep I think is exactly these kinds of situations where you want to pass kwargs to another function while preserving type checker’s ability to understand the code. Using your example it could be written like,

class CheeseshopArgs(TypedDict):
  red_leicester: bool
  tilsit: bool
  caerphilly: bool
  stilton: bool

class Cheeseshop:
    def __init__(
        self,
        # much as I want to, I won't list all of the cheeses...
        red_leicester: bool = False,
        tilsit: bool = False,
        caerphilly: bool = False,
        stilton: bool = False,
    ) -> None:
  ...

class FineCheeseshop(Cheeseshop):
  def __init__(self, limburger: bool = False, **kwargs: **CheeseshopArgs) -> None:

or if you’d like to avoid some duplication you could do,

class Cheeseshop:
    def __init__(
        self,
        **kwargs: **CheeseshopArgs
    ) -> None:
  ...

class FineCheeseshop(Cheeseshop):
  def __init__(self, limburger: bool = False, **kwargs: **CheeseshopArgs) -> None:
1 Like