|
1 | 1 | # ruff: noqa: F401 |
| 2 | +"""Lazy re-exports from optimizer submodules. |
2 | 3 |
|
3 | | -from sqlglot.optimizer.optimizer import RULES as RULES, optimize as optimize |
4 | | -from sqlglot.optimizer.scope import ( |
5 | | - Scope as Scope, |
6 | | - build_scope as build_scope, |
7 | | - find_all_in_scope as find_all_in_scope, |
8 | | - find_in_scope as find_in_scope, |
9 | | - traverse_scope as traverse_scope, |
10 | | - walk_in_scope as walk_in_scope, |
11 | | -) |
| 4 | +Eager re-exports here trip a circular import under sqlglot[c]: importing |
| 5 | +sqlglot loads compiled `expressions.builders`, which eagerly wires up its |
| 6 | +links to compiled optimizer modules, which causes Python to run this |
| 7 | +file. The eager `from sqlglot.optimizer.optimizer import ...` then asks |
| 8 | +for `sqlglot.Schema`, but `sqlglot/__init__.py` hasn't bound it yet. |
| 9 | +
|
| 10 | +PEP 562 `__getattr__` defers the import to first attribute access, by |
| 11 | +which point sqlglot is fully loaded. Tracked upstream in python/mypy#21299. |
| 12 | +""" |
| 13 | + |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import threading |
| 17 | +import typing as t |
| 18 | + |
| 19 | +# Serialise concurrent first-access lookups; importlib's per-module lock is |
| 20 | +# released before our caching write to globals(), so two threads racing on |
| 21 | +# the same name could otherwise both run the import + getattr. |
| 22 | +_import_lock = threading.RLock() |
| 23 | + |
| 24 | +# Only for type checkers and IDEs; runtime resolution goes through __getattr__. |
| 25 | +if t.TYPE_CHECKING: |
| 26 | + from sqlglot.optimizer.optimizer import RULES as RULES, optimize as optimize |
| 27 | + from sqlglot.optimizer.scope import ( |
| 28 | + Scope as Scope, |
| 29 | + build_scope as build_scope, |
| 30 | + find_all_in_scope as find_all_in_scope, |
| 31 | + find_in_scope as find_in_scope, |
| 32 | + traverse_scope as traverse_scope, |
| 33 | + walk_in_scope as walk_in_scope, |
| 34 | + ) |
| 35 | + |
| 36 | +# Explicit, because some names collide between optimizer.py and submodules |
| 37 | +# (e.g. `qualify` is both a function and a submodule). |
| 38 | +_LAZY_ATTRS: dict[str, tuple[str, str]] = { |
| 39 | + "RULES": ("sqlglot.optimizer.optimizer", "RULES"), |
| 40 | + "optimize": ("sqlglot.optimizer.optimizer", "optimize"), |
| 41 | + "Scope": ("sqlglot.optimizer.scope", "Scope"), |
| 42 | + "build_scope": ("sqlglot.optimizer.scope", "build_scope"), |
| 43 | + "find_all_in_scope": ("sqlglot.optimizer.scope", "find_all_in_scope"), |
| 44 | + "find_in_scope": ("sqlglot.optimizer.scope", "find_in_scope"), |
| 45 | + "traverse_scope": ("sqlglot.optimizer.scope", "traverse_scope"), |
| 46 | + "walk_in_scope": ("sqlglot.optimizer.scope", "walk_in_scope"), |
| 47 | +} |
| 48 | + |
| 49 | + |
| 50 | +def __getattr__(name: str) -> t.Any: |
| 51 | + import importlib |
| 52 | + |
| 53 | + with _import_lock: |
| 54 | + target = _LAZY_ATTRS.get(name) |
| 55 | + if target is not None: |
| 56 | + module_name, attr = target |
| 57 | + value = getattr(importlib.import_module(module_name), attr) |
| 58 | + else: |
| 59 | + # Submodule fallback so `from sqlglot.optimizer import qualify` works. |
| 60 | + try: |
| 61 | + value = importlib.import_module(f"{__name__}.{name}") |
| 62 | + except ModuleNotFoundError: |
| 63 | + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") from None |
| 64 | + globals()[name] = value |
| 65 | + return value |
0 commit comments