Skip to content

Commit 37fcc1c

Browse files
BvB93Bas van Beek
authored andcommitted
TST: Add typing tests for np.rec
1 parent c92d3a1 commit 37fcc1c

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
import numpy.typing as npt
3+
4+
AR_i8: npt.NDArray[np.int64]
5+
6+
np.rec.fromarrays(1) # E: No overload variant
7+
np.rec.fromarrays([1, 2, 3], dtype=[("f8", "f8")], formats=["f8", "f8"]) # E: No overload variant
8+
9+
np.rec.fromrecords(AR_i8) # E: incompatible type
10+
np.rec.fromrecords([(1.5,)], dtype=[("f8", "f8")], formats=["f8", "f8"]) # E: No overload variant
11+
12+
np.rec.fromstring("string", dtype=[("f8", "f8")]) # E: No overload variant
13+
np.rec.fromstring(b"bytes") # E: No overload variant
14+
np.rec.fromstring(b"(1.5,)", dtype=[("f8", "f8")], formats=["f8", "f8"]) # E: No overload variant
15+
16+
with open("test", "r") as f:
17+
np.rec.fromfile(f, dtype=[("f8", "f8")]) # E: No overload variant
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
import io
2+
from typing import Any, List
3+
4+
import numpy as np
5+
import numpy.typing as npt
6+
7+
AR_i8: npt.NDArray[np.int64]
8+
REC_AR_V: np.recarray[Any, np.dtype[np.record]]
9+
AR_LIST: List[npt.NDArray[np.int64]]
10+
11+
format_parser: np.format_parser
12+
record: np.record
13+
file_obj: io.BufferedIOBase
14+
15+
reveal_type(np.format_parser( # E: numpy.format_parser
16+
formats=[np.float64, np.int64, np.bool_],
17+
names=["f8", "i8", "?"],
18+
titles=None,
19+
aligned=True,
20+
))
21+
reveal_type(format_parser.dtype) # E: numpy.dtype[numpy.void]
22+
23+
reveal_type(record.field_a) # E: Any
24+
reveal_type(record.field_b) # E: Any
25+
reveal_type(record["field_a"]) # E: Any
26+
reveal_type(record["field_b"]) # E: Any
27+
reveal_type(record.pprint()) # E: str
28+
record.field_c = 5
29+
30+
reveal_type(REC_AR_V.field(0)) # E: Any
31+
reveal_type(REC_AR_V.field("field_a")) # E: Any
32+
reveal_type(REC_AR_V.field(0, AR_i8)) # E: None
33+
reveal_type(REC_AR_V.field("field_a", AR_i8)) # E: None
34+
reveal_type(REC_AR_V["field_a"]) # E: Any
35+
reveal_type(REC_AR_V.field_a) # E: Any
36+
37+
reveal_type(np.recarray( # numpy.recarray[Any, numpy.dtype[numpy.record]]
38+
shape=(10, 5),
39+
formats=[np.float64, np.int64, np.bool_],
40+
order="K",
41+
byteorder="|",
42+
))
43+
reveal_type(np.recarray( # numpy.recarray[Any, numpy.dtype[Any]]
44+
shape=(10, 5),
45+
dtype=[("f8", np.float64), ("i8", np.int64)],
46+
strides=(5, 5),
47+
))
48+
49+
reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[numpy.record]]
50+
AR_LIST,
51+
))
52+
reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[Any]]
53+
AR_LIST,
54+
dtype=np.int64,
55+
))
56+
reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[Any]]
57+
AR_LIST,
58+
formats=[np.int64, np.float64],
59+
names=["i8", "f8"]
60+
))
61+
62+
reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]]
63+
(1, 1.5),
64+
))
65+
reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]]
66+
[(1, 1.5)],
67+
dtype=[("i8", np.int64), ("f8", np.float64)],
68+
))
69+
reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]]
70+
REC_AR_V,
71+
formats=[np.int64, np.float64],
72+
names=["i8", "f8"]
73+
))
74+
75+
reveal_type(np.rec.fromstring( # numpy.recarray[Any, numpy.dtype[numpy.record]]
76+
b"(1, 1.5)",
77+
dtype=[("i8", np.int64), ("f8", np.float64)],
78+
))
79+
reveal_type(np.rec.fromstring( # numpy.recarray[Any, numpy.dtype[numpy.record]]
80+
REC_AR_V,
81+
formats=[np.int64, np.float64],
82+
names=["i8", "f8"]
83+
))
84+
85+
reveal_type(np.rec.fromfile( # numpy.recarray[Any, numpy.dtype[Any]]
86+
"test_file.txt",
87+
dtype=[("i8", np.int64), ("f8", np.float64)],
88+
))
89+
reveal_type(np.rec.fromfile( # numpy.recarray[Any, numpy.dtype[numpy.record]]
90+
file_obj,
91+
formats=[np.int64, np.float64],
92+
names=["i8", "f8"]
93+
))
94+
95+
reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[{int64}]]
96+
AR_i8,
97+
))
98+
reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[Any]]
99+
[(1, 1.5)],
100+
dtype=[("i8", np.int64), ("f8", np.float64)],
101+
))
102+
reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[numpy.record]]
103+
[(1, 1.5)],
104+
formats=[np.int64, np.float64],
105+
names=["i8", "f8"]
106+
))

0 commit comments

Comments
 (0)