diff options
author | Bas van Beek <43369155+BvB93@users.noreply.github.com> | 2021-09-23 18:39:12 +0200 |
---|---|---|
committer | Bas van Beek <b.f.van.beek@vu.nl> | 2021-09-24 17:15:54 +0200 |
commit | 37fcc1ce08c3eee86feaeef61e385d463a07ae35 (patch) | |
tree | 9ce99890f5876b5041eae609ed00642f6fefc340 /numpy | |
parent | c92d3a15f4449854b8757f659642ce3bee32382a (diff) | |
download | numpy-37fcc1ce08c3eee86feaeef61e385d463a07ae35.tar.gz |
TST: Add typing tests for `np.rec`
Diffstat (limited to 'numpy')
-rw-r--r-- | numpy/typing/tests/data/fail/rec.py | 17 | ||||
-rw-r--r-- | numpy/typing/tests/data/reveal/rec.py | 106 |
2 files changed, 123 insertions, 0 deletions
diff --git a/numpy/typing/tests/data/fail/rec.py b/numpy/typing/tests/data/fail/rec.py new file mode 100644 index 000000000..a57f1ba27 --- /dev/null +++ b/numpy/typing/tests/data/fail/rec.py @@ -0,0 +1,17 @@ +import numpy as np +import numpy.typing as npt + +AR_i8: npt.NDArray[np.int64] + +np.rec.fromarrays(1) # E: No overload variant +np.rec.fromarrays([1, 2, 3], dtype=[("f8", "f8")], formats=["f8", "f8"]) # E: No overload variant + +np.rec.fromrecords(AR_i8) # E: incompatible type +np.rec.fromrecords([(1.5,)], dtype=[("f8", "f8")], formats=["f8", "f8"]) # E: No overload variant + +np.rec.fromstring("string", dtype=[("f8", "f8")]) # E: No overload variant +np.rec.fromstring(b"bytes") # E: No overload variant +np.rec.fromstring(b"(1.5,)", dtype=[("f8", "f8")], formats=["f8", "f8"]) # E: No overload variant + +with open("test", "r") as f: + np.rec.fromfile(f, dtype=[("f8", "f8")]) # E: No overload variant diff --git a/numpy/typing/tests/data/reveal/rec.py b/numpy/typing/tests/data/reveal/rec.py new file mode 100644 index 000000000..2fa8cc7b9 --- /dev/null +++ b/numpy/typing/tests/data/reveal/rec.py @@ -0,0 +1,106 @@ +import io +from typing import Any, List + +import numpy as np +import numpy.typing as npt + +AR_i8: npt.NDArray[np.int64] +REC_AR_V: np.recarray[Any, np.dtype[np.record]] +AR_LIST: List[npt.NDArray[np.int64]] + +format_parser: np.format_parser +record: np.record +file_obj: io.BufferedIOBase + +reveal_type(np.format_parser( # E: numpy.format_parser + formats=[np.float64, np.int64, np.bool_], + names=["f8", "i8", "?"], + titles=None, + aligned=True, +)) +reveal_type(format_parser.dtype) # E: numpy.dtype[numpy.void] + +reveal_type(record.field_a) # E: Any +reveal_type(record.field_b) # E: Any +reveal_type(record["field_a"]) # E: Any +reveal_type(record["field_b"]) # E: Any +reveal_type(record.pprint()) # E: str +record.field_c = 5 + +reveal_type(REC_AR_V.field(0)) # E: Any +reveal_type(REC_AR_V.field("field_a")) # E: Any +reveal_type(REC_AR_V.field(0, AR_i8)) # E: None +reveal_type(REC_AR_V.field("field_a", AR_i8)) # E: None +reveal_type(REC_AR_V["field_a"]) # E: Any +reveal_type(REC_AR_V.field_a) # E: Any + +reveal_type(np.recarray( # numpy.recarray[Any, numpy.dtype[numpy.record]] + shape=(10, 5), + formats=[np.float64, np.int64, np.bool_], + order="K", + byteorder="|", +)) +reveal_type(np.recarray( # numpy.recarray[Any, numpy.dtype[Any]] + shape=(10, 5), + dtype=[("f8", np.float64), ("i8", np.int64)], + strides=(5, 5), +)) + +reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[numpy.record]] + AR_LIST, +)) +reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[Any]] + AR_LIST, + dtype=np.int64, +)) +reveal_type(np.rec.fromarrays( # numpy.recarray[Any, numpy.dtype[Any]] + AR_LIST, + formats=[np.int64, np.float64], + names=["i8", "f8"] +)) + +reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]] + (1, 1.5), +)) +reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]] + [(1, 1.5)], + dtype=[("i8", np.int64), ("f8", np.float64)], +)) +reveal_type(np.rec.fromrecords( # numpy.recarray[Any, numpy.dtype[numpy.record]] + REC_AR_V, + formats=[np.int64, np.float64], + names=["i8", "f8"] +)) + +reveal_type(np.rec.fromstring( # numpy.recarray[Any, numpy.dtype[numpy.record]] + b"(1, 1.5)", + dtype=[("i8", np.int64), ("f8", np.float64)], +)) +reveal_type(np.rec.fromstring( # numpy.recarray[Any, numpy.dtype[numpy.record]] + REC_AR_V, + formats=[np.int64, np.float64], + names=["i8", "f8"] +)) + +reveal_type(np.rec.fromfile( # numpy.recarray[Any, numpy.dtype[Any]] + "test_file.txt", + dtype=[("i8", np.int64), ("f8", np.float64)], +)) +reveal_type(np.rec.fromfile( # numpy.recarray[Any, numpy.dtype[numpy.record]] + file_obj, + formats=[np.int64, np.float64], + names=["i8", "f8"] +)) + +reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[{int64}]] + AR_i8, +)) +reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[Any]] + [(1, 1.5)], + dtype=[("i8", np.int64), ("f8", np.float64)], +)) +reveal_type(np.rec.array( # numpy.recarray[Any, numpy.dtype[numpy.record]] + [(1, 1.5)], + formats=[np.int64, np.float64], + names=["i8", "f8"] +)) |