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

Commitc6e458c

Browse files
committed
Add types to config.py GitConfigParser .read()
1 parent94b7ece commitc6e458c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

‎git/config.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
importabc
1010
fromfunctoolsimportwraps
1111
importinspect
12-
fromioimportIOBase
12+
fromioimportBufferedReader,IOBase
1313
importlogging
1414
importos
1515
importre
@@ -325,7 +325,7 @@ def __init__(self, file_or_files: Union[None, PathLike, IO, Sequence[Union[PathL
325325
def_acquire_lock(self)->None:
326326
ifnotself._read_only:
327327
ifnotself._lock:
328-
ifisinstance(self._file_or_files, (tuple,list,Sequence)):
328+
ifisinstance(self._file_or_files, (tuple,list)):
329329
raiseValueError(
330330
"Write-ConfigParsers can operate on a single file only, multiple files have been passed")
331331
# END single file check
@@ -382,7 +382,7 @@ def optionxform(self, optionstr: str) -> str:
382382
"""Do not transform options in any way when writing"""
383383
returnoptionstr
384384

385-
def_read(self,fp:IO[bytes],fpname:str)->None:
385+
def_read(self,fp:Union[BufferedReader,IO[bytes]],fpname:str)->None:
386386
"""A direct copy of the py2.4 version of the super class's _read method
387387
to assure it uses ordered dicts. Had to change one line to make it work.
388388
@@ -534,33 +534,38 @@ def _included_paths(self) -> List[Tuple[str, str]]:
534534

535535
returnpaths
536536

537-
defread(self):
537+
defread(self)->None:
538538
"""Reads the data stored in the files we have been initialized with. It will
539539
ignore files that cannot be read, possibly leaving an empty configuration
540540
541541
:return: Nothing
542542
:raise IOError: if a file cannot be handled"""
543543
ifself._is_initialized:
544-
return
544+
returnNone
545545
self._is_initialized=True
546546

547-
ifnotisinstance(self._file_or_files, (tuple,list)):
548-
files_to_read= [self._file_or_files]
547+
files_to_read= [""]# type: List[Union[PathLike, IO]] ## just for types until 3.5 dropped
548+
ifisinstance(self._file_or_files, (str)):# replace with PathLike once 3.5 dropped
549+
files_to_read= [self._file_or_files]# for str, as str is a type of Sequence
550+
elifnotisinstance(self._file_or_files, (tuple,list,Sequence)):
551+
files_to_read= [self._file_or_files]# for IO or Path
549552
else:
550-
files_to_read=list(self._file_or_files)
553+
files_to_read=list(self._file_or_files)# for lists or tuples
551554
# end assure we have a copy of the paths to handle
552555

553556
seen=set(files_to_read)
554557
num_read_include_files=0
555558
whilefiles_to_read:
556559
file_path=files_to_read.pop(0)
557-
fp=file_path
558560
file_ok=False
559561

560-
ifhasattr(fp,"seek"):
561-
self._read(fp,fp.name)
562+
ifhasattr(file_path,"seek"):
563+
# must be a file objectfile-object
564+
file_path=cast(IO[bytes],file_path)# replace with assert to narrow type, once sure
565+
self._read(file_path,file_path.name)
562566
else:
563567
# assume a path if it is not a file-object
568+
file_path=cast(PathLike,file_path)
564569
try:
565570
withopen(file_path,'rb')asfp:
566571
file_ok=True
@@ -578,6 +583,7 @@ def read(self):
578583
ifnotfile_ok:
579584
continue
580585
# end ignore relative paths if we don't know the configuration file path
586+
file_path=cast(PathLike,file_path)
581587
assertosp.isabs(file_path),"Need absolute paths to be sure our cycle checks will work"
582588
include_path=osp.join(osp.dirname(file_path),include_path)
583589
# end make include path absolute

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp