Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer forward conversion if registered #95

Merged
merged 5 commits into from
Jan 24, 2024

Conversation

eischaefer
Copy link
Contributor

When a forward conversion is registered to getFreezeConversionMap(), that explicit conversion is now preferred over the (implicit) inverse of a (backward) conversion registered to getFreezeConversionInverseMap(). This behavior is consistent with the preexisting preference for explicit forward conversion of certain simple types (_freeze_types_plain) and non-iterables and strings (i.e., excluded by isIterableNotString()).

The modified behavior can be seen in the example below:

import frozendict
import types

dic = {"a": "A"}
prox = types.MappingProxyType(dic)

frozendict.register(types.MappingProxyType, frozendict.frozendict)

frozen = frozendict.deepfreeze(prox)
isinstance(frozen, frozendict.frozendict)  # Now True, was False
isinstance(frozen, types.MappingProxyType)  # Was True, now False

As a practical use case, I'm currently working on a project in which I'd like to use deepfreeze() to ensure that a nested mappingproxy is hashable.

eischaefer and others added 3 commits January 20, 2024 19:58
When a forward conversion is registered to getFreezeConversionMap(),
that explicit conversion is now preferred over the (implicit) inverse of
a (backward) conversion registered to getFreezeConversionInverseMap().
This behavior is consistent with the preexisting preference for explicit
forward conversion of certain simple types(_freeze_types_plain) and non-
iterables and strings (i.e., excluded by isIterableNotString()).
@Marco-Sulla
Copy link
Owner

Uh, a subtle bug, good.
Can I ask you the favor to add a little unit test for this case under test/test_freeze.py?

@Marco-Sulla Marco-Sulla merged commit 956fbe0 into Marco-Sulla:master Jan 24, 2024
7 checks passed
@Marco-Sulla
Copy link
Owner

I thought you add a test with MappingProxy, instead you preferred a more robust solution.

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