I am trying to override a function from a class that inherits from another class, where the parent class is calling on a global function.
I believe I would need to move the function within the class and override, but even if I do that I am not sure how I can override it with the existing code.
Here is my code:
@dataclasses.dataclass
class ConsolidatedDailyParsingDefinitionByMic(DailyProcessingParameters):
def DAG(self):
...
with DAG(dag_name, default_args=args, ...) as dag:
for task_group_config in self.task_group_list:
task_group_instance = DailyParsingTaskDefinition(**task_group_config)
task_group_instance.parsing_tasks() <---- trying to override a functio in here
def count_identities_stage(dag_def):
########## I am trying to get this function to be called!!#############
@dataclasses.dataclass
class DailyParsingTaskDefinition(DailyDownloadDefinition):
def parsing_tasks(self):
self.count_identities = count_identities_stage(self)
def count_identities_stage(dag_def):
....
return count_identities
What I am trying to do is to override count_identities_stage
, so that when it is called in the class ConsolidatedDailyParsingDefinitionByMic
, it calls on the count_identities_stage
function that is defined inside of the class, and not the global one.
If you’re in control of where def count_identities_stage(dag_def):
is defined, you could make it trampoline:
def count_identities_stage(dag_def):
if hasattr(dag_def, "count_identities_stage"):
return dag_def.count_identities_stage()
return count_identities
If you’re not in control, you could use e.g. unittest.mock.patch
to patch in a trampoline implementation like that in the namespace where DailyParsingTaskDefinition
imports count_identities_stage
.
1