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
6 changes: 6 additions & 0 deletions pathspec/pathspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def __init__(
contains the compiled patterns.
"""

def __repr__(self) -> str:
"""
Returns a debug representation of this path-spec.
"""
return f"{self.__class__.__name__}(patterns={self.patterns!r}, backend={self._backend_name!r})"

def __add__(self: Self, other: PathSpec) -> Self:
"""
Combines the :attr:`self.patterns <.PathSpec.patterns>` patterns from two
Expand Down
15 changes: 15 additions & 0 deletions pathspec/pattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ def __init__(
expression for the pattern.
"""

def __repr__(self) -> str:
"""
Returns a debug representation of this regex pattern.
"""
return f"{self.__class__.__name__}(pattern={self.pattern!r}, include={self.include!r})"

def __str__(self) -> str:
"""
Returns a string representation of this regex pattern. Equivalent to uncompiled pattern.

The string representation is the uncompiled pattern if it is not
:data:`None`; otherwise, an empty string.
"""
return str(self.pattern or "")

def __copy__(self: RegexPatternSelf) -> RegexPatternSelf:
"""
Performa a shallow copy of the pattern.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_02_gitignore_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,3 +926,11 @@ def test_15_issue_93_c_2(self):
pattern = GitIgnoreBasicPattern('[!]')
self.assertIs(pattern.include, True)
self.assertEqual(pattern.regex.pattern, f'^(?:.+/)?\\[!\\]{_DIR_OPT}')

def test_16_repr_str(self):
"""
Test debug and string representations.
"""
pattern = GitIgnoreBasicPattern('*.py')
self.assertEqual(repr(pattern), "GitIgnoreBasicPattern(pattern='*.py', include=True)")
self.assertEqual(str(pattern), '*.py')
10 changes: 10 additions & 0 deletions tests/test_04_pathspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -1041,3 +1041,13 @@ def test_10_issue_100(self):
includes = get_includes(results)
debug = debug_results(spec, results)
self.assertEqual(includes, set(), debug)

def test_11_repr(self):
"""
Test the path-spec debug representation.
"""
spec = PathSpec.from_lines('gitignore', ['*.py'], backend='simple')
self.assertEqual(
repr(spec),
"PathSpec(patterns=[GitIgnoreBasicPattern(pattern='*.py', include=True)], backend='simple')",
)