Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@puddly
Copy link
Collaborator

@puddly puddly commented Aug 11, 2025

Seen here: zigpy/zha-device-handlers#4251

This function makes a pretty bizarre assumption:

In [1]: from frozendict import deepfreeze

In [2]: deepfreeze?
Signature: deepfreeze(o, custom_converters=None, custom_inverse_converters=None)
Docstring:
Converts the object and all the objects nested in it in its
immutable counterparts.

The conversion map is in getFreezeConversionMap().

You can register a new conversion using `register()` You can also
pass a map of custom converters with `custom_converters` and a map
of custom inverse converters with `custom_inverse_converters`,
without using `register()`.

By default, if the type is not registered and has a `__dict__`
attribute, it's converted to the `frozendict` of that `__dict__`.                <------------------

This function assumes that hashable == immutable (that is not
always true).

This function uses recursion, with all the limits of recursions in
Python.

Where is a good old tail call when you need it?

This fails for basically every primitive type subclass:

In [3]: class MyInt(int):
   ...:     pass
   ...:

In [4]: deepfreeze(MyInt())
Out[4]: frozendict.frozendict({})

Copilot AI review requested due to automatic review settings August 11, 2025 17:55
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR removes the problematic use of frozendict.deepfreeze which incorrectly converts primitive type subclasses to empty frozendicts by assuming that hashable equals immutable. The change replaces deepfreeze with a custom converter that properly handles nested dictionary structures while avoiding the flawed conversion behavior.

  • Removes frozendict.deepfreeze import and usage
  • Replaces deepfreeze converter with a custom lambda that correctly handles nested dictionaries
  • Updates test values to use proper cluster ID references instead of hardcoded integers

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
zigpy/quirks/v2/init.py Removes deepfreeze import and replaces its usage with a custom converter for nested frozendict creation
tests/test_quirks_v2.py Updates test automation triggers to use cluster ID constants and adds assertion to verify correct cluster ID value

] = attrs.field(factory=frozendict, converter=deepfreeze)
] = attrs.field(
factory=frozendict,
converter=lambda d: frozendict({k: frozendict(v) for k, v in d.items()}),
Copy link

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lambda converter assumes d is always a dictionary with the items() method. If d is not a dictionary or is None, this will raise an AttributeError. Consider adding type checking or using a proper function with error handling.

Suggested change
converter=lambda d: frozendict({k: frozendict(v) for k, v in d.items()}),
converter=_convert_device_automation_triggers_metadata,

Copilot uses AI. Check for mistakes.
@codecov
Copy link

codecov bot commented Aug 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.29%. Comparing base (a16895b) to head (add637a).
⚠️ Report is 1 commits behind head on dev.

Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #1646   +/-   ##
=======================================
  Coverage   99.29%   99.29%           
=======================================
  Files          57       57           
  Lines       11638    11638           
=======================================
  Hits        11556    11556           
  Misses         82       82           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@puddly puddly merged commit 8c3f80a into zigpy:dev Aug 12, 2025
14 checks passed
] = attrs.field(factory=frozendict, converter=deepfreeze)
] = attrs.field(
factory=frozendict,
converter=lambda d: frozendict({k: frozendict(v) for k, v in d.items()}),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change already causes the TypeError: unhashable type: 'dict' in the one quirks test @puddly. I've only been looking at all the translation placeholder changes this whole time and was wondering why it didn't work 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants