2323# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424# Boston, MA 02110-1301, USA.
2525
26+ from __future__ import annotations
27+
2628import weakref
2729from collections .abc import Callable
30+ from typing import TYPE_CHECKING
2831
29- from ._pygit2 import Blob , FilterSource , Repository
32+ from ._pygit2 import Blob , FilterSource
3033from .errors import check_error
3134from .ffi import C , ffi
35+ from .repository import BaseRepository
3236from .utils import to_bytes
3337
38+ if TYPE_CHECKING :
39+ from ._libgit2 .ffi import GitFilterListC
40+
3441
3542class Filter :
3643 """
@@ -114,7 +121,9 @@ def close(self, write_next: Callable[[bytes], None]) -> None:
114121
115122
116123class FilterList :
117- _all_filter_lists = set ()
124+ _all_filter_lists : set [weakref .ReferenceType [FilterList ]] = set ()
125+
126+ _pointer : GitFilterListC
118127
119128 @classmethod
120129 def _from_c (cls , ptr ):
@@ -134,7 +143,14 @@ def _from_c(cls, ptr):
134143
135144 @classmethod
136145 def _is_filter_in_use (cls , name : str ) -> bool :
137- return any (name in ref () for ref in cls ._all_filter_lists )
146+ for ref in cls ._all_filter_lists :
147+ fl = ref ()
148+ assert fl is not None , (
149+ 'dead FilterList refs should be purged by weakref callback'
150+ )
151+ if name in fl :
152+ return True
153+ return False
138154
139155 def __contains__ (self , name : str ) -> bool :
140156 if not isinstance (name , str ):
@@ -159,7 +175,7 @@ def apply_to_buffer(self, data: bytes) -> bytes:
159175 finally :
160176 C .git_buf_dispose (buf )
161177
162- def apply_to_file (self , repo : Repository , path : str ) -> bytes :
178+ def apply_to_file (self , repo : BaseRepository , path : str ) -> bytes :
163179 """
164180 Apply a filter list to the contents of a file on disk.
165181 Return the filtered contents.
0 commit comments