Skip to content

Commit 7728546

Browse files
authored
Merge pull request #110 from henryiii/henryiii/fix/addstr
fix: nicer debug print outs (and str for regex pattern)
2 parents fda7c31 + b70e3fb commit 7728546

4 files changed

Lines changed: 39 additions & 0 deletions

File tree

pathspec/pathspec.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ def __init__(
9898
contains the compiled patterns.
9999
"""
100100

101+
def __repr__(self) -> str:
102+
"""
103+
Returns a debug representation of this path-spec.
104+
"""
105+
return f"{self.__class__.__name__}(patterns={self.patterns!r}, backend={self._backend_name!r})"
106+
101107
def __add__(self: Self, other: PathSpec) -> Self:
102108
"""
103109
Combines the :attr:`self.patterns <.PathSpec.patterns>` patterns from two

pathspec/pattern.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ def __init__(
158158
expression for the pattern.
159159
"""
160160

161+
def __repr__(self) -> str:
162+
"""
163+
Returns a debug representation of this regex pattern.
164+
"""
165+
return f"{self.__class__.__name__}(pattern={self.pattern!r}, include={self.include!r})"
166+
167+
def __str__(self) -> str:
168+
"""
169+
Returns a string representation of this regex pattern. Equivalent to uncompiled pattern.
170+
171+
The string representation is the uncompiled pattern if it is not
172+
:data:`None`; otherwise, an empty string.
173+
"""
174+
return str(self.pattern or "")
175+
161176
def __copy__(self: RegexPatternSelf) -> RegexPatternSelf:
162177
"""
163178
Performa a shallow copy of the pattern.

tests/test_02_gitignore_basic.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,3 +926,11 @@ def test_15_issue_93_c_2(self):
926926
pattern = GitIgnoreBasicPattern('[!]')
927927
self.assertIs(pattern.include, True)
928928
self.assertEqual(pattern.regex.pattern, f'^(?:.+/)?\\[!\\]{_DIR_OPT}')
929+
930+
def test_16_repr_str(self):
931+
"""
932+
Test debug and string representations.
933+
"""
934+
pattern = GitIgnoreBasicPattern('*.py')
935+
self.assertEqual(repr(pattern), "GitIgnoreBasicPattern(pattern='*.py', include=True)")
936+
self.assertEqual(str(pattern), '*.py')

tests/test_04_pathspec.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,3 +1041,13 @@ def test_10_issue_100(self):
10411041
includes = get_includes(results)
10421042
debug = debug_results(spec, results)
10431043
self.assertEqual(includes, set(), debug)
1044+
1045+
def test_11_repr(self):
1046+
"""
1047+
Test the path-spec debug representation.
1048+
"""
1049+
spec = PathSpec.from_lines('gitignore', ['*.py'], backend='simple')
1050+
self.assertEqual(
1051+
repr(spec),
1052+
"PathSpec(patterns=[GitIgnoreBasicPattern(pattern='*.py', include=True)], backend='simple')",
1053+
)

0 commit comments

Comments
 (0)