Skip to content

Commit ece10ac

Browse files
committed
TYP: Annotate fileholders
1 parent 8c43ffe commit ece10ac

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

nibabel/filebasedimages.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
from typing import Type
1717
from urllib import request
1818

19-
from .fileholders import FileHolder
19+
from .fileholders import FileHolder, FileMap
2020
from .filename_parser import TypesFilenamesError, splitext_addext, types_filenames
2121
from .openers import ImageOpener
2222

2323
FileSpec = ty.Union[str, os.PathLike]
24-
FileMap = ty.Mapping[str, FileHolder]
2524
FileSniff = ty.Tuple[bytes, str]
2625

2726
ImgT = ty.TypeVar('ImgT', bound='FileBasedImage')

nibabel/fileholders.py

+18-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
#
88
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
99
"""Fileholder class"""
10+
from __future__ import annotations
11+
12+
import io
13+
import typing as ty
1014
from copy import copy
1115

1216
from .openers import ImageOpener
@@ -19,7 +23,12 @@ class FileHolderError(Exception):
1923
class 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

Comments
 (0)