Skip to content

Commit d58a397

Browse files
authored
🏷️ stub numpy.compat (deprecated) (#262)
1 parent e8c813a commit d58a397

File tree

4 files changed

+154
-5
lines changed

4 files changed

+154
-5
lines changed

src/numpy-stubs/compat/__init__.pyi

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# NOTE: numpy.compat is deprecated since 1.26.0
2+
3+
from numpy._utils._inspect import formatargspec, getargspec
4+
5+
from .py3k import (
6+
Path,
7+
asbytes,
8+
asbytes_nested,
9+
asstr,
10+
asunicode,
11+
asunicode_nested,
12+
basestring,
13+
bytes,
14+
contextlib_nullcontext,
15+
getexception,
16+
integer_types,
17+
is_pathlib_path,
18+
isfileobj,
19+
long,
20+
npy_load_module,
21+
open_latin1,
22+
os_PathLike,
23+
os_fspath,
24+
pickle,
25+
sixu,
26+
strchar,
27+
unicode,
28+
)
29+
30+
__all__ = [
31+
"Path",
32+
"asbytes",
33+
"asbytes_nested",
34+
"asstr",
35+
"asunicode",
36+
"asunicode_nested",
37+
"basestring",
38+
"bytes",
39+
"contextlib_nullcontext",
40+
"formatargspec",
41+
"getargspec",
42+
"getexception",
43+
"integer_types",
44+
"is_pathlib_path",
45+
"isfileobj",
46+
"long",
47+
"npy_load_module",
48+
"open_latin1",
49+
"os_PathLike",
50+
"os_fspath",
51+
"pickle",
52+
"sixu",
53+
"strchar",
54+
"unicode",
55+
]

src/numpy-stubs/compat/py3k.pyi

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import io
2+
import pickle as pickle
3+
from _typeshed import FileDescriptorOrPath, Incomplete, OpenTextMode
4+
from builtins import bytes # noqa: UP029
5+
from collections.abc import Iterable, Sequence
6+
from os import PathLike as os_PathLike, fspath as os_fspath # noqa: ICN003
7+
from pathlib import Path as Path
8+
from types import ModuleType, TracebackType
9+
from typing import IO, Any, Final, Generic, TypeGuard, overload
10+
from typing_extensions import TypeIs, TypeVar
11+
12+
__all__ = [
13+
"Path",
14+
"asbytes",
15+
"asbytes_nested",
16+
"asstr",
17+
"asunicode",
18+
"asunicode_nested",
19+
"basestring",
20+
"bytes",
21+
"contextlib_nullcontext",
22+
"getexception",
23+
"integer_types",
24+
"is_pathlib_path",
25+
"isfileobj",
26+
"long",
27+
"npy_load_module",
28+
"open_latin1",
29+
"os_PathLike",
30+
"os_fspath",
31+
"pickle",
32+
"sixu",
33+
"strchar",
34+
"unicode",
35+
]
36+
37+
###
38+
39+
_T = TypeVar("_T")
40+
_T_co = TypeVar("_T_co", covariant=True, default=None)
41+
42+
###
43+
44+
long = int
45+
unicode = str
46+
basestring = str
47+
integer_types: Final[tuple[type[int]]] = ...
48+
strchar: Final = "U"
49+
50+
class contextlib_nullcontext(Generic[_T_co]):
51+
enter_result: Incomplete
52+
def __init__(self, /, enter_result: _T_co | None = None) -> None: ...
53+
def __enter__(self, /) -> _T_co: ...
54+
def __exit__(self, cls: type[BaseException] | None, exc: BaseException | None, tb: TracebackType | None, /) -> None: ...
55+
56+
#
57+
def asstr(s: object) -> str: ...
58+
def asunicode(s: object) -> str: ...
59+
def asbytes(s: object) -> bytes: ...
60+
61+
#
62+
@overload
63+
def asbytes_nested(x: bytes | str) -> bytes: ...
64+
@overload
65+
def asbytes_nested(x: Iterable[bytes]) -> Sequence[bytes]: ...
66+
@overload
67+
def asbytes_nested(x: Iterable[Iterable[bytes]]) -> Sequence[Sequence[bytes]]: ...
68+
@overload
69+
def asbytes_nested(x: Iterable[object]) -> Any: ...
70+
71+
#
72+
@overload
73+
def asunicode_nested(x: bytes | str) -> str: ...
74+
@overload
75+
def asunicode_nested(x: Iterable[bytes | str]) -> Sequence[str]: ...
76+
@overload
77+
def asunicode_nested(x: Iterable[object]) -> Sequence[Any]: ...
78+
79+
# NOTE: don't use `TypeIs` here; even `f` is of this type, this function might still return `False`.
80+
def isfileobj(f: object) -> TypeGuard[io.FileIO | io.BufferedReader | io.BufferedWriter]: ...
81+
82+
#
83+
def is_pathlib_path(obj: object) -> TypeIs[Path]: ...
84+
85+
#
86+
@overload
87+
def open_latin1(filename: FileDescriptorOrPath, mode: OpenTextMode = "r") -> io.TextIOWrapper: ...
88+
@overload
89+
def open_latin1(filename: FileDescriptorOrPath, mode: str = "r") -> IO[Any]: ...
90+
91+
#
92+
def sixu(s: _T) -> _T: ...
93+
94+
#
95+
def getexception() -> BaseException: ...
96+
97+
#
98+
def npy_load_module(name: str, fn: str, info: None = None) -> ModuleType: ...

tool/.mypyignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ numpy\._core\.cversions
2727
# mypy plugin
2828
numpy\.typing\.mypy_plugin
2929

30-
3130
# workaround mypy's lack of Python 3.13 support for `NamedTuple` types (mypy <= 1.15.0)
3231
numpy(\.\w+)+\.__replace__
3332

@@ -36,4 +35,5 @@ numpy\.testing(\._private\.utils)?\.check_support_sve
3635

3736
# workaround for stubtest ignoring `if sys.version_info` blocks in `_typeshed` when
3837
# `python_version`
38+
numpy\.compat(\.py3k)?\.(Path|os_PathLike|bytes)\.[_a-zA-Z]+
3939
numpy\.testing\.TestCase\.[_a-zA-Z]+

tool/.mypyignore-todo

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ numpy(\..+)?\.complexfloating.__hash__
99
numpy(\..+)?\.complexfloating.__complex__
1010

1111
numpy._pyinstaller.hook-numpy
12-
1312
numpy.ctypeslib._ctypeslib
1413

15-
numpy.compat
16-
numpy.compat.py3k
17-
1814
numpy.distutils
1915

2016
numpy.f2py.__main__

0 commit comments

Comments
 (0)