Skip to content

Commit 9b8bf1a

Browse files
bosdqwencoder
andcommitted
Fix typeguard issues in import tests and improve type safety\n\n- Fix typeguard error in test_run_import_invalid_json_type_context by using proper typing\n- Improve type safety in run_import function by separating json.loads() result from typed variable\n- Ensure all 385 tests pass with typeguard enabled\n- Maintain all existing functionality while improving code quality
Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
1 parent 99f89ce commit 9b8bf1a

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/odoo_data_flow/importer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,10 @@ def run_import( # noqa: C901
113113
parsed_context: dict[str, Any]
114114
if isinstance(context, str):
115115
try:
116-
parsed_context = json.loads(context)
117-
if not isinstance(parsed_context, dict):
116+
loaded_context = json.loads(context)
117+
if not isinstance(loaded_context, dict):
118118
raise TypeError
119+
parsed_context = loaded_context
119120
except (json.JSONDecodeError, TypeError):
120121
_show_error_panel(
121122
"Invalid Context",

tests/test_importer.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -470,15 +470,17 @@ def preflight_side_effect(*args: Any, **kwargs: Any) -> bool:
470470

471471

472472
@patch("odoo_data_flow.importer._show_error_panel")
473-
def test_run_import_invalid_json_type_context(
474-
mock_show_error: MagicMock,
475-
) -> None:
473+
def test_run_import_invalid_json_type_context(mock_show_error: MagicMock) -> None:
476474
"""Test that run_import handles context that is not a JSON dict."""
475+
# Using Any type to bypass type checking for this specific test case
476+
# This tests the runtime error handling for invalid context types
477+
context: Any = '["not", "a", "dict"]' # Valid JSON, but not a dict
478+
477479
run_import(
478480
config="dummy.conf",
479481
filename="dummy.csv",
480482
model="res.partner",
481-
context='["not", "a", "dict"]', # Valid JSON, but not a dict
483+
context=context,
482484
deferred_fields=None,
483485
unique_id_field=None,
484486
no_preflight_checks=True,

0 commit comments

Comments
 (0)