From b7dbc13457f62293136ea67a6124059f861ec5ff Mon Sep 17 00:00:00 2001 From: RHammond2 <13874373+RHammond2@users.noreply.github.com> Date: Thu, 30 Nov 2023 09:13:14 -0800 Subject: [PATCH 1/6] fix third-case to be correct with new conversion logic --- tests/type_dec_unit_test.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/type_dec_unit_test.py b/tests/type_dec_unit_test.py index 43bf5bc3a..34e4276f3 100644 --- a/tests/type_dec_unit_test.py +++ b/tests/type_dec_unit_test.py @@ -143,10 +143,10 @@ def test_convert_to_path(): # Test that both of those inputs are the same assert test_str_input == test_path_input - # Test that a non-existent folder also works even though it's a valid data type - str_input = "tests" - test_str_input = convert_to_path(str_input) - assert isinstance(test_str_input, Path) + # Test that a non-existent folder fails, now that the conversion has a multi-pronged search + str_input = str(Path(__file__).parent / "tests") + with pytest.raises(FileExistsError): + convert_to_path(str_input) # Test that invalid data types fail with pytest.raises(TypeError): From 1d109686674cbbafcef07732e0c7f0702b9e0ec5 Mon Sep 17 00:00:00 2001 From: Rafael M Mudafort Date: Fri, 1 Dec 2023 00:30:47 -0600 Subject: [PATCH 2/6] Test the value of the result, implicitly the type --- tests/type_dec_unit_test.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/tests/type_dec_unit_test.py b/tests/type_dec_unit_test.py index 34e4276f3..8d5e91491 100644 --- a/tests/type_dec_unit_test.py +++ b/tests/type_dec_unit_test.py @@ -130,19 +130,27 @@ def test_attrs_array_converter(): def test_convert_to_path(): - # Test that a string works str_input = "../tests" + expected_path = (Path(__file__).parent / str_input).resolve() + + # Test that a string works test_str_input = convert_to_path(str_input) - assert isinstance(test_str_input, Path) + assert test_str_input == expected_path # Test that a pathlib.Path works - path_input = Path("../tests") + path_input = Path(str_input) test_path_input = convert_to_path(path_input) - assert isinstance(test_path_input, Path) + assert test_path_input == expected_path # Test that both of those inputs are the same + # NOTE These first three asserts tests the relative path search assert test_str_input == test_path_input + # Test absolute path + abs_path = expected_path + test_abs_path = convert_to_path(abs_path) + assert test_abs_path == expected_path + # Test that a non-existent folder fails, now that the conversion has a multi-pronged search str_input = str(Path(__file__).parent / "tests") with pytest.raises(FileExistsError): From 2beadeed26adbe412ae1d5e3e3108cd27de05ba8 Mon Sep 17 00:00:00 2001 From: Rafael M Mudafort Date: Fri, 1 Dec 2023 00:31:34 -0600 Subject: [PATCH 3/6] Test a file search --- tests/type_dec_unit_test.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/type_dec_unit_test.py b/tests/type_dec_unit_test.py index 8d5e91491..5cfe307d9 100644 --- a/tests/type_dec_unit_test.py +++ b/tests/type_dec_unit_test.py @@ -151,6 +151,11 @@ def test_convert_to_path(): test_abs_path = convert_to_path(abs_path) assert test_abs_path == expected_path + # Test a file + file_input = Path(__file__) + test_file = convert_to_path(file_input) + assert test_file == file_input + # Test that a non-existent folder fails, now that the conversion has a multi-pronged search str_input = str(Path(__file__).parent / "tests") with pytest.raises(FileExistsError): From 232f498b76bac229411e24d7d7847446079f358d Mon Sep 17 00:00:00 2001 From: Rafael M Mudafort Date: Fri, 1 Dec 2023 00:39:02 -0600 Subject: [PATCH 4/6] Use a more obviously bad path --- tests/type_dec_unit_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/type_dec_unit_test.py b/tests/type_dec_unit_test.py index 5cfe307d9..df2c1b1ff 100644 --- a/tests/type_dec_unit_test.py +++ b/tests/type_dec_unit_test.py @@ -157,7 +157,7 @@ def test_convert_to_path(): assert test_file == file_input # Test that a non-existent folder fails, now that the conversion has a multi-pronged search - str_input = str(Path(__file__).parent / "tests") + str_input = str(Path(__file__).parent / "bad_path") with pytest.raises(FileExistsError): convert_to_path(str_input) From 8c2cf22919c07541ceacf69909c2d91f6045a374 Mon Sep 17 00:00:00 2001 From: RHammond2 <13874373+RHammond2@users.noreply.github.com> Date: Fri, 1 Dec 2023 09:09:23 -0800 Subject: [PATCH 5/6] udpate to be inclusive of file checking --- floris/type_dec.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/floris/type_dec.py b/floris/type_dec.py index 830f6c6ab..07359c140 100644 --- a/floris/type_dec.py +++ b/floris/type_dec.py @@ -85,7 +85,7 @@ def iter_validator(iter_type, item_types: Union[Any, Tuple[Any]]) -> Callable: return validator def convert_to_path(fn: str | Path) -> Path: - """Converts an input string or pathlib.Path object to a fully resolved ``pathlib.Path`` + """Converts an input string or ``pathlib.Path`` object to a fully resolved ``pathlib.Path`` object. Args: @@ -113,11 +113,11 @@ def convert_to_path(fn: str | Path) -> Path: absolute_fn = fn.resolve() relative_fn_script = (base_fn_script / fn).resolve() relative_fn_sys = (base_fn_sys / fn).resolve() - if absolute_fn.is_dir(): + if absolute_fn.exists(): return absolute_fn - if relative_fn_script.is_dir(): + if relative_fn_script.exists(): return relative_fn_script - if relative_fn_sys.is_dir(): + if relative_fn_sys.exists(): return relative_fn_sys raise FileExistsError( f"{fn} could not be found as either a\n" From d3040a173c0c62d9f2e7b6bcb1e809289d78d0b0 Mon Sep 17 00:00:00 2001 From: Rafael M Mudafort Date: Fri, 1 Dec 2023 11:38:30 -0600 Subject: [PATCH 6/6] Add more context in the docstring --- floris/type_dec.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/floris/type_dec.py b/floris/type_dec.py index 07359c140..e2cec3ce6 100644 --- a/floris/type_dec.py +++ b/floris/type_dec.py @@ -85,8 +85,12 @@ def iter_validator(iter_type, item_types: Union[Any, Tuple[Any]]) -> Callable: return validator def convert_to_path(fn: str | Path) -> Path: - """Converts an input string or ``pathlib.Path`` object to a fully resolved ``pathlib.Path`` - object. + """ + Converts an input string or ``pathlib.Path`` object to a fully resolved ``pathlib.Path`` + object. If the input is a string, it is converted to a pathlib.Path object. + The function then checks if the path exists as an absolute path, a relative path from + the script, or a relative path from the system location. If the path does not exist in + any of these locations, a FileExistsError is raised. Args: fn (str | Path): The user input file path or file name.