MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _configure_class_instrumentation

Method _configure_class_instrumentation

lib/sqlalchemy/orm/mapper.py:1405–1480  ·  view source on GitHub ↗

Associate this Mapper with the given class and entity name. Subsequent calls to ``class_mapper()`` for the ``class_`` / ``entity`` name combination will return this mapper. Also decorate the `__init__` method on the mapped class to include optional auto-sess

(self)

Source from the content-addressed store, hash-verified

1403 self._configure_polymorphic_setter(True)
1404
1405 def _configure_class_instrumentation(self):
1406 """Associate this Mapper with the
1407 given class and entity name.
1408
1409 Subsequent calls to ``class_mapper()`` for the ``class_`` / ``entity``
1410 name combination will return this mapper. Also decorate the
1411 `__init__` method on the mapped class to include optional
1412 auto-session attachment logic.
1413
1414 """
1415
1416 # we expect that declarative has applied the class manager
1417 # already and set up a registry. if this is None,
1418 # this raises as of 2.0.
1419 manager = attributes.opt_manager_of_class(self.class_)
1420
1421 if manager is None or not manager.registry:
1422 raise sa_exc.InvalidRequestError(
1423 "The _mapper() function and Mapper() constructor may not be "
1424 "invoked directly outside of a declarative registry."
1425 " Please use the sqlalchemy.orm.registry.map_imperatively() "
1426 "function for a classical mapping."
1427 )
1428
1429 self.dispatch.instrument_class(self, self.class_)
1430
1431 # this invokes the class_instrument event and sets up
1432 # the __init__ method. documented behavior is that this must
1433 # occur after the instrument_class event above.
1434 # yes two events with the same two words reversed and different APIs.
1435 # :(
1436
1437 manager = instrumentation.register_class(
1438 self.class_,
1439 mapper=self,
1440 expired_attribute_loader=util.partial(
1441 loading._load_scalar_attributes, self
1442 ),
1443 # finalize flag means instrument the __init__ method
1444 # and call the class_instrument event
1445 finalize=True,
1446 )
1447
1448 self.class_manager = manager
1449
1450 assert manager.registry is not None
1451 self.registry = manager.registry
1452
1453 # The remaining members can be added by any mapper,
1454 # e_name None or not.
1455 if manager.mapper is None:
1456 return
1457
1458 event.listen(manager, "init", _event_on_init, raw=True)
1459
1460 for key, method in util.iterate_attributes(self.class_):
1461 if key == "__init__" and hasattr(method, "_sa_original_init"):
1462 method = method._sa_original_init

Callers 1

__init__Method · 0.95

Calls 4

opt_manager_of_classMethod · 0.80
instrument_classMethod · 0.45
listenMethod · 0.45
unionMethod · 0.45

Tested by

no test coverage detected