99import abc
1010from functools import wraps
1111import inspect
12- from io import IOBase
12+ from io import BufferedReader , IOBase
1313import logging
1414import os
1515import re
@@ -325,7 +325,7 @@ def __init__(self, file_or_files: Union[None, PathLike, IO, Sequence[Union[PathL
325325 def _acquire_lock (self ) -> None :
326326 if not self ._read_only :
327327 if not self ._lock :
328- if isinstance (self ._file_or_files , (tuple , list , Sequence )):
328+ if isinstance (self ._file_or_files , (tuple , list )):
329329 raise ValueError (
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 return optionstr
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 return paths
536536
537- def read (self ):
537+ def read (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 if self ._is_initialized :
544- return
544+ return None
545545 self ._is_initialized = True
546546
547- if not isinstance (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+ if isinstance (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+ elif not isinstance (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 while files_to_read :
556559 file_path = files_to_read .pop (0 )
557- fp = file_path
558560 file_ok = False
559561
560- if hasattr (fp , "seek" ):
561- self ._read (fp , fp .name )
562+ if hasattr (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 with open (file_path , 'rb' ) as fp :
566571 file_ok = True
@@ -578,6 +583,7 @@ def read(self):
578583 if not file_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 assert osp .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