2323# the Free Software Foundation, 51 Franklin Street, Fifth Floor,
2424# Boston, MA 02110-1301, USA.
2525
26+ from os import PathLike
27+ from typing import TYPE_CHECKING
28+
2629try :
2730 from functools import cached_property
2831except ImportError :
3336from .ffi import C , ffi
3437from .utils import to_bytes
3538
39+ if TYPE_CHECKING :
40+ from ._libgit2 .ffi import GitConfigC , GitConfigEntryC , GitRepositoryC
41+
3642
37- def str_to_bytes (value , name ) :
43+ def str_to_bytes (value : str | PathLike [ str ] | bytes , name : str ) -> bytes :
3844 if not isinstance (value , str ):
3945 raise TypeError (f'{ name } must be a string' )
4046
@@ -72,41 +78,44 @@ def __next__(self):
7278class Config :
7379 """Git configuration management."""
7480
75- def __init__ (self , path = None ):
81+ _repo : 'GitRepositoryC'
82+ _config : 'GitConfigC'
83+
84+ def __init__ (self , path : str | None = None ) -> None :
7685 cconfig = ffi .new ('git_config **' )
7786
7887 if not path :
7988 err = C .git_config_new (cconfig )
8089 else :
81- path = str_to_bytes (path , 'path' )
82- err = C .git_config_open_ondisk (cconfig , path )
90+ path_bytes = str_to_bytes (path , 'path' )
91+ err = C .git_config_open_ondisk (cconfig , path_bytes )
8392
8493 check_error (err , io = True )
8594 self ._config = cconfig [0 ]
8695
8796 @classmethod
88- def from_c (cls , repo , ptr ) :
97+ def from_c (cls , repo : 'GitRepositoryC' , ptr : 'GitConfigC' ) -> 'Config' :
8998 config = cls .__new__ (cls )
9099 config ._repo = repo
91100 config ._config = ptr
92101
93102 return config
94103
95- def __del__ (self ):
104+ def __del__ (self ) -> None :
96105 try :
97106 C .git_config_free (self ._config )
98107 except AttributeError :
99108 pass
100109
101- def _get (self , key ) :
110+ def _get (self , key : str | bytes ) -> tuple [ object , 'ConfigEntry' ] :
102111 key = str_to_bytes (key , 'key' )
103112
104113 entry = ffi .new ('git_config_entry **' )
105114 err = C .git_config_get_entry (entry , self ._config , key )
106115
107116 return err , ConfigEntry ._from_c (entry [0 ])
108117
109- def _get_entry (self , key ) :
118+ def _get_entry (self , key : str | bytes ) -> 'ConfigEntry' :
110119 err , entry = self ._get (key )
111120
112121 if err == C .GIT_ENOTFOUND :
@@ -306,8 +315,13 @@ def get_xdg_config():
306315class ConfigEntry :
307316 """An entry in a configuration object."""
308317
318+ _entry : 'GitConfigEntryC'
319+ iterator : ConfigIterator | None
320+
309321 @classmethod
310- def _from_c (cls , ptr , iterator = None ):
322+ def _from_c (
323+ cls , ptr : 'GitConfigEntryC' , iterator : ConfigIterator | None = None
324+ ) -> 'ConfigEntry' :
311325 """Builds the entry from a ``git_config_entry`` pointer.
312326
313327 ``iterator`` must be a ``ConfigIterator`` instance if the entry was
@@ -330,7 +344,7 @@ def _from_c(cls, ptr, iterator=None):
330344
331345 return entry
332346
333- def __del__ (self ):
347+ def __del__ (self ) -> None :
334348 if self .iterator is None :
335349 C .git_config_entry_free (self ._entry )
336350
@@ -340,24 +354,24 @@ def c_value(self):
340354 return self ._entry .value
341355
342356 @cached_property
343- def raw_name (self ):
357+ def raw_name (self ) -> bytes :
344358 return ffi .string (self ._entry .name )
345359
346360 @cached_property
347- def raw_value (self ):
361+ def raw_value (self ) -> bytes :
348362 return ffi .string (self .c_value )
349363
350364 @cached_property
351- def level (self ):
365+ def level (self ) -> int :
352366 """The entry's ``git_config_level_t`` value."""
353367 return self ._entry .level
354368
355369 @property
356- def name (self ):
370+ def name (self ) -> str :
357371 """The entry's name."""
358372 return self .raw_name .decode ('utf-8' )
359373
360374 @property
361- def value (self ):
375+ def value (self ) -> str :
362376 """The entry's value as a string."""
363377 return self .raw_value .decode ('utf-8' )
0 commit comments