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