Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bff4681

Browse files
authored
Merge pull request #25655 from ksunden/file_io_cleanup
Clean up FileIO type hints
2 parents 2771894 + 358973d commit bff4681

File tree

6 files changed

+46
-29
lines changed

6 files changed

+46
-29
lines changed

lib/matplotlib/backend_bases.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from enum import Enum, IntEnum
22
import os
3-
import io
43
from matplotlib import (
54
cbook,
65
colors,
@@ -27,7 +26,7 @@ from matplotlib.text import Text
2726
from matplotlib.transforms import Affine2D, Transform, TransformedPath, Bbox
2827

2928
from collections.abc import Callable, Iterable, Sequence
30-
from typing import Any, Literal, NamedTuple, TypeVar
29+
from typing import Any, IO, Literal, NamedTuple, TypeVar
3130
from numpy.typing import ArrayLike
3231
from .typing import ColorType, LineStyleType
3332

@@ -347,7 +346,7 @@ class FigureCanvasBase:
347346
def get_supported_filetypes_grouped(cls) -> dict[str, list[str]]: ...
348347
def print_figure(
349348
self,
350-
filename: str | os.PathLike | io.FileIO,
349+
filename: str | os.PathLike | IO,
351350
dpi: float | None = ...,
352351
facecolor: ColorType | Literal["auto"] | None = ...,
353352
edgecolor: ColorType | Literal["auto"] | None = ...,

lib/matplotlib/cbook.pyi

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import collections.abc
22
from collections.abc import Callable, Collection, Generator, Iterable, Iterator
33
import contextlib
4-
import io
54
import os
65
from pathlib import Path
76

@@ -12,7 +11,9 @@ from numpy.typing import ArrayLike
1211

1312
from typing import (
1413
Any,
14+
ContextManager,
1515
Generic,
16+
IO,
1617
Literal,
1718
TypeVar,
1819
overload,
@@ -42,35 +43,50 @@ class silent_list(list[_T]):
4243
def strip_math(s: str) -> str: ...
4344
def is_writable_file_like(obj: Any) -> bool: ...
4445
def file_requires_unicode(x: Any) -> bool: ...
46+
@overload
4547
def to_filehandle(
46-
fname: str | os.PathLike | io.FileIO,
48+
fname: str | os.PathLike | IO,
4749
flag: str = ...,
48-
return_opened: bool = ...,
50+
return_opened: Literal[False] = ...,
51+
encoding: str | None = ...,
52+
) -> IO: ...
53+
@overload
54+
def to_filehandle(
55+
fname: str | os.PathLike | IO,
56+
flag: str,
57+
return_opened: Literal[True],
58+
encoding: str | None = ...,
59+
) -> tuple[IO, bool]: ...
60+
@overload
61+
def to_filehandle(
62+
fname: str | os.PathLike | IO,
63+
*, # if flag given, will match previous sig
64+
return_opened: Literal[True],
4965
encoding: str | None = ...,
50-
) -> io.FileIO: ...
66+
) -> tuple[IO, bool]: ...
5167
def open_file_cm(
52-
path_or_file: str | os.PathLike | io.FileIO,
68+
path_or_file: str | os.PathLike | IO,
5369
mode: str = ...,
5470
encoding: str | None = ...,
55-
): ...
71+
) -> ContextManager[IO]: ...
5672
def is_scalar_or_string(val: Any) -> bool: ...
5773
@overload
5874
def get_sample_data(
59-
fname: str | os.PathLike | io.FileIO,
75+
fname: str | os.PathLike,
6076
asfileobj: Literal[True] = ...,
6177
*,
6278
np_load: Literal[True]
6379
) -> np.ndarray: ...
6480
@overload
6581
def get_sample_data(
66-
fname: str | os.PathLike | io.FileIO,
82+
fname: str | os.PathLike,
6783
asfileobj: Literal[True] = ...,
6884
*,
6985
np_load: Literal[False] = ...
70-
) -> io.FileIO: ...
86+
) -> IO: ...
7187
@overload
7288
def get_sample_data(
73-
fname: str | os.PathLike | io.FileIO,
89+
fname: str | os.PathLike,
7490
asfileobj: Literal[False],
7591
*,
7692
np_load: bool = ...
@@ -96,7 +112,7 @@ class Stack(Generic[_T]):
96112

97113
def safe_masked_invalid(x: ArrayLike, copy: bool = ...) -> np.ndarray: ...
98114
def print_cycles(
99-
objects: Iterable[Any], outstream: io.FileIO = ..., show_progress: bool = ...
115+
objects: Iterable[Any], outstream: IO = ..., show_progress: bool = ...
100116
) -> None: ...
101117

102118
class Grouper(Generic[_T]):

lib/matplotlib/figure.pyi

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import os
32

43
from matplotlib import backend_bases, projections
@@ -40,7 +39,7 @@ import numpy as np
4039
from numpy.typing import ArrayLike
4140

4241
from collections.abc import Callable, Iterable
43-
from typing import Any, Literal, overload
42+
from typing import Any, IO, Literal, overload
4443
from .typing import ColorType, HashableList
4544

4645
class SubplotParams:
@@ -372,7 +371,7 @@ class Figure(FigureBase):
372371
def add_axobserver(self, func: Callable[[Figure], Any]): ...
373372
def savefig(
374373
self,
375-
fname: str | os.PathLike | io.FileIO,
374+
fname: str | os.PathLike | IO,
376375
*,
377376
transparent: bool | None = ...,
378377
**kwargs

lib/matplotlib/image.pyi

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ from matplotlib.transforms import (
2121
)
2222

2323
from collections.abc import Sequence
24-
from typing import Any, Literal
24+
from typing import Any, BinaryIO, Literal
2525
import numpy as np
2626
from numpy.typing import ArrayLike
2727

@@ -76,7 +76,7 @@ class _ImageBase(martist.Artist, cm.ScalarMappable):
7676
self, renderer: RendererBase, magnification: float = ..., unsampled: bool = ...
7777
) -> tuple[np.ndarray, float, float, Affine2D]: ...
7878
def draw(self, renderer: RendererBase, *args, **kwargs) -> None: ...
79-
def write_png(self, fname: str | pathlib.Path | io.FileIO) -> None: ...
79+
def write_png(self, fname: str | pathlib.Path | BinaryIO) -> None: ...
8080
def set_data(self, A: ArrayLike | None) -> None: ...
8181
def set_array(self, A: ArrayLike | None) -> None: ...
8282
def get_shape(self) -> tuple[int, int, int]: ...
@@ -176,9 +176,11 @@ class BboxImage(_ImageBase):
176176
) -> None: ...
177177
def get_window_extent(self, renderer: RendererBase | None = ...): ...
178178

