|
12 | 12 |
|
13 | 13 |
|
14 | 14 | @contextmanager |
15 | | -def try_import(module_name: str, *symbol_names: str, extra_name: str | list[str]) -> Iterator[None]: |
| 15 | +def try_import( |
| 16 | + module_name: str, |
| 17 | + *symbol_names: str, |
| 18 | + extra_name: str | list[str] | None = None, |
| 19 | + package_name: str = 'crawlee', |
| 20 | +) -> Iterator[None]: |
16 | 21 | """Context manager to attempt importing symbols into a module. |
17 | 22 |
|
18 | 23 | If an `ImportError` is raised during the import, the symbols are replaced with `FailedImport` objects. When the |
19 | | - error is a `ModuleNotFoundError`, the message also names the optional extra (or one of several) that installs the |
20 | | - missing dependency. Other import errors, including those raised by a nested guard, keep their message as is. |
| 24 | + error is a `ModuleNotFoundError` and `extra_name` is given, the message also names the optional extra (or one of |
| 25 | + several) that installs the missing dependency. Other import errors, including those raised by a nested guard, |
| 26 | + keep their message as is. |
| 27 | +
|
| 28 | + Args: |
| 29 | + module_name: The name of the module the symbols are imported into. |
| 30 | + symbol_names: The names of the symbols being imported. |
| 31 | + extra_name: The optional extra (or extras) providing the dependency. When omitted, the error message is |
| 32 | + left as it is. |
| 33 | + package_name: The distribution whose extras are named in the message. Defaults to this package, and exists |
| 34 | + so that downstream packages reusing this helper can point users at their own extras. |
21 | 35 | """ |
22 | 36 | try: |
23 | 37 | yield |
24 | 38 | except ImportError as e: |
25 | | - message = e.args[0] |
26 | | - if isinstance(e, ModuleNotFoundError): |
27 | | - message = f'{message}. {_get_install_hint(extra_name)}' |
| 39 | + message = str(e) |
| 40 | + if isinstance(e, ModuleNotFoundError) and extra_name: |
| 41 | + message = f'{message}. {_get_install_hint(extra_name, package_name)}' |
28 | 42 | for symbol_name in symbol_names: |
29 | 43 | setattr(sys.modules[module_name], symbol_name, FailedImport(message)) |
30 | 44 |
|
31 | 45 |
|
32 | | -def _get_install_hint(extra_name: str | list[str]) -> str: |
| 46 | +def _get_install_hint(extra_name: str | list[str], package_name: str) -> str: |
33 | 47 | """Build the sentence telling the user which extra installs the missing optional dependency.""" |
34 | | - if isinstance(extra_name, str): |
35 | | - return f"Install the optional '{extra_name}' extra to use it: pip install 'crawlee[{extra_name}]'" |
36 | | - extras = ', '.join(f"'{name}'" for name in extra_name) |
37 | | - return f"Install one of the optional extras {extras} to use it, e.g. pip install 'crawlee[{extra_name[0]}]'" |
| 48 | + extras = [extra_name] if isinstance(extra_name, str) else list(extra_name) |
| 49 | + if len(extras) == 1: |
| 50 | + return f"Install the optional '{extras[0]}' extra to use it: pip install '{package_name}[{extras[0]}]'" |
| 51 | + names = ', '.join(f"'{name}'" for name in extras) |
| 52 | + return f"Install one of the optional extras {names} to use it, e.g. pip install '{package_name}[{extras[0]}]'" |
38 | 53 |
|
39 | 54 |
|
40 | 55 | def install_import_hook(module_name: str) -> None: |
|
0 commit comments