Skip to content

Commit

Permalink
Streamline & simplify __eq__ methods in models Dag and BaseOperator (#…
Browse files Browse the repository at this point in the history
…13449)

- Use getattr() instead of __dict__ as __dict__ doesn't return
  correct values for properties.
- Avoid unnecessary condition checks (the removed condition checks are covered by _comps)

(cherry picked from commit 6ef23af)
  • Loading branch information
XD-DENG authored and kaxil committed Jan 21, 2021
1 parent b90adbe commit 278e3b1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
6 changes: 4 additions & 2 deletions airflow/models/baseoperator.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,10 @@ def __init__(
)

def __eq__(self, other):
if type(self) is type(other) and self.task_id == other.task_id:
return all(self.__dict__.get(c, None) == other.__dict__.get(c, None) for c in self._comps)
if type(self) is type(other):
# Use getattr() instead of __dict__ as __dict__ doesn't return
# correct values for properties.
return all(getattr(self, c, None) == getattr(other, c, None) for c in self._comps)
return False

def __ne__(self, other):
Expand Down
3 changes: 1 addition & 2 deletions airflow/models/dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ def __repr__(self):
return f"<DAG: {self.dag_id}>"

def __eq__(self, other):
if type(self) == type(other) and self.dag_id == other.dag_id:

if type(self) == type(other):
# Use getattr() instead of __dict__ as __dict__ doesn't return
# correct values for properties.
return all(getattr(self, c, None) == getattr(other, c, None) for c in self._comps)
Expand Down

0 comments on commit 278e3b1

Please sign in to comment.