MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _prepare_instrumentation

Function _prepare_instrumentation

lib/sqlalchemy/orm/collections.py:778–823  ·  view source on GitHub ↗

Prepare a callable for future use as a collection class factory. Given a collection class factory (either a type or no-arg callable), return another factory that will produce compatible instances when called. This function is responsible for converting collection_class=list int

(
    factory: Union[Type[Collection[Any]], _CollectionFactoryType],
)

Source from the content-addressed store, hash-verified

776
777
778def _prepare_instrumentation(
779 factory: Union[Type[Collection[Any]], _CollectionFactoryType],
780) -> _CollectionFactoryType:
781 """Prepare a callable for future use as a collection class factory.
782
783 Given a collection class factory (either a type or no-arg callable),
784 return another factory that will produce compatible instances when
785 called.
786
787 This function is responsible for converting collection_class=list
788 into the run-time behavior of collection_class=InstrumentedList.
789
790 """
791
792 impl_factory: _CollectionFactoryType
793
794 # Convert a builtin to 'Instrumented*'
795 if factory in __canned_instrumentation:
796 impl_factory = __canned_instrumentation[factory]
797 else:
798 impl_factory = cast(_CollectionFactoryType, factory)
799
800 cls: Union[_CollectionFactoryType, Type[Collection[Any]]]
801
802 # Create a specimen
803 cls = type(impl_factory())
804
805 # Did factory callable return a builtin?
806 if cls in __canned_instrumentation:
807 # if so, just convert.
808 # in previous major releases, this codepath wasn't working and was
809 # not covered by tests. prior to that it supplied a "wrapper"
810 # function that would return the class, though the rationale for this
811 # case is not known
812 impl_factory = __canned_instrumentation[cls]
813 cls = type(impl_factory())
814
815 # Instrument the class if needed.
816 if __instrumentation_mutex.acquire():
817 try:
818 if getattr(cls, "_sa_instrumented", None) != id(cls):
819 _instrument_class(cls)
820 finally:
821 __instrumentation_mutex.release()
822
823 return impl_factory
824
825
826def _instrument_class(cls):

Callers

nothing calls this directly

Calls 2

_instrument_classFunction · 0.85
castFunction · 0.50

Tested by

no test coverage detected