Skip to content

Commit 9770a69

Browse files
vuanhphungIsaac
andcommitted
fix: read pyarrow attributes from the live module through the stand-in
The stand-in copied each resolved attribute onto itself, so a later patch of pyarrow (mock.patch, monkeypatch, shims) didn't reach code reading through it. It now keeps no copies and always delegates to the imported module; the hot path reads the real module directly since the rebinding. Co-authored-by: Isaac <no-reply@databricks.com> Signed-off-by: Vu Anh Phung <vu.phung@databricks.com>
1 parent 517c4c8 commit 9770a69

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

‎src/databricks/sql/common/optional_pyarrow.py‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ class _LazyPyarrow(types.ModuleType):
2525
2626
The first use rebinds ``pyarrow`` to the real module in every registered
2727
namespace still holding the stand-in, so reads on the hot path afterwards
28-
are plain module attribute lookups.
28+
are plain module attribute lookups. The stand-in keeps no copies: a read
29+
through it always gets the live module's attribute, patches included.
2930
"""
3031

3132
def __getattr__(self, name: str) -> Any:
@@ -43,9 +44,7 @@ def __getattr__(self, name: str) -> Any:
4344
for namespace in _NAMESPACES:
4445
if namespace.get("pyarrow") is self:
4546
namespace["pyarrow"] = module
46-
value = getattr(module, name)
47-
setattr(self, name, value)
48-
return value
47+
return getattr(module, name)
4948

5049

5150
_LAZY_PYARROW = _LazyPyarrow("pyarrow")

‎tests/unit/test_lazy_pyarrow_import.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,17 @@ def test_rebinding_leaves_a_replaced_global_alone(monkeypatch):
151151
assert replaced["pyarrow"] == "patched"
152152

153153

154+
def test_stand_in_reads_the_live_module(monkeypatch):
155+
# No copies on the stand-in, so patching pyarrow applies through it too.
156+
import pyarrow
157+
158+
stand_in = optional_pyarrow_module._LazyPyarrow("pyarrow")
159+
assert stand_in.concat_tables is pyarrow.concat_tables
160+
patched = object()
161+
monkeypatch.setattr(pyarrow, "concat_tables", patched)
162+
assert stand_in.concat_tables is patched
163+
164+
154165
def test_optional_pyarrow_is_none_when_not_installed(monkeypatch):
155166
monkeypatch.setitem(sys.modules, "pyarrow", None)
156167
monkeypatch.setattr(

0 commit comments

Comments
 (0)