Skip to content

Commit 4c522b3

Browse files
committed
removes determine all and module reload tests
1 parent d433fae commit 4c522b3

File tree

4 files changed

+12
-157
lines changed

4 files changed

+12
-157
lines changed

db_dtypes/__init__.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -337,22 +337,6 @@ def __sub__(self, other):
337337
return super().__sub__(other)
338338

339339

340-
def _determine_all(json_array_type, json_dtype_type):
341-
"""Determines the list for __all__ based on JSON type availability."""
342-
base_all = [
343-
"__version__",
344-
"DateArray",
345-
"DateDtype",
346-
"TimeArray",
347-
"TimeDtype",
348-
]
349-
# Check if both JSON types are available (truthy)
350-
if json_array_type and json_dtype_type:
351-
return base_all + ["JSONDtype", "JSONArray", "JSONArrowType"]
352-
else:
353-
return base_all
354-
355-
356340
def _check_python_version():
357341
"""Checks the runtime Python version and issues a warning if needed."""
358342
sys_major, sys_minor, sys_micro = _versions_helpers.extract_runtime_version()
@@ -370,4 +354,13 @@ def _check_python_version():
370354

371355
_check_python_version()
372356

373-
__all__ = _determine_all(JSONArray, JSONDtype)
357+
__all__ = [
358+
"__version__",
359+
"DateArray",
360+
"DateDtype",
361+
"TimeArray",
362+
"TimeDtype",
363+
"JSONDtype",
364+
"JSONArray",
365+
"JSONArrowType",
366+
]

db_dtypes/json.py

+2-7
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,5 @@ def to_pandas_dtype(self):
277277

278278

279279
# Register the type to be included in RecordBatches, sent over IPC and received in
280-
# another Python process. Also handle potential pre-registration
281-
try:
282-
pa.register_extension_type(JSONArrowType())
283-
except pa.ArrowKeyError:
284-
# Type 'dbjson' might already be registered if the module is reloaded,
285-
# which is okay.
286-
pass
280+
# another Python process.
281+
pa.register_extension_type(JSONArrowType())

tests/unit/test__init__.py

-59
Original file line numberDiff line numberDiff line change
@@ -83,62 +83,3 @@ def test_check_python_version_does_not_warn_on_supported(mock_version_tuple):
8383

8484
# Assert that warnings.warn was NOT called
8585
mock_warn_call.assert_not_called()
86-
87-
88-
def test_determine_all_includes_json_when_available():
89-
"""
90-
Test that _determine_all includes JSON types when both are truthy.
91-
"""
92-
93-
from db_dtypes import _determine_all
94-
95-
# Simulate available types (can be any truthy object)
96-
mock_json_array = object()
97-
mock_json_dtype = object()
98-
99-
result = _determine_all(mock_json_array, mock_json_dtype)
100-
101-
expected_all = [
102-
"__version__",
103-
"DateArray",
104-
"DateDtype",
105-
"TimeArray",
106-
"TimeDtype",
107-
"JSONDtype",
108-
"JSONArray",
109-
"JSONArrowType",
110-
]
111-
assert set(result) == set(expected_all)
112-
assert "JSONDtype" in result
113-
assert "JSONArray" in result
114-
assert "JSONArrowType" in result
115-
116-
117-
@pytest.mark.parametrize(
118-
"mock_array, mock_dtype",
119-
[
120-
(None, object()), # JSONArray is None
121-
(object(), None), # JSONDtype is None
122-
(None, None), # Both are None
123-
],
124-
)
125-
def test_determine_all_excludes_json_when_unavailable(mock_array, mock_dtype):
126-
"""
127-
Test that _determine_all excludes JSON types if either is falsy.
128-
"""
129-
130-
from db_dtypes import _determine_all
131-
132-
result = _determine_all(mock_array, mock_dtype)
133-
134-
expected_all = [
135-
"__version__",
136-
"DateArray",
137-
"DateDtype",
138-
"TimeArray",
139-
"TimeDtype",
140-
]
141-
assert set(result) == set(expected_all)
142-
assert "JSONDtype" not in result
143-
assert "JSONArray" not in result
144-
assert "JSONArrowType" not in result

tests/unit/test_json.py

-74
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
import json
16-
import sys
1716

1817
import numpy as np
1918
import pandas as pd
@@ -227,76 +226,3 @@ def test_json_arrow_record_batch():
227226
== '{"null_field":null,"order":{"address":{"city":"Anytown","street":"123 Main St"},"items":["book","pen","computer"],"total":15}}'
228227
)
229228
assert s[6] == "null"
230-
231-
232-
@pytest.fixture
233-
def cleanup_json_module_for_reload():
234-
"""
235-
Fixture to ensure db_dtypes.json is registered and then removed
236-
from sys.modules to allow testing the registration except block via reload.
237-
"""
238-
239-
json_module_name = "db_dtypes.json"
240-
original_module = sys.modules.get(json_module_name)
241-
242-
# Ensure the type is registered initially (usually by the first import)
243-
try:
244-
# Make sure the module is loaded so the type exists
245-
import db_dtypes.json
246-
247-
# Explicitly register just in case it wasn't, or was cleaned up elsewhere.
248-
# This might raise ArrowKeyError itself if already registered, which is fine here.
249-
pa.register_extension_type(db_dtypes.json.JSONArrowType())
250-
251-
except pa.ArrowKeyError:
252-
pass # Already registered is the state we want before the test runs
253-
254-
# Remove the module from sys.modules so importlib.reload which will happen
255-
# after the yield statement will re-execute it
256-
if json_module_name in sys.modules: # pragma: NO COVER
257-
del sys.modules[json_module_name]
258-
259-
yield # Run the test that uses this fixture
260-
261-
# Cleanup: Put the original module back if it existed
262-
# This helps isolate from other tests that might import db_dtypes.json
263-
if original_module:
264-
sys.modules[json_module_name] = original_module
265-
elif json_module_name in sys.modules: # pragma: NO COVER
266-
# If the test re-imported it but it wasn't there originally, remove it
267-
del sys.modules[json_module_name]
268-
269-
# Note: PyArrow doesn't have a public API to unregister types easily,
270-
# thus we are using the testing pattern of module isolation/reloading.
271-
272-
273-
# Test specifically for the fixture's pre-yield removal logic
274-
def test_fixture_removes_module_if_present(cleanup_json_module_for_reload):
275-
"""
276-
Tests that the cleanup_json_module_for_reload fixture removes
277-
db_dtypes.json from sys.modules before yielding to the test.
278-
This specifically targets the 'if json_module_name in sys.modules:' block.
279-
"""
280-
# This test runs *after* the fixture's `yield`.
281-
# The fixture should have removed the module if it was present.
282-
283-
json_module_name = "db_dtypes.json"
284-
285-
assert (
286-
json_module_name not in sys.modules
287-
), f"The fixture cleanup_json_module_for_reload should have removed {json_module_name}"
288-
289-
290-
def test_json_arrow_type_reregistration_is_handled(cleanup_json_module_for_reload):
291-
"""
292-
Verify that attempting to re-register JSONArrowType via module reload
293-
is caught by the except block and does not raise an error.
294-
"""
295-
296-
# Re-importing the module after the fixture removed it from sys.modules
297-
# forces Python to execute the module's top-level code again.
298-
# This includes the pa.register_extension_type call.
299-
300-
import db_dtypes.json # noqa: F401
301-
302-
assert True, "Module re-import completed without error, except block likely worked."

0 commit comments

Comments
 (0)