You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "ebongzzang (via GitHub)" <gi...@apache.org> on 2026/01/15 04:14:32 UTC
[PR] Fix `TypeError: unhashable type: 'dict'` in Asset.__hash__() [airflow]
ebongzzang opened a new pull request, #60562:
URL: https://github.com/apache/airflow/pull/60562
## What change does this PR introduce?
- Fixes the method `airflow.sdk.definitions.asset.Asset.__hash__()` in task-sdk which was attempting to hash a dictionary directly, causing a `TypeError`.
- The current implementation attempts to hash a dictionary directly, which is not allowed in Python since dictionaries are mutable and unhashable. This prevents Asset objects from being used in sets or as dictionary keys, which is inconsistent with the `__eq__` implementation.
## Changes
- Modified `__hash__` to serialize the attrs dictionary to JSON string using `json.dumps(sort_keys=True)` before hashing.
- Applied the same pattern used in the codebase for hashing dictionaries:
- Similar to airflow/triggers/base.py:147 which uses json.dumps() to serialize kwargs before hashing.
- Similar to airflow/providers/standard/operators/python.py:834-836 which uses json.dumps(hash_dict, sort_keys=True) to create a hashable string.
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR, please
change below checkbox to `[X]` followed by the name of the tool, uncomment the "Generated-by".
-->
- [ ] Yes (please specify the tool below)
<!--
Generated-by: [Tool Name] following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
-->
---
* Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment: `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `TypeError: unhashable type: 'dict'` in Asset.__hash__() [airflow]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #60562:
URL: https://github.com/apache/airflow/pull/60562#issuecomment-3787272200
Awesome work, congrats on your first merged pull request! You are invited to check our [Issue Tracker](https://github.com/apache/airflow/issues) for additional contributions.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `TypeError: unhashable type: 'dict'` in Asset.__hash__() [airflow]
Posted by "boring-cyborg[bot] (via GitHub)" <gi...@apache.org>.
boring-cyborg[bot] commented on PR #60562:
URL: https://github.com/apache/airflow/pull/60562#issuecomment-3752833854
Congratulations on your first Pull Request and welcome to the Apache Airflow community! If you have any issues or are unsure about any anything please check our Contributors' Guide (https://github.com/apache/airflow/blob/main/contributing-docs/README.rst)
Here are some useful points:
- Pay attention to the quality of your code (ruff, mypy and type annotations). Our [prek-hooks]( https://github.com/apache/airflow/blob/main/contributing-docs/08_static_code_checks.rst#prerequisites-for-prek-hooks) will help you with that.
- In case of a new feature add useful documentation (in docstrings or in `docs/` directory). Adding a new operator? Check this short [guide](https://github.com/apache/airflow/blob/main/airflow-core/docs/howto/custom-operator.rst) Consider adding an example DAG that shows how users should use it.
- Consider using [Breeze environment](https://github.com/apache/airflow/blob/main/dev/breeze/doc/README.rst) for testing locally, it's a heavy docker but it ships with a working Airflow and a lot of integrations.
- Be patient and persistent. It might take some time to get a review or get the final approval from Committers.
- Please follow [ASF Code of Conduct](https://www.apache.org/foundation/policies/conduct) for all communication including (but not limited to) comments on Pull Requests, Mailing list and Slack.
- Be sure to read the [Airflow Coding style]( https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#coding-style-and-best-practices).
- Always keep your Pull Requests rebased, otherwise your build might fail due to changes not related to your commits.
Apache Airflow is a community-driven project and together we are making it better 🚀.
In case of doubts contact the developers at:
Mailing List: dev@airflow.apache.org
Slack: https://s.apache.org/airflow-slack
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `TypeError: unhashable type: 'dict'` in Asset.__hash__() [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #60562:
URL: https://github.com/apache/airflow/pull/60562#issuecomment-3785361863
> If this is an issue for the project, I can disable Copilot and resubmit the PR. Please let me know if you'd like me to take any action.
Yep. It would be great. Copilot reviews are mosty unhelpful and reviews are actually for humans to discuss things and to learn from each other, copilot reviews do not serve that purpose.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `TypeError: unhashable type: 'dict'` in Asset.__hash__() [airflow]
Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk merged PR #60562:
URL: https://github.com/apache/airflow/pull/60562
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `TypeError: unhashable type: 'dict'` in Asset.__hash__() [airflow]
Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #60562:
URL: https://github.com/apache/airflow/pull/60562#discussion_r2692951549
##########
task-sdk/src/airflow/sdk/definitions/asset/__init__.py:
##########
@@ -378,7 +379,7 @@ def __eq__(self, other: Any) -> bool:
def __hash__(self):
f = attrs.filters.include(*attrs.fields_dict(Asset))
- return hash(attrs.asdict(self, filter=f))
+ return hash(json.dumps(attrs.asdict(self, filter=f), sort_keys=True))
Review Comment:
The `attrs.asdict()` call will recursively convert all nested attrs objects (including `AssetWatcher` instances with their `trigger` field) to dictionaries. However, the `BaseEventTrigger` objects in the `watchers` field may not be JSON serializable by default, which could cause a `TypeError` when `json.dumps()` is called. Consider adding `recurse=False` to `attrs.asdict()` or implementing a custom value serializer that handles non-JSON-serializable objects, or document that Assets with watchers cannot be hashed.
```suggestion
data = attrs.asdict(self, filter=f)
try:
serialized = json.dumps(data, sort_keys=True)
except TypeError:
# Fallback for non-JSON-serializable values (e.g. BaseEventTrigger instances).
# This preserves hashing capability instead of raising at runtime.
serialized = repr(data)
return hash(serialized)
```
##########
task-sdk/tests/task_sdk/definitions/test_asset.py:
##########
@@ -192,6 +192,19 @@ def test_not_equal_when_different_uri():
assert asset1 != asset2
+def test_hash_for_same_uri():
+ asset1 = Asset(uri="s3://example/asset")
+ asset2 = Asset(uri="s3://example/asset")
+
+ assert hash(asset1) == hash(asset2)
Review Comment:
The test cases only verify hash behavior with basic Assets that have default values for `extra` and `watchers`. Add test cases that verify hash behavior when Assets have non-empty `extra` dictionaries and when Assets have identical/different `extra` values to ensure the JSON serialization handles these fields correctly.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Fix `TypeError: unhashable type: 'dict'` in Asset.__hash__() [airflow]
Posted by "ebongzzang (via GitHub)" <gi...@apache.org>.
ebongzzang commented on PR #60562:
URL: https://github.com/apache/airflow/pull/60562#issuecomment-3752862566
Apologies - the Copilot review comment was automatically generated by my GitHub account settings when I created this PR. This was unintentional.
If this is an issue for the project, I can disable Copilot and resubmit the PR. Please let me know if you'd like me to take any action.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org