7
7
#
8
8
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
9
9
"""Fileholder class"""
10
+ from __future__ import annotations
11
+
12
+ import io
10
13
from copy import copy
11
14
12
15
from .openers import ImageOpener
@@ -19,7 +22,12 @@ class FileHolderError(Exception):
19
22
class FileHolder :
20
23
"""class to contain filename, fileobj and file position"""
21
24
22
- def __init__ (self , filename = None , fileobj = None , pos = 0 ):
25
+ def __init__ (
26
+ self ,
27
+ filename : str | None = None ,
28
+ fileobj : io .IOBase | None = None ,
29
+ pos : int = 0 ,
30
+ ):
23
31
"""Initialize FileHolder instance
24
32
25
33
Parameters
@@ -37,7 +45,7 @@ def __init__(self, filename=None, fileobj=None, pos=0):
37
45
self .fileobj = fileobj
38
46
self .pos = pos
39
47
40
- def get_prepare_fileobj (self , * args , ** kwargs ):
48
+ def get_prepare_fileobj (self , * args , ** kwargs ) -> ImageOpener :
41
49
"""Return fileobj if present, or return fileobj from filename
42
50
43
51
Set position to that given in self.pos
@@ -69,7 +77,7 @@ def get_prepare_fileobj(self, *args, **kwargs):
69
77
raise FileHolderError ('No filename or fileobj present' )
70
78
return obj
71
79
72
- def same_file_as (self , other ) :
80
+ def same_file_as (self , other : FileHolder ) -> bool :
73
81
"""Test if `self` refers to same files / fileobj as `other`
74
82
75
83
Parameters
@@ -86,12 +94,15 @@ def same_file_as(self, other):
86
94
return (self .filename == other .filename ) and (self .fileobj == other .fileobj )
87
95
88
96
@property
89
- def file_like (self ):
97
+ def file_like (self ) -> str | io . IOBase | None :
90
98
"""Return ``self.fileobj`` if not None, otherwise ``self.filename``"""
91
99
return self .fileobj if self .fileobj is not None else self .filename
92
100
93
101
94
- def copy_file_map (file_map ):
102
+ FileMap = dict [str , FileHolder ]
103
+
104
+
105
+ def copy_file_map (file_map : FileMap ) -> FileMap :
95
106
r"""Copy mapping of fileholders given by `file_map`
96
107
97
108
Parameters
@@ -105,7 +116,4 @@ def copy_file_map(file_map):
105
116
Copy of `file_map`, using shallow copy of ``FileHolder``\s
106
117
107
118
"""
108
- fm_copy = {}
109
- for key , fh in file_map .items ():
110
- fm_copy [key ] = copy (fh )
111
- return fm_copy
119
+ return {key : copy (fh ) for key , fh in file_map .items ()}
0 commit comments