From fcaf529140e1b69c3ba24506edc6fdedf321f8c4 Mon Sep 17 00:00:00 2001 From: Andrei Berenda Date: Fri, 25 Oct 2024 21:12:09 +0200 Subject: [PATCH] Refactor path_type_label --- pydantic_settings/utils.py | 22 +++++++++++----------- tests/test_utils.py | 6 ++++++ 2 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 tests/test_utils.py diff --git a/pydantic_settings/utils.py b/pydantic_settings/utils.py index 73090b8a..35dceea2 100644 --- a/pydantic_settings/utils.py +++ b/pydantic_settings/utils.py @@ -1,14 +1,14 @@ from pathlib import Path -path_type_labels = { - 'is_dir': 'directory', - 'is_file': 'file', - 'is_mount': 'mount point', - 'is_symlink': 'symlink', - 'is_block_device': 'block device', - 'is_char_device': 'char device', - 'is_fifo': 'FIFO', - 'is_socket': 'socket', +_PATH_TYPE_LABELS = { + Path.is_dir: 'directory', + Path.is_file: 'file', + Path.is_mount: 'mount point', + Path.is_symlink: 'symlink', + Path.is_block_device: 'block device', + Path.is_char_device: 'char device', + Path.is_fifo: 'FIFO', + Path.is_socket: 'socket', } @@ -17,8 +17,8 @@ def path_type_label(p: Path) -> str: Find out what sort of thing a path is. """ assert p.exists(), 'path does not exist' - for method, name in path_type_labels.items(): - if getattr(p, method)(): + for method, name in _PATH_TYPE_LABELS.items(): + if method(p): return name return 'unknown' diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..c01ebdb9 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,6 @@ +from pydantic_settings.utils import path_type_label + + +def test_path_type_label(tmp_path): + result = path_type_label(tmp_path) + assert result == 'directory'