Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- n/a
### Changed
- Patterns to `Matcher.regex` are now more strictly typechecked when the matcher is created.

## [2.3.0] - 2019-09-25

Expand Down
7 changes: 7 additions & 0 deletions pubtools/pulplib/_impl/criteria.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,13 @@ class RegexMatcher(Matcher):

@_pattern.validator
def _check_pattern(self, _, pattern):
# It must be a string.
# Need an explicit check here because re.compile also succeeds
# on already-compiled regex objects.
if not isinstance(pattern, six.string_types):
raise TypeError("Regex matcher expected string, got: %s" % repr(pattern))

# Verify that the given value can really be compiled as a regex.
re.compile(pattern)

# Note: regex matcher does not implement _map since regex is defined only
Expand Down
7 changes: 7 additions & 0 deletions tests/criteria/test_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ def test_matcher_regex_invalid():

with pytest.raises(re.error):
Matcher.regex("foo [bar")


def test_matcher_regex_compiled():
"""Matcher.regex raises if passed value is a compiled regex."""

with pytest.raises(TypeError):
Matcher.regex(re.compile("foobar"))