Skip to content

Commit d5d4ddf

Browse files
committed
🤖 also generate type-tests for the comparison ops
1 parent 7ec205b commit d5d4ddf

File tree

3 files changed

+1266
-11
lines changed

3 files changed

+1266
-11
lines changed

‎test/generate_scalar_binops.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@
2929
"&": op.__and__,
3030
"^": op.__xor__,
3131
"|": op.__or__,
32+
"<": op.__lt__,
33+
"<=": op.__le__,
34+
">=": op.__ge__,
35+
">": op.__gt__,
36+
# TODO(jorenham): these currently all return `Any`; fix this
37+
# "==": op.__eq__,
3238
}
3339
NAMES = {
3440
# builtins (key length > 1)
@@ -70,10 +76,10 @@
7076
BITWISE_CHARS = "?bhilqBHILQ"
7177

7278

73-
def _scalar(key: str, /) -> np.generic | complex:
79+
def _scalar(key: str, /) -> np.number | np.bool | np.timedelta64 | np.datetime64 | bool:
7480
if len(key) > 1:
7581
# must be one of the builtin scalars
76-
pytype: type[complex] = getattr(__builtins__, key)
82+
pytype: type[bool] = getattr(__builtins__, key)
7783
return pytype(1)
7884

7985
dtype = np.dtype(key)
@@ -105,7 +111,7 @@ def _assert_stmt(op: str, lhs: str, rhs: str, /) -> str | None:
105111
expr_eval = f"{NAMES[lhs]}{pad}{op}{pad}{NAMES[rhs]}"
106112

107113
try:
108-
val_out: np.generic = OPS[op](_scalar(lhs), _scalar(rhs))
114+
val_out = OPS[op](_scalar(lhs), _scalar(rhs))
109115
except TypeError:
110116
# generate rejection test, while avoiding trivial cases
111117
if op not in DATETIME_OPS and (lhs == "M" or rhs == "M"):
@@ -125,9 +131,9 @@ def _assert_stmt(op: str, lhs: str, rhs: str, /) -> str | None:
125131
"# type: ignore[operator]",
126132
"# pyright: ignore[reportOperatorIssue]",
127133
))
128-
else:
129-
expr_type = _sctype_expr(val_out.dtype)
130-
return f"assert_type({expr_eval}, {expr_type})"
134+
135+
expr_type = _sctype_expr(val_out.dtype)
136+
return f"assert_type({expr_eval}, {expr_type})"
131137

132138

133139
def _gen_imports() -> Generator[str]:

‎test/generated/ruff.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ line-length = 130
33

44
[lint]
55
extend-ignore = [
6-
"PTH", # flake8-use-pathlib
7-
"PYI015", # flake8-pyi: assignment-default-in-stub
8-
"PYI017", # flake8-pyi: complex-assignment-in-stub
9-
"SLF001", # flake8-self: private-member-access
6+
"PTH", # flake8-use-pathlib
7+
"PYI015", # flake8-pyi: assignment-default-in-stub
8+
"PYI017", # flake8-pyi: complex-assignment-in-stub
9+
"SLF001", # flake8-self: private-member-access
10+
"PLR0124", # pylint/R: comparison-with-itself
1011
]

0 commit comments

Comments
 (0)