179-
def imread(fname: str | io.FileIO, format: str | None = ...) -> np.ndarray: ...
179+
def imread(
180+
fname: str | pathlib.Path | BinaryIO, format: str | None = ...
181+
) -> np.ndarray: ...
180182
def imsave(
181-
fname: str | os.PathLike | io.FileIO,
183+
fname: str | os.PathLike | BinaryIO,
182184
arr: ArrayLike,
183185
vmin: float | None = ...,
184186
vmax: float | None = ...,
@@ -192,8 +194,8 @@ def imsave(
192194
) -> None: ...
193195
def pil_to_array(pilImage: PIL.Image.Image) -> np.ndarray: ...
194196
def thumbnail(
195-
infile: str | io.FileIO,
196-
thumbfile: str | io.FileIO,
197+
infile: str | BinaryIO,
198+
thumbfile: str | BinaryIO,
197199
scale: float = ...,
198200
interpolation: str = ...,
199201
preview: bool = ...,

lib/matplotlib/mathtext.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ from ._mathtext import RasterParse, VectorParse, get_unicode_index
44
from matplotlib.font_manager import FontProperties
55
from matplotlib.ft2font import FT2Image, LOAD_NO_HINTING
66

7-
from typing import Literal
7+
from typing import IO, Literal
88
from matplotlib.typing import ColorType
99

1010
class MathTextParser:
@@ -13,7 +13,7 @@ class MathTextParser:
1313

1414
def math_to_image(
1515
s: str,
16-
filename_or_obj: str | os.PathLike | io.FileIO,
16+
filename_or_obj: str | os.PathLike | IO,
1717
prop: FontProperties | None = ...,
1818
dpi: float | None = ...,
1919
format: str | None = ...,

lib/matplotlib/pyplot.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,14 @@
8282

8383
if TYPE_CHECKING:
8484
import datetime
85-
import io
8685
import pathlib
8786
import os
8887

8988
import PIL
9089

9190
from numpy.typing import ArrayLike
9291
from typing import (
93-
Any, Callable, Hashable, Literal, Sequence, Iterable, Type, Generator
92+
Any, BinaryIO, Callable, Hashable, Literal, Sequence, Iterable, Type
9493
)
9594

9695
from matplotlib.axis import Tick
@@ -2289,13 +2288,15 @@ def set_cmap(cmap: Colormap | str) -> None:
22892288

22902289

22912290
@_copy_docstring_and_deprecators(matplotlib.image.imread)
2292-
def imread(fname: str | io.FileIO, format: str | None = None) -> np.ndarray:
2291+
def imread(
2292+
fname: str | pathlib.Path | BinaryIO, format: str | None = None
2293+
) -> np.ndarray:
22932294
return matplotlib.image.imread(fname, format)
22942295

22952296

22962297
@_copy_docstring_and_deprecators(matplotlib.image.imsave)
22972298
def imsave(
2298-
fname: str | os.PathLike | io.FileIO, arr: ArrayLike, **kwargs
2299+
fname: str | os.PathLike | BinaryIO, arr: ArrayLike, **kwargs
22992300
) -> None:
23002301
return matplotlib.image.imsave(fname, arr, **kwargs)
23012302

0 commit comments

Comments
 (0)