/ lib / numpy / lib / npyio.pyi
npyio.pyi
  1  import os
  2  import sys
  3  import zipfile
  4  import types
  5  from re import Pattern
  6  from collections.abc import Collection, Mapping, Iterator, Sequence, Callable, Iterable
  7  from typing import (
  8      Literal as L,
  9      Any,
 10      TypeVar,
 11      Generic,
 12      IO,
 13      overload,
 14      Protocol,
 15  )
 16  
 17  from numpy import (
 18      DataSource as DataSource,
 19      ndarray,
 20      recarray,
 21      dtype,
 22      generic,
 23      float64,
 24      void,
 25      record,
 26  )
 27  
 28  from numpy.ma.mrecords import MaskedRecords
 29  from numpy._typing import (
 30      ArrayLike,
 31      DTypeLike,
 32      NDArray,
 33      _DTypeLike,
 34      _SupportsArrayFunc,
 35  )
 36  
 37  from numpy.core.multiarray import (
 38      packbits as packbits,
 39      unpackbits as unpackbits,
 40  )
 41  
 42  _T = TypeVar("_T")
 43  _T_contra = TypeVar("_T_contra", contravariant=True)
 44  _T_co = TypeVar("_T_co", covariant=True)
 45  _SCT = TypeVar("_SCT", bound=generic)
 46  _CharType_co = TypeVar("_CharType_co", str, bytes, covariant=True)
 47  _CharType_contra = TypeVar("_CharType_contra", str, bytes, contravariant=True)
 48  
 49  class _SupportsGetItem(Protocol[_T_contra, _T_co]):
 50      def __getitem__(self, key: _T_contra, /) -> _T_co: ...
 51  
 52  class _SupportsRead(Protocol[_CharType_co]):
 53      def read(self) -> _CharType_co: ...
 54  
 55  class _SupportsReadSeek(Protocol[_CharType_co]):
 56      def read(self, n: int, /) -> _CharType_co: ...
 57      def seek(self, offset: int, whence: int, /) -> object: ...
 58  
 59  class _SupportsWrite(Protocol[_CharType_contra]):
 60      def write(self, s: _CharType_contra, /) -> object: ...
 61  
 62  __all__: list[str]
 63  
 64  class BagObj(Generic[_T_co]):
 65      def __init__(self, obj: _SupportsGetItem[str, _T_co]) -> None: ...
 66      def __getattribute__(self, key: str) -> _T_co: ...
 67      def __dir__(self) -> list[str]: ...
 68  
 69  class NpzFile(Mapping[str, NDArray[Any]]):
 70      zip: zipfile.ZipFile
 71      fid: None | IO[str]
 72      files: list[str]
 73      allow_pickle: bool
 74      pickle_kwargs: None | Mapping[str, Any]
 75      # Represent `f` as a mutable property so we can access the type of `self`
 76      @property
 77      def f(self: _T) -> BagObj[_T]: ...
 78      @f.setter
 79      def f(self: _T, value: BagObj[_T]) -> None: ...
 80      def __init__(
 81          self,
 82          fid: IO[str],
 83          own_fid: bool = ...,
 84          allow_pickle: bool = ...,
 85          pickle_kwargs: None | Mapping[str, Any] = ...,
 86      ) -> None: ...
 87      def __enter__(self: _T) -> _T: ...
 88      def __exit__(
 89          self,
 90          exc_type: None | type[BaseException],
 91          exc_value: None | BaseException,
 92          traceback: None | types.TracebackType,
 93          /,
 94      ) -> None: ...
 95      def close(self) -> None: ...
 96      def __del__(self) -> None: ...
 97      def __iter__(self) -> Iterator[str]: ...
 98      def __len__(self) -> int: ...
 99      def __getitem__(self, key: str) -> NDArray[Any]: ...
