Create a function that helps retrieve objects from their new locations. The goal of this function is to help users transition from deprecated imports to new imports. The function will raise deprecation warning on loops using deprecated_lookups or fallback_module. Module lookup
(
package: str,
*,
module_lookup: Optional[Dict[str, str]] = None,
deprecated_lookups: Optional[Dict[str, str]] = None,
fallback_module: Optional[str] = None,
)
| 13 | |
| 14 | |
| 15 | def create_importer( |
| 16 | package: str, |
| 17 | *, |
| 18 | module_lookup: Optional[Dict[str, str]] = None, |
| 19 | deprecated_lookups: Optional[Dict[str, str]] = None, |
| 20 | fallback_module: Optional[str] = None, |
| 21 | ) -> Callable[[str], Any]: |
| 22 | class="st">"""Create a function that helps retrieve objects from their new locations. |
| 23 | |
| 24 | The goal of this function is to help users transition from deprecated |
| 25 | imports to new imports. |
| 26 | |
| 27 | The function will raise deprecation warning on loops using |
| 28 | deprecated_lookups or fallback_module. |
| 29 | |
| 30 | Module lookups will import without deprecation warnings (used to speed |
| 31 | up imports from large namespaces like llms or chat models). |
| 32 | |
| 33 | This function should ideally only be used with deprecated imports not with |
| 34 | existing imports that are valid, as in addition to raising deprecation warnings |
| 35 | the dynamic imports can create other issues for developers (e.g., |
| 36 | loss of type information, IDE support for going to definition etc). |
| 37 | |
| 38 | Args: |
| 39 | package: current package. Use __package__ |
| 40 | module_lookup: maps name of object to the module where it is defined. |
| 41 | e.g., |
| 42 | { |
| 43 | class="st">"MyDocumentLoader": ( |
| 44 | class="st">"langchain_community.document_loaders.my_document_loader" |
| 45 | ) |
| 46 | } |
| 47 | deprecated_lookups: same as module look up, but will raise |
| 48 | deprecation warnings. |
| 49 | fallback_module: module to import from if the object is not found in |
| 50 | module_lookup or if module_lookup is not provided. |
| 51 | |
| 52 | Returns: |
| 53 | A function that imports objects from the specified modules. |
| 54 | class="st">""" |
| 55 | all_module_lookup = {**(deprecated_lookups or {}), **(module_lookup or {})} |
| 56 | |
| 57 | def import_by_name(name: str) -> Any: |
| 58 | class="st">""class="st">"Import stores from langchain_community."class="st">"" |
| 59 | class="cm"># If not in interactive env, raise warning. |
| 60 | if all_module_lookup and name in all_module_lookup: |
| 61 | new_module = all_module_lookup[name] |
| 62 | if new_module.split(class="st">".")[0] not in ALLOWED_TOP_LEVEL_PKGS: |
| 63 | raise AssertionError( |
| 64 | fclass="st">"Importing from {new_module} is not allowed. " |
| 65 | fclass="st">"Allowed top-level packages are: {ALLOWED_TOP_LEVEL_PKGS}" |
| 66 | ) |
| 67 | |
| 68 | try: |
| 69 | module = importlib.import_module(new_module) |
| 70 | except ModuleNotFoundError as e: |
| 71 | if new_module.startswith(class="st">"langchain_community"): |
| 72 | raise ModuleNotFoundError( |
no outgoing calls