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