Skip to content

Commit 6bfd19d

Browse files
committed
Make internalizing anonymous dicts support IMapping, not just its subclass IDict.
1 parent ab8e405 commit 6bfd19d

5 files changed

Lines changed: 15 additions & 11 deletions

File tree

CHANGES.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
3.2.1 (unreleased)
77
==================
88

9-
- Nothing changed yet.
9+
- Make internalizing anonymous dictionary objects
10+
support ``IMapping`` fields, rather than only its subclass
11+
``IDict``.
1012

1113

1214
3.2.0 (2026-05-19)

src/nti/externalization/_datastructures.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ cdef make_repr
3232
cdef isSyntheticKey
3333
cdef find_most_derived_interface
3434
cdef NotGiven
35-
cdef IDict_providedBy
35+
cdef IMapping_providedBy
3636
cdef IObject_providedBy
3737
cdef _anonymous_dict_factory
3838

src/nti/externalization/datastructures.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from zope import interface
2020
from zope import schema
2121
from zope.component import getUtility
22-
from zope.schema.interfaces import IDict
22+
from zope.schema.interfaces import IMapping
2323
from zope.schema.interfaces import IObject
2424
from zope.schema.interfaces import SchemaNotProvided
2525

@@ -49,7 +49,7 @@
4949
StandardExternalFields = get_standard_external_fields()
5050
StandardInternalFields = get_standard_internal_fields()
5151
DEFAULT_EXTERNALIZATION_POLICY = get_default_externalization_policy()
52-
IDict_providedBy = IDict.providedBy
52+
IMapping_providedBy = IMapping.providedBy
5353
IObject_providedBy = IObject.providedBy
5454

5555
__all__ = [
@@ -694,13 +694,16 @@ def find_factory_for_named_value(self, key, value):
694694
remove this limitation is to subclass this object.
695695
696696
If no registered factory is found, and the schema field is
697-
a `zope.schema.Dict` with a value type of `zope.schema.Object`,
697+
a `zope.schema.Mapping` with a value type of `zope.schema.Object`,
698698
then we return a factory which will update the object in place.
699699
700700
.. versionchanged:: 1.0a6
701701
Only return an anonymous factory for ``IDict`` fields when
702702
it wants objects for the value.
703703
704+
.. versionchanged:: NEXT
705+
Use ``IMapping`` instead of ``IDict`` to be more general.
706+
704707
"""
705708
factory = AbstractDynamicObjectIO.find_factory_for_named_value(self, key, value)
706709
if factory is None: # pylint:disable=too-many-nested-blocks
@@ -727,12 +730,12 @@ def find_factory_for_named_value(self, key, value):
727730

728731
if (
729732
factory is None
730-
and IDict_providedBy(field) # pylint:disable=no-value-for-parameter
733+
and IMapping_providedBy(field) # pylint:disable=no-value-for-parameter
731734
and isinstance(value, dict)
732735
and IObject_providedBy(field.value_type) # pylint:disable=no-value-for-parameter
733736
):
734-
# If is no factory found, check to see if the
735-
# schema field is a Dict with a complex value type, and if
737+
# If there is no factory found, check to see if the
738+
# schema field is a Mapping with a complex value type, and if
736739
# so, automatically update it in place. The alternative
737740
# requires the user to use a ZCML directive for each such
738741
# dict field.

src/nti/externalization/tests/test_datastructures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,14 +549,14 @@ class O(object):
549549
assert_that(factory, is_(none()))
550550

551551
def test_factory_for_dict_with_object_value(self):
552-
from zope.schema import Dict
552+
from zope.schema import Mapping
553553
from zope.schema import Object
554554

555555
class I2(interface.Interface):
556556
pass
557557

558558
class I(interface.Interface):
559-
field = Dict(
559+
field = Mapping(
560560
title='A blank field',
561561
value_type=Object(I2)
562562
)

src/nti/externalization/zcml.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ def _ap_find_package_name(_cls):
244244
# The plus side is that now that we are using component_zcml.utility()
245245
# to register legacy class factories too, there's not much harm in
246246
# initing the class early.
247-
248247
legacy_factories = cls_iio.__class_init__()
249248

250249
# Now that it's initted, register the factories

0 commit comments

Comments
 (0)