Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc2f9f4e

Browse files
committed
Add types to config.py GitConfigParser ._assure_writable .add_section .read_only .get_value .get_values ._string_to_value ._value_to_string .add_value .rename_section
1 parentab69b9a commitc2f9f4e

File tree

2 files changed

+18
-16
lines changed

2 files changed

+18
-16
lines changed

‎git/config.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -667,12 +667,13 @@ def write(self) -> None:
667667
fp=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
671671
ifis_file_lockandself._lockisnotNone:# else raise Error?
672672
self._lock._obtain_lock()
673+
673674
ifnothasattr(fp,"seek"):
674-
self._file_or_files=cast(PathLike,self._file_or_files)
675-
withopen(self._file_or_files,"wb")asfp_open:
675+
fp=cast(PathLike,fp)
676+
withopen(fp,"wb")asfp_open:
676677
self._write(fp_open)
677678
else:
678679
fp=cast(IO,fp)
@@ -682,20 +683,22 @@ def write(self) -> None:
682683
fp.truncate()
683684
self._write(fp)
684685

685-
def_assure_writable(self,method_name):
686+
def_assure_writable(self,method_name:str)->None:
686687
ifself.read_only:
687688
raiseIOError("Cannot execute non-constant method %s.%s"% (self,method_name))
688689

689-
defadd_section(self,section):
690+
defadd_section(self,section:str)->None:
690691
"""Assures added options will stay in order"""
691692
returnsuper(GitConfigParser,self).add_section(section)
692693

693694
@property
694-
defread_only(self):
695+
defread_only(self)->bool:
695696
""":return: True if this instance may change the configuration file"""
696697
returnself._read_only
697698

698-
defget_value(self,section,option,default=None):
699+
defget_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

718721
returnself._string_to_value(valuestr)
719722

720-
defget_values(self,section,option,default=None):
723+
defget_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

740744
return [self._string_to_value(valuestr)forvaluestrinlst]
741745

742-
def_string_to_value(self,valuestr):
746+
def_string_to_value(self,valuestr:str)->Union[int,float,str,bool]:
743747
types= (int,float)
744748
fornumtypeintypes:
745749
try:
746750
val=numtype(valuestr)
747-
748751
# truncated value ?
749752
ifval!=float(valuestr):
750753
continue
751-
752754
returnval
753755
except (ValueError,TypeError):
754756
continue
@@ -768,14 +770,14 @@ def _string_to_value(self, valuestr):
768770

769771
returnvaluestr
770772

771-
def_value_to_string(self,value):
773+
def_value_to_string(self,value:Union[str,bytes,int,float,bool])->str:
772774
ifisinstance(value, (int,float,bool)):
773775
returnstr(value)
774776
returnforce_text(value)
775777

776778
@needs_values
777779
@set_dirty_and_flush_changes
778-
defset_value(self,section,option,value):
780+
defset_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-
defadd_value(self,section,option,value):
798+
defadd_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):
810812
self._sections[section].add(option,self._value_to_string(value))
811813
returnself
812814

813-
defrename_section(self,section,new_name):
815+
defrename_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

‎git/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
PathLike=Union[str,os.PathLike]
2121
elifsys.version_info[:2]>= (3,9):
2222
# os.PathLike only becomes subscriptable from Python 3.9 onwards
23-
PathLike=Union[str,os.PathLike[str]]
23+
PathLike=Union[str,'os.PathLike[str]']# forward ref as pylance complains unless editing with py3.9+
2424

2525
TBD=Any
2626

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp