MCPcopy
hub / github.com/pydantic/pydantic / import_string

Function import_string

pydantic/v1/utils.py:122–138  ·  view source on GitHub ↗

Stolen approximately from django. Import a dotted module path and return the attribute/class designated by the last name in the path. Raise ImportError if the import fails.

(dotted_path: str)

Source from the content-addressed store, hash-verified

120
121
122def import_string(dotted_path: str) -> Any:
123 """
124 Stolen approximately from django. Import a dotted module path and return the attribute/class designated by the
125 last name in the path. Raise ImportError if the import fails.
126 """
127 from importlib import import_module
128
129 try:
130 module_path, class_name = dotted_path.strip(' ').rsplit('.', 1)
131 except ValueError as e:
132 raise ImportError(f'"{dotted_path}" doesn\'t look like a module path') from e
133
134 module = import_module(module_path)
135 try:
136 return getattr(module, class_name)
137 except AttributeError as e:
138 raise ImportError(f'Module "{module_path}" does not define a "{class_name}" attribute') from e
139
140
141def truncate(v: Union[str], *, max_len: int = 80) -> str:

Callers 2

validateMethod · 0.90
wrapperFunction · 0.50

Calls

no outgoing calls

Tested by

no test coverage detected