From 7e3a03feaf87caad8734ccf09e04e2f2f178019d Mon Sep 17 00:00:00 2001 From: Mrunmayee Daware <165297479+Mrun25@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:21:59 +0530 Subject: [PATCH 1/4] Refactor Pattern class by removing TypeVar and __copy__ --- pathspec/pattern.py | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/pathspec/pattern.py b/pathspec/pattern.py index 018cd2c..9ec56e6 100644 --- a/pathspec/pattern.py +++ b/pathspec/pattern.py @@ -12,7 +12,6 @@ from typing import ( Any, Optional, # Replaced by `X | None` in 3.10. - TypeVar, Union) # Replaced by `X | Y` in 3.10. from ._typing import ( @@ -20,12 +19,6 @@ deprecated, # Added in 3.13. override) # Added in 3.12. -RegexPatternSelf = TypeVar("RegexPatternSelf", bound='RegexPattern') -""" -:class:`.RegexPattern` self type hint to support Python v<3.11 using PEP 673 -recommendation. -""" - class Pattern(object): """ The :class:`Pattern` class is the abstract definition of a pattern. @@ -179,16 +172,6 @@ def __str__(self) -> str: """ return str(self.pattern or "") - def __copy__(self: RegexPatternSelf) -> RegexPatternSelf: - """ - Performa a shallow copy of the pattern. - - Returns the copy (:class:`RegexPattern`). - """ - other = self.__class__(self.regex, self.include) - other.pattern = self.pattern - return other - def __eq__(self, other: object) -> bool: """ Tests the equality of this regex pattern with *other* (:class:`RegexPattern`) From 56e9a10200bb0711b30525d6cf9cbd7bcba8db11 Mon Sep 17 00:00:00 2001 From: Mrunmayee Daware <165297479+Mrun25@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:22:43 +0530 Subject: [PATCH 2/4] Preserve subclass state when copying regular expression patterns --- CHANGES_1.in.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES_1.in.rst b/CHANGES_1.in.rst index ca91dab..20c8bad 100644 --- a/CHANGES_1.in.rst +++ b/CHANGES_1.in.rst @@ -21,6 +21,8 @@ New features: Bug fixes: +- Preserve subclass state when shallow-copying regular expression patterns, including subclasses without an include parameter. + - Preserve slashes inside gitignore bracket expressions while excluding path separators from their matches. - Hyperscan backend no longer fails on multibyte UTF-8 characters. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. From 9c839182f756c3ab4441e1c6a0da60267d57c3ff Mon Sep 17 00:00:00 2001 From: Mrunmayee Daware <165297479+Mrun25@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:23:08 +0530 Subject: [PATCH 3/4] Preserve subclass state when copying regular expression patterns --- CHANGES.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES.rst b/CHANGES.rst index 867e56e..bb5d86c 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -24,6 +24,8 @@ New features: Bug fixes: +- Preserve subclass state when shallow-copying regular expression patterns, including subclasses without an include parameter. + - Preserve slashes inside gitignore bracket expressions while excluding path separators from their matches. - Hyperscan backend no longer fails on multibyte UTF-8 characters. - `Pull #123`_: Ignore invalid gitignore bracket ranges for `GitIgnoreSpec`. From dbc6a6e25979ed8daffebad346da21fcbb18d20f Mon Sep 17 00:00:00 2001 From: Mrunmayee Daware <165297479+Mrun25@users.noreply.github.com> Date: Sun, 8 Nov 2026 09:24:13 +0530 Subject: [PATCH 4/4] Preserve subclass state when copying regular expression patterns --- tests/test_11_pattern_copy.py | 78 +++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/test_11_pattern_copy.py diff --git a/tests/test_11_pattern_copy.py b/tests/test_11_pattern_copy.py new file mode 100644 index 0000000..f15b3bf --- /dev/null +++ b/tests/test_11_pattern_copy.py @@ -0,0 +1,78 @@ +"""Test shallow copying of regular expression patterns and their subclasses.""" + +import re +from copy import copy +from unittest import TestCase + +from pathspec import RegexPattern +from pathspec.patterns.gitignore.basic import GitIgnoreBasicPattern +from pathspec.patterns.gitignore.spec import GitIgnoreSpecPattern + + +class SingleArgumentPattern(RegexPattern): + def __init__(self, pattern): + super().__init__(pattern) + + +class StatefulPattern(RegexPattern): + def __init__(self, pattern, include=None): + super().__init__(pattern, include) + self.metadata = [] + + +class SlottedPattern(RegexPattern): + __slots__ = ('metadata', '__private', 'unset') + + def __init__(self, pattern, include=None): + super().__init__(pattern, include) + self.metadata = [] + self.__private = [] + + +class PatternCopyTest(TestCase): + + def assert_pattern_copy(self, pattern): + other = copy(pattern) + self.assertIsNot(other, pattern) + self.assertIs(type(other), type(pattern)) + self.assertIs(other.pattern, pattern.pattern) + self.assertIs(other.regex, pattern.regex) + self.assertIs(other.include, pattern.include) + self.assertEqual( + bool(other.match_file('abc.txt')), bool(pattern.match_file('abc.txt')), + ) + return other + + def test_builtin_patterns(self): + for factory in (RegexPattern, GitIgnoreBasicPattern, GitIgnoreSpecPattern): + for text in ('abc.txt', None): + with self.subTest(factory=factory, text=text): + self.assert_pattern_copy(factory(text)) + + def test_compiled_pattern_and_include(self): + for include in (True, False, None): + with self.subTest(include=include): + pattern = RegexPattern(re.compile('abc.txt', re.IGNORECASE), include) + other = self.assert_pattern_copy(pattern) + self.assertIsNotNone(other.match_file('ABC.TXT')) + + def test_subclass_without_include_parameter(self): + self.assert_pattern_copy(SingleArgumentPattern('abc.txt')) + + def test_subclass_dictionary_state(self): + pattern = StatefulPattern('abc.txt') + pattern.metadata.append('retained') + other = self.assert_pattern_copy(pattern) + self.assertIs(other.metadata, pattern.metadata) + self.assertIsNot(other.__dict__, pattern.__dict__) + + def test_subclass_slot_state(self): + pattern = SlottedPattern('abc.txt') + pattern.metadata.append('retained') + other = self.assert_pattern_copy(pattern) + self.assertIs(other.metadata, pattern.metadata) + self.assertIs( + getattr(other, '_SlottedPattern__private'), + getattr(pattern, '_SlottedPattern__private'), + ) + self.assertFalse(hasattr(other, 'unset'))