Skip to content

Validate the names of data catalogs. #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/source/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ releases are available on [PyPI](https://pypi.org/project/pytask) and
in pytask-parallel.
- {pull}`611` removes the initial task execution status from
`pytask_execute_task_log_start`.
- {pull}`612` adds validation for data catalog names.

## 0.4.7 - 2024-03-19

Expand Down
16 changes: 15 additions & 1 deletion src/_pytask/data_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import hashlib
import inspect
import pickle
import re
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -61,13 +62,26 @@ class DataCatalog:

default_node: type[PNode] = PickleNode
entries: dict[str, PNode | PProvisionalNode] = field(factory=dict)
name: str = "default"
name: str = field(default="default")
path: Path | None = None
_session_config: dict[str, Any] = field(
factory=lambda *x: {"check_casing_of_paths": True} # noqa: ARG005
)
_instance_path: Path = field(factory=_get_parent_path_of_data_catalog_module)

@name.validator
def _check(self, attribute: str, value: str) -> None: # noqa: ARG002
_rich_traceback_omit = True
if not isinstance(value, str):
msg = "The name of a data catalog must be a string."
raise TypeError(msg)
if not re.match(r"[a-zA-Z0-9-_]+", value):
msg = (
"The name of a data catalog must be a string containing only letters, "
"numbers, hyphens, and underscores."
)
raise ValueError(msg)

def __attrs_post_init__(self) -> None:
root_path, _ = find_project_root_and_config((self._instance_path,))
self._session_config["paths"] = (root_path,)
Expand Down
14 changes: 14 additions & 0 deletions tests/test_data_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,17 @@ def task_add_content(
result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.OK
assert tmp_path.joinpath("output.txt").read_text() == "Hello, World!"


@pytest.mark.end_to_end()
def test_data_catalog_has_invalid_name(runner, tmp_path):
source = """
from pytask import DataCatalog

data_catalog = DataCatalog(name="?1")
"""
tmp_path.joinpath("task_example.py").write_text(textwrap.dedent(source))

result = runner.invoke(cli, [tmp_path.as_posix()])
assert result.exit_code == ExitCode.COLLECTION_FAILED
assert "The name of a data catalog" in result.stdout
Loading