77#
88### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99"""Fileholder class"""
10+ from __future__ import annotations
11+
12+ import io
13+ import typing as ty
1014from copy import copy
1115
1216from .openers import ImageOpener
@@ -19,7 +23,12 @@ class FileHolderError(Exception):
1923class FileHolder :
2024 """class to contain filename, fileobj and file position"""
2125
22- def __init__ (self , filename = None , fileobj = None , pos = 0 ):
26+ def __init__ (
27+ self ,
28+ filename : str | None = None ,
29+ fileobj : io .IOBase | None = None ,
30+ pos : int = 0 ,
31+ ):
2332 """Initialize FileHolder instance
2433
2534 Parameters
@@ -37,7 +46,7 @@ def __init__(self, filename=None, fileobj=None, pos=0):
3746 self .fileobj = fileobj
3847 self .pos = pos
3948
40- def get_prepare_fileobj (self , * args , ** kwargs ):
49+ def get_prepare_fileobj (self , * args , ** kwargs ) -> ImageOpener :
4150 """Return fileobj if present, or return fileobj from filename
4251
4352 Set position to that given in self.pos
@@ -69,7 +78,7 @@ def get_prepare_fileobj(self, *args, **kwargs):
6978 raise FileHolderError ('No filename or fileobj present' )
7079 return obj
7180
72- def same_file_as (self , other ) :
81+ def same_file_as (self , other : FileHolder ) -> bool :
7382 """Test if `self` refers to same files / fileobj as `other`
7483
7584 Parameters
@@ -86,12 +95,15 @@ def same_file_as(self, other):
8695 return (self .filename == other .filename ) and (self .fileobj == other .fileobj )
8796
8897 @property
89- def file_like (self ):
98+ def file_like (self ) -> str | io . IOBase | None :
9099 """Return ``self.fileobj`` if not None, otherwise ``self.filename``"""
91100 return self .fileobj if self .fileobj is not None else self .filename
92101
93102
94- def copy_file_map (file_map ):
103+ FileMap = ty .Mapping [str , FileHolder ]
104+
105+
106+ def copy_file_map (file_map : FileMap ) -> FileMap :
95107 r"""Copy mapping of fileholders given by `file_map`
96108
97109 Parameters
@@ -105,7 +117,4 @@ def copy_file_map(file_map):
105117 Copy of `file_map`, using shallow copy of ``FileHolder``\s
106118
107119 """
108- fm_copy = {}
109- for key , fh in file_map .items ():
110- fm_copy [key ] = copy (fh )
111- return fm_copy
120+ return {key : copy (fh ) for key , fh in file_map .items ()}
0 commit comments