@@ -103,6 +103,7 @@ class DeferredFieldInvalidation:
103103 changed_type : ParameterOwner
104104 field_name : str
105105 invalidate_saved : bool = False
106+ include_origin : bool = True
106107
107108
108109class ObjectStateRegistry :
@@ -228,6 +229,7 @@ def flush_deferred_invalidations(cls) -> None:
228229 changed_type = request .changed_type ,
229230 field_name = request .field_name ,
230231 invalidate_saved = request .invalidate_saved ,
232+ include_origin = request .include_origin ,
231233 )
232234 finally :
233235 cls ._deferred_invalidation_depth = previous_depth
@@ -634,7 +636,8 @@ def invalidate_by_type_and_scope(
634636 scope_id : Optional [str ],
635637 changed_type : ParameterOwner ,
636638 field_name : str ,
637- invalidate_saved : bool = False
639+ invalidate_saved : bool = False ,
640+ include_origin : bool = True ,
638641 ) -> None :
639642 """Invalidate a specific field in states that could inherit from changed_type at scope_id.
640643
@@ -653,6 +656,8 @@ def invalidate_by_type_and_scope(
653656 changed_type: The type of the ObjectState that was modified
654657 field_name: The specific field that changed
655658 invalidate_saved: If True, also invalidate saved_resolved cache for descendants
659+ include_origin: Whether to invalidate the state at ``scope_id`` as
660+ well as its descendants.
656661 """
657662 changed_scope = cls ._normalize_scope_id (scope_id )
658663 if cls ._deferred_invalidation_depth > 0 :
@@ -662,6 +667,7 @@ def invalidate_by_type_and_scope(
662667 changed_type = changed_type ,
663668 field_name = field_name ,
664669 invalidate_saved = invalidate_saved ,
670+ include_origin = include_origin ,
665671 )
666672 )
667673 return
@@ -671,6 +677,7 @@ def invalidate_by_type_and_scope(
671677 changed_type = changed_type ,
672678 field_name = field_name ,
673679 invalidate_saved = invalidate_saved ,
680+ include_origin = include_origin ,
674681 )
675682
676683 @classmethod
@@ -680,6 +687,7 @@ def _invalidate_by_type_and_scope_now(
680687 changed_type : ParameterOwner ,
681688 field_name : str ,
682689 invalidate_saved : bool = False ,
690+ include_origin : bool = True ,
683691 ) -> None :
684692 """Immediate implementation for scope/type/field-aware invalidation."""
685693 from objectstate .lazy_factory import get_base_type_for_lazy
@@ -703,6 +711,9 @@ def _invalidate_by_type_and_scope_now(
703711
704712 for state in cls ._states .values ():
705713 state_scope = cls ._normalize_scope_id (state .scope_id )
714+ is_self = state_scope == changed_scope
715+ if is_self and not include_origin :
716+ continue
706717
707718 # SCOPE CHECK: must be at or below changed scope
708719 # Global scope (empty string) affects ALL states
@@ -713,7 +724,6 @@ def _invalidate_by_type_and_scope_now(
713724 logger .debug (f"[SCOPE] Global change affects state scope={ state_scope !r} " )
714725 else :
715726 # Non-global: check exact match or descendant
716- is_self = (state_scope == changed_scope )
717727 prefix = changed_scope + "::"
718728 is_descendant = state_scope .startswith (prefix )
719729 if not (is_self or is_descendant ):
0 commit comments