From 665c8d6a3e6174d88924fb953416a1f6e161136e Mon Sep 17 00:00:00 2001 From: Tianqi Tang <47551755+TianqiTang1117@users.noreply.github.com> Date: Thu, 21 Aug 2025 09:56:02 +0800 Subject: [PATCH] Add tests for file path utility? --- qlib/utils/file.py | 7 ++++++- qlib/utils/index_data.py | 10 +++++----- qlib/utils/mod.py | 2 +- tests/misc/test_file_utils.py | 20 ++++++++++++++++++++ 4 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 tests/misc/test_file_utils.py diff --git a/qlib/utils/file.py b/qlib/utils/file.py index 1e17a574a9..b4da695585 100644 --- a/qlib/utils/file.py +++ b/qlib/utils/file.py @@ -34,7 +34,12 @@ def get_or_create_path(path: Optional[Text] = None, return_dir: bool = False): if not os.path.exists(temp_dir): os.makedirs(temp_dir) if return_dir: - _, path = tempfile.mkdtemp(dir=temp_dir) + # `mkdtemp` returns the created directory path directly, while `mkstemp` + # returns a tuple of `(fd, path)`. The previous implementation tried to + # unpack the return value of `mkdtemp` into two variables, which raises a + # `ValueError` at runtime. This branch should therefore assign the path + # directly without unpacking. + path = tempfile.mkdtemp(dir=temp_dir) else: _, path = tempfile.mkstemp(dir=temp_dir) return path diff --git a/qlib/utils/index_data.py b/qlib/utils/index_data.py index c707240d09..60730ba9c3 100644 --- a/qlib/utils/index_data.py +++ b/qlib/utils/index_data.py @@ -11,15 +11,15 @@ from __future__ import annotations -from typing import Dict, Tuple, Union, Callable, List +from typing import Dict, Tuple, Callable, List import bisect import numpy as np import pandas as pd -def concat(data_list: Union[SingleData], axis=0) -> MultiData: - """concat all SingleData by index. +def concat(data_list: List["SingleData"], axis=0) -> "MultiData": + """Concat multiple :class:`SingleData` objects by index. TODO: now just for SingleData. Parameters @@ -54,8 +54,8 @@ def concat(data_list: Union[SingleData], axis=0) -> MultiData: raise ValueError(f"axis must be 0 or 1") -def sum_by_index(data_list: Union[SingleData], new_index: list, fill_value=0) -> SingleData: - """concat all SingleData by new index. +def sum_by_index(data_list: List["SingleData"], new_index: list, fill_value=0) -> "SingleData": + """Sum all ``SingleData`` instances into a new index. Parameters ---------- diff --git a/qlib/utils/mod.py b/qlib/utils/mod.py index 12fbc58703..29e4f8db93 100644 --- a/qlib/utils/mod.py +++ b/qlib/utils/mod.py @@ -3,7 +3,7 @@ """ All module related class, e.g. : - importing a module, class -- walkiing a module +- walking a module - operations on class or module... """ diff --git a/tests/misc/test_file_utils.py b/tests/misc/test_file_utils.py new file mode 100644 index 0000000000..d32fd4cd34 --- /dev/null +++ b/tests/misc/test_file_utils.py @@ -0,0 +1,20 @@ +import os +from pathlib import Path + +from qlib.utils.file import get_or_create_path + + +def test_get_or_create_path_none_dir(tmp_path): + # When no path is provided, a temporary directory should be created + created = get_or_create_path(return_dir=True) + assert os.path.isdir(created) + # cleanup + os.rmdir(created) + + +def test_get_or_create_path_creates_parent(tmp_path): + # When a file path is provided, the parent directory should be created + file_path = tmp_path / "a" / "b" / "c.txt" + result = get_or_create_path(str(file_path)) + assert Path(result) == file_path + assert file_path.parent.is_dir()