From 9141fc06ffeef721e72fa83c51ca39a4377d231b Mon Sep 17 00:00:00 2001 From: r-spiewak <63987228+r-spiewak@users.noreply.github.com> Date: Wed, 18 Jun 2025 16:43:31 -0400 Subject: [PATCH 1/2] Dedupe init calls for non-super-respecting chains --- .../utils/split_args_for_inits.py | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/python_template/utils/split_args_for_inits.py b/src/python_template/utils/split_args_for_inits.py index 629de79..e5db115 100644 --- a/src/python_template/utils/split_args_for_inits.py +++ b/src/python_template/utils/split_args_for_inits.py @@ -360,7 +360,6 @@ def call_init_chain_respecting_super( # pylint: disable=too-many-locals,too-com """Call init methods of super-respecting classes via `super()` chain, and manually call those that don't use `super()`. """ - called = set() # Step 1: Identify super-respecting classes: # (crude guess: look at if "super" is in source) @@ -379,15 +378,23 @@ def call_init_chain_respecting_super( # pylint: disable=too-many-locals,too-com # except Exception: # pass - # Step 2: Call init for non-super-respecting classes: - for base in cls.__mro__: - if base in (object, stop_at, skip_class) or base in called: - continue - if base not in super_respecting and base in split: - args = split[base].get("args", []) - kwargs = split[base].get("kwargs", {}) - base.__init__(self, *args, **kwargs) # type: ignore[misc] # pylint: disable=unnecessary-dunder-call - called.add(base) + # Maybe skip this next step, since the classes will be called again + # as part of the super-respecting chains (as individual single-link + # chains)? + # # Step 2: Call init for non-super-respecting classes: + # for base in cls.__mro__: + # if base in (object, stop_at, skip_class) or base in called: + # continue + # if base not in super_respecting and base in split: + # args = split[base].get("args", []) + # kwargs = split[base].get("kwargs", {}) + # if DEBUG_PRINTS: + # print( + # "[call_init_chain_respecting_super] Calling init of " + # f"non-super-respecting class {base.__name__}" + # ) + # base.__init__(self, *args, **kwargs) # type: ignore[misc] # pylint: disable=unnecessary-dunder-call + # called.add(base) # Step 3: Collect args and kwargs for super-respecting chain: # first_bases = [] @@ -421,6 +428,11 @@ def call_init_chain_respecting_super( # pylint: disable=too-many-locals,too-com for base in first_bases: if base in (skip_class,): continue + if DEBUG_PRINTS: + print( + "[call_init_chain_respecting_super] Calling init of " + f"super-respecting class {base.__name__}" + ) # super(base, self).__init__(*super_args[base], **super_kwargs[base]) base.__init__( # type: ignore[misc] # pylint: disable=unnecessary-dunder-call self, *super_args[base], **super_kwargs[base] From cc5c710c5c32e87e3e11b303ad56f7fe31d23c5a Mon Sep 17 00:00:00 2001 From: r-spiewak <63987228+r-spiewak@users.noreply.github.com> Date: Wed, 18 Jun 2025 16:45:54 -0400 Subject: [PATCH 2/2] Add comment on called set line --- src/python_template/utils/split_args_for_inits.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/python_template/utils/split_args_for_inits.py b/src/python_template/utils/split_args_for_inits.py index e5db115..8efe7da 100644 --- a/src/python_template/utils/split_args_for_inits.py +++ b/src/python_template/utils/split_args_for_inits.py @@ -360,6 +360,7 @@ def call_init_chain_respecting_super( # pylint: disable=too-many-locals,too-com """Call init methods of super-respecting classes via `super()` chain, and manually call those that don't use `super()`. """ + # called = set() # Step 1: Identify super-respecting classes: # (crude guess: look at if "super" is in source)