From c89159a994643bccc31259e6b520e0b3511fbc2a Mon Sep 17 00:00:00 2001 From: Bob Gregory Date: Sat, 19 Oct 2024 22:50:42 +0100 Subject: [PATCH] Add some more comments --- punq/__init__.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/punq/__init__.py b/punq/__init__.py index 5ccaae5..8257f2b 100644 --- a/punq/__init__.py +++ b/punq/__init__.py @@ -162,15 +162,21 @@ def _match_defaults(spec): inspect.getfullargspec returns a complex object that includes the defaults on args and kwonly args. This function takes a list of args, and a tuple of the last N defaults and returns a dict of args to defaults. + + These defaults are passed to _resolve_impl when building a needed dependency + and used when a registration is missing. """ ns = {} if spec.defaults is not None: + # Defaults for args are just a tuple. We match args with their defaults + # by position, starting at the first defaulted arg offset = len(spec.args) - len(spec.defaults) defaults = ([None] * offset) + list(spec.defaults) ns = {key: value for key, value in zip(spec.args, defaults) if value is not None} if spec.kwonlydefaults is not None: + # defaults for kwargs are in a dict, so we just update the result dict. ns.update(spec.kwonlydefaults) return ns