File tree 3 files changed +30
-1
lines changed
3 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
51
51
in pytask-parallel.
52
52
- {pull}` 611 ` removes the initial task execution status from
53
53
` pytask_execute_task_log_start ` .
54
+ - {pull}` 612 ` adds validation for data catalog names.
54
55
55
56
## 0.4.7 - 2024-03-19
56
57
Original file line number Diff line number Diff line change 9
9
import hashlib
10
10
import inspect
11
11
import pickle
12
+ import re
12
13
from pathlib import Path
13
14
from typing import Any
14
15
@@ -61,13 +62,26 @@ class DataCatalog:
61
62
62
63
default_node : type [PNode ] = PickleNode
63
64
entries : dict [str , PNode | PProvisionalNode ] = field (factory = dict )
64
- name : str = "default"
65
+ name : str = field ( default = "default" )
65
66
path : Path | None = None
66
67
_session_config : dict [str , Any ] = field (
67
68
factory = lambda * x : {"check_casing_of_paths" : True } # noqa: ARG005
68
69
)
69
70
_instance_path : Path = field (factory = _get_parent_path_of_data_catalog_module )
70
71
72
+ @name .validator
73
+ def _check (self , attribute : str , value : str ) -> None : # noqa: ARG002
74
+ _rich_traceback_omit = True
75
+ if not isinstance (value , str ):
76
+ msg = "The name of a data catalog must be a string."
77
+ raise TypeError (msg )
78
+ if not re .match (r"[a-zA-Z0-9-_]+" , value ):
79
+ msg = (
80
+ "The name of a data catalog must be a string containing only letters, "
81
+ "numbers, hyphens, and underscores."
82
+ )
83
+ raise ValueError (msg )
84
+
71
85
def __attrs_post_init__ (self ) -> None :
72
86
root_path , _ = find_project_root_and_config ((self ._instance_path ,))
73
87
self ._session_config ["paths" ] = (root_path ,)
Original file line number Diff line number Diff line change @@ -234,3 +234,17 @@ def task_add_content(
234
234
result = runner .invoke (cli , [tmp_path .as_posix ()])
235
235
assert result .exit_code == ExitCode .OK
236
236
assert tmp_path .joinpath ("output.txt" ).read_text () == "Hello, World!"
237
+
238
+
239
+ @pytest .mark .end_to_end ()
240
+ def test_data_catalog_has_invalid_name (runner , tmp_path ):
241
+ source = """
242
+ from pytask import DataCatalog
243
+
244
+ data_catalog = DataCatalog(name="?1")
245
+ """
246
+ tmp_path .joinpath ("task_example.py" ).write_text (textwrap .dedent (source ))
247
+
248
+ result = runner .invoke (cli , [tmp_path .as_posix ()])
249
+ assert result .exit_code == ExitCode .COLLECTION_FAILED
250
+ assert "The name of a data catalog" in result .stdout
You can’t perform that action at this time.
0 commit comments