100  
101  # NOTE: Returns a `NpzFile` if file is a zip file;
102  # returns an `ndarray`/`memmap` otherwise
103  def load(
104      file: str | bytes | os.PathLike[Any] | _SupportsReadSeek[bytes],
105      mmap_mode: L[None, "r+", "r", "w+", "c"] = ...,
106      allow_pickle: bool = ...,
107      fix_imports: bool = ...,
108      encoding: L["ASCII", "latin1", "bytes"] = ...,
109  ) -> Any: ...
110  
111  def save(
112      file: str | os.PathLike[str] | _SupportsWrite[bytes],
113      arr: ArrayLike,
114      allow_pickle: bool = ...,
115      fix_imports: bool = ...,
116  ) -> None: ...
117  
118  def savez(
119      file: str | os.PathLike[str] | _SupportsWrite[bytes],
120      *args: ArrayLike,
121      **kwds: ArrayLike,
122  ) -> None: ...
123  
124  def savez_compressed(
125      file: str | os.PathLike[str] | _SupportsWrite[bytes],
126      *args: ArrayLike,
127      **kwds: ArrayLike,
128  ) -> None: ...
129  
130  # File-like objects only have to implement `__iter__` and,
131  # optionally, `encoding`
132  @overload
133  def loadtxt(
134      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
135      dtype: None = ...,
136      comments: None | str | Sequence[str] = ...,
137      delimiter: None | str = ...,
138      converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
139      skiprows: int = ...,
140      usecols: int | Sequence[int] = ...,
141      unpack: bool = ...,
142      ndmin: L[0, 1, 2] = ...,
143      encoding: None | str = ...,
144      max_rows: None | int = ...,
145      *,
146      quotechar: None | str = ...,
147      like: None | _SupportsArrayFunc = ...
148  ) -> NDArray[float64]: ...
149  @overload
150  def loadtxt(
151      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
152      dtype: _DTypeLike[_SCT],
153      comments: None | str | Sequence[str] = ...,
154      delimiter: None | str = ...,
155      converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
156      skiprows: int = ...,
157      usecols: int | Sequence[int] = ...,
158      unpack: bool = ...,
159      ndmin: L[0, 1, 2] = ...,
160      encoding: None | str = ...,
161      max_rows: None | int = ...,
162      *,
163      quotechar: None | str = ...,
164      like: None | _SupportsArrayFunc = ...
165  ) -> NDArray[_SCT]: ...
166  @overload
167  def loadtxt(
168      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
169      dtype: DTypeLike,
170      comments: None | str | Sequence[str] = ...,
171      delimiter: None | str = ...,
172      converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
173      skiprows: int = ...,
174      usecols: int | Sequence[int] = ...,
175      unpack: bool = ...,
176      ndmin: L[0, 1, 2] = ...,
177      encoding: None | str = ...,
178      max_rows: None | int = ...,
179      *,
180      quotechar: None | str = ...,
181      like: None | _SupportsArrayFunc = ...
182  ) -> NDArray[Any]: ...
183  
184  def savetxt(
185      fname: str | os.PathLike[str] | _SupportsWrite[str] | _SupportsWrite[bytes],
186      X: ArrayLike,
187      fmt: str | Sequence[str] = ...,
188      delimiter: str = ...,
189      newline: str = ...,
190      header: str = ...,
191      footer: str = ...,
192      comments: str = ...,
193      encoding: None | str = ...,
194  ) -> None: ...
195  
196  @overload
197  def fromregex(
198      file: str | os.PathLike[str] | _SupportsRead[str] | _SupportsRead[bytes],
199      regexp: str | bytes | Pattern[Any],
200      dtype: _DTypeLike[_SCT],
201      encoding: None | str = ...
202  ) -> NDArray[_SCT]: ...
203  @overload
204  def fromregex(
205      file: str | os.PathLike[str] | _SupportsRead[str] | _SupportsRead[bytes],
206      regexp: str | bytes | Pattern[Any],
207      dtype: DTypeLike,
208      encoding: None | str = ...
209  ) -> NDArray[Any]: ...
210  
211  @overload
212  def genfromtxt(
213      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
214      dtype: None = ...,
215      comments: str = ...,
216      delimiter: None | str | int | Iterable[int] = ...,
217      skip_header: int = ...,
218      skip_footer: int = ...,
219      converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
220      missing_values: Any = ...,
221      filling_values: Any = ...,
222      usecols: None | Sequence[int] = ...,
223      names: L[None, True] | str | Collection[str] = ...,
224      excludelist: None | Sequence[str] = ...,
225      deletechars: str = ...,
226      replace_space: str = ...,
227      autostrip: bool = ...,
228      case_sensitive: bool | L['upper', 'lower'] = ...,
229      defaultfmt: str = ...,
230      unpack: None | bool = ...,
231      usemask: bool = ...,
232      loose: bool = ...,
233      invalid_raise: bool = ...,
234      max_rows: None | int = ...,
235      encoding: str = ...,
236      *,
237      ndmin: L[0, 1, 2] = ...,
238      like: None | _SupportsArrayFunc = ...,
239  ) -> NDArray[float64]: ...
240  @overload
241  def genfromtxt(
242      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
243      dtype: _DTypeLike[_SCT],
244      comments: str = ...,
245      delimiter: None | str | int | Iterable[int] = ...,
246      skip_header: int = ...,
247      skip_footer: int = ...,
248      converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
249      missing_values: Any = ...,
250      filling_values: Any = ...,
251      usecols: None | Sequence[int] = ...,
252      names: L[None, True] | str | Collection[str] = ...,
253      excludelist: None | Sequence[str] = ...,
254      deletechars: str = ...,
255      replace_space: str = ...,
256      autostrip: bool = ...,
257      case_sensitive: bool | L['upper', 'lower'] = ...,
258      defaultfmt: str = ...,
259      unpack: None | bool = ...,
260      usemask: bool = ...,
261      loose: bool = ...,
262      invalid_raise: bool = ...,
263      max_rows: None | int = ...,
264      encoding: str = ...,
265      *,
266      ndmin: L[0, 1, 2] = ...,
267      like: None | _SupportsArrayFunc = ...,
268  ) -> NDArray[_SCT]: ...
269  @overload
270  def genfromtxt(
271      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
272      dtype: DTypeLike,
273      comments: str = ...,
274      delimiter: None | str | int | Iterable[int] = ...,
275      skip_header: int = ...,
276      skip_footer: int = ...,
277      converters: None | Mapping[int | str, Callable[[str], Any]] = ...,
278      missing_values: Any = ...,
279      filling_values: Any = ...,
280      usecols: None | Sequence[int] = ...,
281      names: L[None, True] | str | Collection[str] = ...,
282      excludelist: None | Sequence[str] = ...,
283      deletechars: str = ...,
284      replace_space: str = ...,
285      autostrip: bool = ...,
286      case_sensitive: bool | L['upper', 'lower'] = ...,
287      defaultfmt: str = ...,
288      unpack: None | bool = ...,
289      usemask: bool = ...,
290      loose: bool = ...,
291      invalid_raise: bool = ...,
292      max_rows: None | int = ...,
293      encoding: str = ...,
294      *,
295      ndmin: L[0, 1, 2] = ...,
296      like: None | _SupportsArrayFunc = ...,
297  ) -> NDArray[Any]: ...
298  
299  @overload
300  def recfromtxt(
301      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
302      *,
303      usemask: L[False] = ...,
304      **kwargs: Any,
305  ) -> recarray[Any, dtype[record]]: ...
306  @overload
307  def recfromtxt(
308      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
309      *,
310      usemask: L[True],
311      **kwargs: Any,
312  ) -> MaskedRecords[Any, dtype[void]]: ...
313  
314  @overload
315  def recfromcsv(
316      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
317      *,
318      usemask: L[False] = ...,
319      **kwargs: Any,
320  ) -> recarray[Any, dtype[record]]: ...
321  @overload
322  def recfromcsv(
323      fname: str | os.PathLike[str] | Iterable[str] | Iterable[bytes],
324      *,
325      usemask: L[True],
326      **kwargs: Any,
327  ) -> MaskedRecords[Any, dtype[void]]: ...