MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / add_class

Function add_class

lib/sqlalchemy/orm/clsregistry.py:64–115  ·  view source on GitHub ↗

Add a class to the _decl_class_registry associated with the given declarative class.

(
    classname: str, cls: Type[_T], decl_class_registry: _ClsRegistryType
)

Source from the content-addressed store, hash-verified

62
63
64def add_class(
65 classname: str, cls: Type[_T], decl_class_registry: _ClsRegistryType
66) -> None:
67 """Add a class to the _decl_class_registry associated with the
68 given declarative class.
69
70 """
71 if classname in decl_class_registry:
72 # class already exists.
73 existing = decl_class_registry[classname]
74 if not isinstance(existing, _MultipleClassMarker):
75 decl_class_registry[classname] = _MultipleClassMarker(
76 [cls, cast("Type[Any]", existing)]
77 )
78 else:
79 decl_class_registry[classname] = cls
80
81 try:
82 root_module = cast(
83 _ModuleMarker, decl_class_registry["_sa_module_registry"]
84 )
85 except KeyError:
86 decl_class_registry["_sa_module_registry"] = root_module = (
87 _ModuleMarker("_sa_module_registry", None)
88 )
89
90 tokens = cls.__module__.split(".")
91
92 # build up a tree like this:
93 # modulename: myapp.snacks.nuts
94 #
95 # myapp->snack->nuts->(classes)
96 # snack->nuts->(classes)
97 # nuts->(classes)
98 #
99 # this allows partial token paths to be used.
100 while tokens:
101 token = tokens.pop(0)
102 module = root_module.get_module(token)
103 for token in tokens:
104 module = module.get_module(token)
105
106 try:
107 module.add_class(classname, cls)
108 except AttributeError as ae:
109 if not isinstance(module, _ModuleMarker):
110 raise exc.InvalidRequestError(
111 f'name "{classname}" matches both a '
112 "class name and a module name"
113 ) from ae
114 else:
115 raise
116
117
118def remove_class(

Callers

nothing calls this directly

Calls 6

castFunction · 0.85
_ModuleMarkerClass · 0.85
get_moduleMethod · 0.80
add_classMethod · 0.80
popMethod · 0.45

Tested by

no test coverage detected