Skip to content
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

error for unnamed fields #110

Merged
merged 2 commits into from
Oct 15, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- Added two methods to find _exactly_ one file or path (and raise an error otherwise):
`FileFinder.find_single_file` and `FileFinder.find_single_path`
([#101](https://github.com/mathause/filefinder/pull/101)).
- Raise an error if an unnamed placeholder (e.g., `"{}"`) is passed
([#110](https://github.com/mathause/filefinder/pull/110))
- The `FileFinder.find_files` arguments `on_parse_error` and `_allow_empty` can no
longer be passed by position ([#99](https://github.com/mathause/filefinder/pull/99)).
- `FileFinder` now raises an error if an invalid `"{placeholder}"` is used
Expand Down
8 changes: 8 additions & 0 deletions filefinder/_filefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ def __init__(self, pattern, suffix=""):
self.keys = _find_keys(pattern)
_assert_valid_keys(self.keys)
self.parser = parse.compile(self.pattern)

if self.parser.fixed_fields:
msg = (
"Only named fields are currently allowed: avoid empty braces and"
" leading underscores."
)
raise ValueError(msg)

self._suffix = suffix

# replace the fmt spec - add the capture group again
Expand Down
10 changes: 10 additions & 0 deletions filefinder/tests/test_filefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ def test_pattern_invalid_placeholder(placeholder):
FileFinder(f"{{{placeholder}}}", "")


@pytest.mark.parametrize("pattern", ("{}", "{_fixed}"))
def test_only_named_fields(pattern):

with pytest.raises(ValueError, match="Only named fields are currently allowed"):
FileFinder("", pattern)

with pytest.raises(ValueError, match="Only named fields are currently allowed"):
FileFinder(pattern, "")


def test_pattern_property():

path_pattern = "path_pattern/"
Expand Down