@@ -667,12 +667,13 @@ def write(self) -> None:
667667fp = self ._file_or_files
668668
669669# we have a physical file on disk, so get a lock
670- is_file_lock = isinstance (fp , (str , IOBase ))
670+ is_file_lock = isinstance (fp , (str , IOBase ))# can't use Pathlike until 3.5 dropped
671671if is_file_lock and self ._lock is not None : # else raise Error?
672672self ._lock ._obtain_lock ()
673+
673674if not hasattr (fp , "seek" ):
674- self . _file_or_files = cast (PathLike , self . _file_or_files )
675- with open (self . _file_or_files , "wb" ) as fp_open :
675+ fp = cast (PathLike , fp )
676+ with open (fp , "wb" ) as fp_open :
676677self ._write (fp_open )
677678else :
678679fp = cast (IO , fp )
@@ -682,20 +683,22 @@ def write(self) -> None:
682683fp .truncate ()
683684self ._write (fp )
684685
685- def _assure_writable (self , method_name ) :
686+ def _assure_writable (self , method_name : str ) -> None :
686687if self .read_only :
687688raise IOError ("Cannot execute non-constant method %s.%s" % (self , method_name ))
688689
689- def add_section (self , section ) :
690+ def add_section (self , section : str ) -> None :
690691"""Assures added options will stay in order"""
691692return super (GitConfigParser , self ).add_section (section )
692693
693694@property
694- def read_only (self ):
695+ def read_only (self )-> bool :
695696""":return: True if this instance may change the configuration file"""
696697return self ._read_only
697698
698- def get_value (self , section , option , default = None ):
699+ def get_value (self , section : str , option : str , default : Union [int , float , str , bool , None ] = None
700+ ) -> Union [int , float , str , bool ]:
701+ # can default or return type include bool?
699702"""Get an option's value.
700703
701704 If multiple values are specified for this option in the section, the
@@ -717,7 +720,8 @@ def get_value(self, section, option, default=None):
717720
718721return self ._string_to_value (valuestr )
719722
720- def get_values (self , section , option , default = None ):
723+ def get_values (self , section : str , option : str , default : Union [int , float , str , bool , None ] = None
724+ ) -> List [Union [int , float , str , bool ]]:
721725"""Get an option's values.
722726
723727 If multiple values are specified for this option in the section, all are
@@ -739,16 +743,14 @@ def get_values(self, section, option, default=None):
739743
740744return [self ._string_to_value (valuestr ) for valuestr in lst ]
741745
742- def _string_to_value (self , valuestr ) :
746+ def _string_to_value (self , valuestr : str ) -> Union [ int , float , str , bool ] :
743747types = (int , float )
744748for numtype in types :
745749try :
746750val = numtype (valuestr )
747-
748751# truncated value ?
749752if val != float (valuestr ):
750753continue
751-
752754return val
753755except (ValueError , TypeError ):
754756continue
@@ -768,14 +770,14 @@ def _string_to_value(self, valuestr):
768770
769771return valuestr
770772
771- def _value_to_string (self , value ) :
773+ def _value_to_string (self , value : Union [ str , bytes , int , float , bool ]) -> str :
772774if isinstance (value , (int , float , bool )):
773775return str (value )
774776return force_text (value )
775777
776778@needs_values
777779@set_dirty_and_flush_changes
778- def set_value (self , section , option , value ) :
780+ def set_value (self , section : str , option : str , value : Union [ str , bytes , int , float , bool ]) -> 'GitConfigParser' :
779781"""Sets the given option in section to the given value.
780782 It will create the section if required, and will not throw as opposed to the default
781783 ConfigParser 'set' method.
@@ -793,7 +795,7 @@ def set_value(self, section, option, value):
793795
794796@needs_values
795797@set_dirty_and_flush_changes
796- def add_value (self , section , option , value ) :
798+ def add_value (self , section : str , option : str , value : Union [ str , bytes , int , float , bool ]) -> 'GitConfigParser' :
797799"""Adds a value for the given option in section.
798800 It will create the section if required, and will not throw as opposed to the default
799801 ConfigParser 'set' method. The value becomes the new value of the option as returned
@@ -810,7 +812,7 @@ def add_value(self, section, option, value):
810812self ._sections [section ].add (option , self ._value_to_string (value ))
811813return self
812814
813- def rename_section (self , section , new_name ) :
815+ def rename_section (self , section : str , new_name : str ) -> 'GitConfigParser' :
814816"""rename the given section to new_name
815817 :raise ValueError: if section doesn't exit
816818 :raise ValueError: if a section with new_name does already exist
0 commit comments