Skip to content

Commit b8ec2d7

Browse files
authored
⚡️♻️ remove redundant dtype.__new__ overloads for faster type-checking (#479)
* ⚡️♻️ remove redundant `dtype.__new__` overloads for faster type-checking * 🔧 have `basedpyright` ignore a bunch of directories * 🐴 rollback the `DTypeLike` changes so that mypy stops flipping out
2 parents f1e7334 + 5c43a04 commit b8ec2d7

File tree

5 files changed

+167
-435
lines changed

5 files changed

+167
-435
lines changed

pyproject.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,18 @@ disable_memoryview_promotion = true
135135
[tool.pyright]
136136
include = ["src", "tool"]
137137
ignore = [".venv"]
138+
exclude = [
139+
".cache",
140+
".git",
141+
".github",
142+
".mypy_cache",
143+
".pytest_cache",
144+
".ruff_cache",
145+
".tox",
146+
".vscode",
147+
"docs",
148+
"site",
149+
]
138150
stubPath = "src"
139151
pythonPlatform = "All"
140152
typeCheckingMode = "strict"

src/numpy-stubs/@test/static/accept/dtype.pyi

Lines changed: 28 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,60 @@
11
import ctypes as ct
2-
import datetime as dt
32
from decimal import Decimal
43
from fractions import Fraction
5-
from typing import Any, Literal, TypeAlias
64
from typing_extensions import LiteralString, assert_type
75

86
import numpy as np
97
from numpy.dtypes import StringDType
108

11-
# a combination of likely `object` dtype-like candidates (no `_co`)
12-
_PyObjectLike: TypeAlias = Decimal | Fraction | dt.datetime | dt.timedelta
13-
149
dtype_U: np.dtype[np.str_]
1510
dtype_V: np.dtype[np.void]
1611
dtype_i8: np.dtype[np.int64]
1712

18-
py_int_co: type[int | bool]
19-
py_float_co: type[float | int | bool]
20-
py_complex_co: type[complex | float | int | bool]
21-
py_object: type[_PyObjectLike]
13+
py_int_co: type[int]
14+
py_float_co: type[float]
15+
py_complex_co: type[complex]
16+
py_object: type
2217
py_character: type[str | bytes]
2318
py_flexible: type[str | bytes | memoryview]
2419

2520
ct_floating: type[ct.c_float | ct.c_double | ct.c_longdouble]
2621
ct_number: type[ct.c_uint8 | ct.c_float]
2722
ct_generic: type[ct.c_bool | ct.c_char]
2823

29-
cs_integer: Literal["u1", "<i2", "L"]
30-
cs_number: Literal["=L", "i", "c16"]
31-
cs_flex: Literal[">V", "S"]
32-
cs_generic: Literal["H", "U", "h", "|M8[Y]", "?"]
33-
34-
dt_inexact: np.dtype[np.inexact]
3524
dt_string: StringDType
3625

3726
assert_type(np.dtype(np.float64), np.dtype[np.float64])
3827
assert_type(np.dtype(np.float64, metadata={"test": "test"}), np.dtype[np.float64])
3928
assert_type(np.dtype(np.int64), np.dtype[np.int64])
4029

4130
# String aliases
42-
assert_type(np.dtype("float64"), np.dtype[np.float64])
43-
assert_type(np.dtype("float32"), np.dtype[np.float32])
44-
assert_type(np.dtype("int64"), np.dtype[np.int64])
45-
assert_type(np.dtype("int32"), np.dtype[np.int32])
4631
assert_type(np.dtype("bool"), np.dtype[np.bool])
32+
assert_type(np.dtype("int32"), np.dtype[np.int32])
33+
assert_type(np.dtype("int64"), np.dtype[np.int64])
34+
assert_type(np.dtype("float32"), np.dtype[np.float32])
35+
assert_type(np.dtype("float64"), np.dtype[np.float64])
4736
assert_type(np.dtype("bytes"), np.dtype[np.bytes_])
4837
assert_type(np.dtype("str"), np.dtype[np.str_])
4938

5039
# Python types
5140
assert_type(np.dtype(bool), np.dtype[np.bool])
52-
assert_type(np.dtype(py_int_co), np.dtype[np.int_ | np.bool])
53-
assert_type(np.dtype(int), np.dtype[np.int_ | np.bool])
54-
assert_type(np.dtype(py_float_co), np.dtype[np.float64 | np.int_ | np.bool])
55-
assert_type(np.dtype(float), np.dtype[np.float64 | np.int_ | np.bool])
56-
assert_type(np.dtype(py_complex_co), np.dtype[np.complex128 | np.float64 | np.int_ | np.bool])
57-
assert_type(np.dtype(complex), np.dtype[np.complex128 | np.float64 | np.int_ | np.bool])
58-
assert_type(np.dtype(py_object), np.dtype[np.object_ | Any])
41+
assert_type(np.dtype(int), np.dtype[np.int_])
42+
assert_type(np.dtype(float), np.dtype[np.float64])
43+
assert_type(np.dtype(complex), np.dtype[np.complex128])
44+
assert_type(np.dtype(object), np.dtype[np.object_])
5945
assert_type(np.dtype(str), np.dtype[np.str_])
6046
assert_type(np.dtype(bytes), np.dtype[np.bytes_])
61-
assert_type(np.dtype(py_character), np.dtype[np.character])
6247
assert_type(np.dtype(memoryview), np.dtype[np.void])
63-
assert_type(np.dtype(py_flexible), np.dtype[np.flexible])
48+
49+
assert_type(np.dtype(np.signedinteger), np.dtype[np.signedinteger])
50+
assert_type(np.dtype(np.unsignedinteger), np.dtype[np.unsignedinteger])
51+
assert_type(np.dtype(np.integer), np.dtype[np.integer])
52+
assert_type(np.dtype(np.floating), np.dtype[np.floating])
53+
assert_type(np.dtype(np.complexfloating), np.dtype[np.complexfloating])
54+
assert_type(np.dtype(np.inexact), np.dtype[np.inexact])
55+
assert_type(np.dtype(np.number), np.dtype[np.number])
56+
# NOTE: `character` and `flexible` always fail on mypy because of some mypy bug
57+
assert_type(np.dtype(np.generic), np.dtype[np.generic])
6458

6559
assert_type(np.dtype(Decimal), np.dtype[np.object_])
6660
assert_type(np.dtype(Fraction), np.dtype[np.object_])
@@ -70,25 +64,22 @@ assert_type(np.dtype("u1"), np.dtype[np.uint8])
7064
assert_type(np.dtype("int_"), np.dtype[np.intp])
7165
assert_type(np.dtype("longlong"), np.dtype[np.longlong])
7266
assert_type(np.dtype(">g"), np.dtype[np.longdouble])
73-
assert_type(np.dtype(cs_integer), np.dtype[np.integer])
74-
assert_type(np.dtype(cs_number), np.dtype[np.number])
75-
assert_type(np.dtype(cs_flex), np.dtype[np.flexible])
76-
assert_type(np.dtype(cs_generic), np.dtype[np.generic])
7767

7868
# ctypes
79-
assert_type(np.dtype(ct.c_double), np.dtype[np.float64])
80-
assert_type(np.dtype(ct.c_longlong), np.dtype[np.longlong])
81-
assert_type(np.dtype(ct.c_uint32), np.dtype[np.uint32])
8269
assert_type(np.dtype(ct.c_bool), np.dtype[np.bool])
83-
assert_type(np.dtype(ct.c_char), np.dtype[np.bytes_])
70+
assert_type(np.dtype(ct.c_uint32), np.dtype[np.uint32])
71+
assert_type(np.dtype(ct.c_ssize_t), np.dtype[np.intp])
72+
assert_type(np.dtype(ct.c_longlong), np.dtype[np.longlong])
73+
assert_type(np.dtype(ct.c_double), np.dtype[np.double])
8474
assert_type(np.dtype(ct.py_object), np.dtype[np.object_])
75+
assert_type(np.dtype(ct.c_char), np.dtype[np.bytes_])
8576

8677
# Special case for None
8778
assert_type(np.dtype(None), np.dtype[np.float64])
8879

8980
# Dypes of dtypes
9081
assert_type(np.dtype(np.dtype(np.float64)), np.dtype[np.float64])
91-
assert_type(np.dtype(dt_inexact), np.dtype[np.inexact])
82+
assert_type(np.dtype(np.dtype(np.inexact)), np.dtype[np.inexact])
9283

9384
# Parameterized dtypes
9485
assert_type(np.dtype("S8"), np.dtype)

0 commit comments

Comments
 (0)