22
22
23
23
__all__ = ('SinglefileData' ,)
24
24
25
+ FilePath = t .Union [str , pathlib .PurePosixPath ]
26
+
25
27
26
28
class SinglefileData (Data ):
27
29
"""Data class that can be used to store a single file in its repository."""
@@ -37,7 +39,9 @@ def from_string(cls, content: str, filename: str | pathlib.Path | None = None, *
37
39
"""
38
40
return cls (io .StringIO (content ), filename , ** kwargs )
39
41
40
- def __init__ (self , file : str | t .IO , filename : str | pathlib .Path | None = None , ** kwargs : t .Any ) -> None :
42
+ def __init__ (
43
+ self , file : str | pathlib .Path | t .IO , filename : str | pathlib .Path | None = None , ** kwargs : t .Any
44
+ ) -> None :
41
45
"""Construct a new instance and set the contents to that of the file.
42
46
43
47
:param file: an absolute filepath or filelike object whose contents to copy.
@@ -60,26 +64,30 @@ def filename(self) -> str:
60
64
61
65
@t .overload
62
66
@contextlib .contextmanager
63
- def open (self , path : str , mode : t .Literal ['r' ]) -> t .Iterator [t .TextIO ]:
67
+ def open (self , path : FilePath , mode : t .Literal ['r' ] = ... ) -> t .Iterator [t .TextIO ]:
64
68
...
65
69
66
70
@t .overload
67
71
@contextlib .contextmanager
68
- def open (self , path : None , mode : t .Literal ['r ' ]) -> t .Iterator [t .TextIO ]:
72
+ def open (self , path : FilePath , mode : t .Literal ['rb ' ]) -> t .Iterator [t .BinaryIO ]:
69
73
...
70
74
71
75
@t .overload
72
76
@contextlib .contextmanager
73
- def open (self , path : str , mode : t .Literal ['rb' ]) -> t .Iterator [t .BinaryIO ]:
77
+ def open ( # type: ignore[overload-overlap]
78
+ self , path : None = None , mode : t .Literal ['r' ] = ...
79
+ ) -> t .Iterator [t .TextIO ]:
74
80
...
75
81
76
82
@t .overload
77
83
@contextlib .contextmanager
78
- def open (self , path : None , mode : t .Literal ['rb' ]) -> t .Iterator [t .BinaryIO ]:
84
+ def open (self , path : None = None , mode : t .Literal ['rb' ] = ... ) -> t .Iterator [t .BinaryIO ]:
79
85
...
80
86
81
87
@contextlib .contextmanager
82
- def open (self , path : str | None = None , mode : t .Literal ['r' , 'rb' ] = 'r' ) -> t .Iterator [t .BinaryIO | t .TextIO ]:
88
+ def open (self ,
89
+ path : FilePath | None = None ,
90
+ mode : t .Literal ['r' , 'rb' ] = 'r' ) -> t .Iterator [t .BinaryIO ] | t .Iterator [t .TextIO ]:
83
91
"""Return an open file handle to the content of this data node.
84
92
85
93
:param path: the relative path of the object within the repository.
@@ -113,7 +121,7 @@ def get_content(self, mode: str = 'r') -> str | bytes:
113
121
with self .open (mode = mode ) as handle : # type: ignore[call-overload]
114
122
return handle .read ()
115
123
116
- def set_file (self , file : str | t .IO , filename : str | pathlib .Path | None = None ) -> None :
124
+ def set_file (self , file : str | pathlib . Path | t .IO , filename : str | pathlib .Path | None = None ) -> None :
117
125
"""Store the content of the file in the node's repository, deleting any other existing objects.
118
126
119
127
:param file: an absolute filepath or filelike object whose contents to copy
0 commit comments