Skip to content

Commit 1935809

Browse files
maresbricardoV94
authored andcommitted
Fix E721: do not compare types, for exact checks use is / is not
1 parent 4b6a444 commit 1935809

File tree

27 files changed

+41
-41
lines changed

27 files changed

+41
-41
lines changed

pytensor/compile/debugmode.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ def _lessbroken_deepcopy(a):
687687
else:
688688
rval = copy.deepcopy(a)
689689

690-
assert type(rval) == type(a), (type(rval), type(a))
690+
assert type(rval) is type(a), (type(rval), type(a))
691691

692692
if isinstance(rval, np.ndarray):
693693
assert rval.dtype == a.dtype
@@ -1154,7 +1154,7 @@ def __str__(self):
11541154
return str(self.__dict__)
11551155

11561156
def __eq__(self, other):
1157-
rval = type(self) == type(other)
1157+
rval = type(self) is type(other)
11581158
if rval:
11591159
# nodes are not compared because this comparison is
11601160
# supposed to be true for corresponding events that happen

pytensor/compile/ops.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def __init__(self, fn, itypes, otypes, infer_shape):
246246
self.infer_shape = self._infer_shape
247247

248248
def __eq__(self, other):
249-
return type(self) == type(other) and self.__fn == other.__fn
249+
return type(self) is type(other) and self.__fn == other.__fn
250250

251251
def __hash__(self):
252252
return hash(type(self)) ^ hash(self.__fn)

pytensor/graph/basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ def __eq__(self, other):
748748
return True
749749

750750
return (
751-
type(self) == type(other)
751+
type(self) is type(other)
752752
and self.id == other.id
753753
and self.type == other.type
754754
)

pytensor/graph/null_type.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def values_eq(self, a, b, force_same_dtype=True):
3333
raise ValueError("NullType has no values to compare")
3434

3535
def __eq__(self, other):
36-
return type(self) == type(other)
36+
return type(self) is type(other)
3737

3838
def __hash__(self):
3939
return hash(type(self))

pytensor/graph/rewriting/unify.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ def __new__(cls, constraint, token=None, prefix=""):
5757
return obj
5858

5959
def __eq__(self, other):
60-
if type(self) == type(other):
61-
return self.token == other.token and self.constraint == other.constraint
60+
if type(self) is type(other):
61+
return self.token is other.token and self.constraint == other.constraint
6262
return NotImplemented
6363

6464
def __hash__(self):

pytensor/graph/utils.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ def __hash__(self):
229229
if "__eq__" not in dct:
230230

231231
def __eq__(self, other):
232-
return type(self) == type(other) and tuple(
232+
return type(self) is type(other) and tuple(
233233
getattr(self, a) for a in props
234234
) == tuple(getattr(other, a) for a in props)
235235

pytensor/ifelse.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def __init__(self, n_outs, as_view=False, name=None):
7878
self.name = name
7979

8080
def __eq__(self, other):
81-
if type(self) != type(other):
81+
if type(self) is not type(other):
8282
return False
8383
if self.as_view != other.as_view:
8484
return False

pytensor/link/c/params_type.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ def __hash__(self):
301301

302302
def __eq__(self, other):
303303
return (
304-
type(self) == type(other)
304+
type(self) is type(other)
305305
and self.__params_type__ == other.__params_type__
306306
and all(
307307
# NB: Params object should have been already filtered.
@@ -435,7 +435,7 @@ def __repr__(self):
435435

436436
def __eq__(self, other):
437437
return (
438-
type(self) == type(other)
438+
type(self) is type(other)
439439
and self.fields == other.fields
440440
and self.types == other.types
441441
)

pytensor/link/c/type.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ def __hash__(self):
515515

516516
def __eq__(self, other):
517517
return (
518-
type(self) == type(other)
518+
type(self) is type(other)
519519
and self.ctype == other.ctype
520520
and len(self) == len(other)
521521
and len(self.aliases) == len(other.aliases)

pytensor/raise_op.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class ExceptionType(Generic):
1818
def __eq__(self, other):
19-
return type(self) == type(other)
19+
return type(self) is type(other)
2020

2121
def __hash__(self):
2222
return hash(type(self))
@@ -51,7 +51,7 @@ def __str__(self):
5151
return f"CheckAndRaise{{{self.exc_type}({self.msg})}}"
5252

5353
def __eq__(self, other):
54-
if type(self) != type(other):
54+
if type(self) is not type(other):
5555
return False
5656

5757
if self.msg == other.msg and self.exc_type == other.exc_type:

pytensor/scalar/basic.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ def __call__(self, *types):
10741074
return [rval]
10751075

10761076
def __eq__(self, other):
1077-
return type(self) == type(other) and self.tbl == other.tbl
1077+
return type(self) is type(other) and self.tbl == other.tbl
10781078

10791079
def __hash__(self):
10801080
return hash(type(self)) # ignore hash of table
@@ -1160,7 +1160,7 @@ def L_op(self, inputs, outputs, output_gradients):
11601160
return self.grad(inputs, output_gradients)
11611161

11621162
def __eq__(self, other):
1163-
test = type(self) == type(other) and getattr(
1163+
test = type(self) is type(other) and getattr(
11641164
self, "output_types_preference", None
11651165
) == getattr(other, "output_types_preference", None)
11661166
return test
@@ -4133,7 +4133,7 @@ def __eq__(self, other):
41334133
if self is other:
41344134
return True
41354135
if (
4136-
type(self) != type(other)
4136+
type(self) is not type(other)
41374137
or self.nin != other.nin
41384138
or self.nout != other.nout
41394139
):

pytensor/scalar/math.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ def c_code(self, node, name, inp, out, sub):
626626
raise NotImplementedError("only floatingpoint is implemented")
627627

628628
def __eq__(self, other):
629-
return type(self) == type(other)
629+
return type(self) is type(other)
630630

631631
def __hash__(self):
632632
return hash(type(self))
@@ -679,7 +679,7 @@ def c_code(self, node, name, inp, out, sub):
679679
raise NotImplementedError("only floatingpoint is implemented")
680680

681681
def __eq__(self, other):
682-
return type(self) == type(other)
682+
return type(self) is type(other)
683683

684684
def __hash__(self):
685685
return hash(type(self))
@@ -732,7 +732,7 @@ def c_code(self, node, name, inp, out, sub):
732732
raise NotImplementedError("only floatingpoint is implemented")
733733

734734
def __eq__(self, other):
735-
return type(self) == type(other)
735+
return type(self) is type(other)
736736

737737
def __hash__(self):
738738
return hash(type(self))
@@ -1045,7 +1045,7 @@ def c_code(self, node, name, inp, out, sub):
10451045
raise NotImplementedError("only floatingpoint is implemented")
10461046

10471047
def __eq__(self, other):
1048-
return type(self) == type(other)
1048+
return type(self) is type(other)
10491049

10501050
def __hash__(self):
10511051
return hash(type(self))
@@ -1083,7 +1083,7 @@ def c_code(self, node, name, inp, out, sub):
10831083
raise NotImplementedError("only floatingpoint is implemented")
10841084

10851085
def __eq__(self, other):
1086-
return type(self) == type(other)
1086+
return type(self) is type(other)
10871087

10881088
def __hash__(self):
10891089
return hash(type(self))

pytensor/scan/op.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ def is_cpu_vector(s):
12461246
return apply_node
12471247

12481248
def __eq__(self, other):
1249-
if type(self) != type(other):
1249+
if type(self) is not type(other):
12501250
return False
12511251

12521252
if self.info != other.info:

pytensor/sparse/basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ def __eq__(self, other):
462462
return (
463463
a == x
464464
and (b.dtype == y.dtype)
465-
and (type(b) == type(y))
465+
and (type(b) is type(y))
466466
and (b.shape == y.shape)
467467
and (abs(b - y).sum() < 1e-6 * b.nnz)
468468
)

pytensor/tensor/random/type.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def _eq(sa, sb):
107107
return _eq(sa, sb)
108108

109109
def __eq__(self, other):
110-
return type(self) == type(other)
110+
return type(self) is type(other)
111111

112112
def __hash__(self):
113113
return hash(type(self))

pytensor/tensor/rewriting/math.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1742,7 +1742,7 @@ def local_reduce_broadcastable(fgraph, node):
17421742
ii += 1
17431743
new_reduced = reduced.dimshuffle(*pattern)
17441744
if new_axis:
1745-
if type(node.op) == CAReduce:
1745+
if type(node.op) is CAReduce:
17461746
# This case handles `CAReduce` instances
17471747
# (e.g. generated by `scalar_elemwise`), and not the
17481748
# scalar `Op`-specific subclasses

pytensor/tensor/type.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ def values_eq_approx(
370370
return values_eq_approx(a, b, allow_remove_inf, allow_remove_nan, rtol, atol)
371371

372372
def __eq__(self, other):
373-
if type(self) != type(other):
373+
if type(self) is not type(other):
374374
return NotImplemented
375375

376376
return other.dtype == self.dtype and other.shape == self.shape
@@ -624,7 +624,7 @@ def c_code_cache_version(self):
624624

625625
class DenseTypeMeta(MetaType):
626626
def __instancecheck__(self, o):
627-
if type(o) == TensorType or isinstance(o, DenseTypeMeta):
627+
if type(o) is TensorType or isinstance(o, DenseTypeMeta):
628628
return True
629629
return False
630630

pytensor/tensor/type_other.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def __str__(self):
6464
return "slice"
6565

6666
def __eq__(self, other):
67-
return type(self) == type(other)
67+
return type(self) is type(other)
6868

6969
def __hash__(self):
7070
return hash(type(self))

pytensor/tensor/variable.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ class TensorConstantSignature(tuple):
945945
"""
946946

947947
def __eq__(self, other):
948-
if type(self) != type(other):
948+
if type(self) is not type(other):
949949
return False
950950
try:
951951
(t0, d0), (t1, d1) = self, other
@@ -1105,7 +1105,7 @@ def __deepcopy__(self, memo):
11051105

11061106
class DenseVariableMeta(MetaType):
11071107
def __instancecheck__(self, o):
1108-
if type(o) == TensorVariable or isinstance(o, DenseVariableMeta):
1108+
if type(o) is TensorVariable or isinstance(o, DenseVariableMeta):
11091109
return True
11101110
return False
11111111

@@ -1120,7 +1120,7 @@ class DenseTensorVariable(TensorType, metaclass=DenseVariableMeta):
11201120

11211121
class DenseConstantMeta(MetaType):
11221122
def __instancecheck__(self, o):
1123-
if type(o) == TensorConstant or isinstance(o, DenseConstantMeta):
1123+
if type(o) is TensorConstant or isinstance(o, DenseConstantMeta):
11241124
return True
11251125
return False
11261126

pytensor/typed_list/type.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def __eq__(self, other):
5555
Two lists are equal if they contain the same type.
5656
5757
"""
58-
return type(self) == type(other) and self.ttype == other.ttype
58+
return type(self) is type(other) and self.ttype == other.ttype
5959

6060
def __hash__(self):
6161
return hash((type(self), self.ttype))

tests/graph/rewriting/test_unify.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def perform(self, node, inputs, outputs):
4242

4343
class CustomOpNoProps(CustomOpNoPropsNoEq):
4444
def __eq__(self, other):
45-
return type(self) == type(other) and self.a == other.a
45+
return type(self) is type(other) and self.a == other.a
4646

4747
def __hash__(self):
4848
return hash((type(self), self.a))

tests/graph/test_fg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ def test_pickle(self):
3131
s = pickle.dumps(func)
3232
new_func = pickle.loads(s)
3333

34-
assert all(type(a) == type(b) for a, b in zip(func.inputs, new_func.inputs))
35-
assert all(type(a) == type(b) for a, b in zip(func.outputs, new_func.outputs))
34+
assert all(type(a) is type(b) for a, b in zip(func.inputs, new_func.inputs))
35+
assert all(type(a) is type(b) for a, b in zip(func.outputs, new_func.outputs))
3636
assert all(
3737
type(a.op) is type(b.op) # noqa: E721
3838
for a, b in zip(func.apply_nodes, new_func.apply_nodes)

tests/graph/test_op.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def __init__(self, thingy):
2525
self.thingy = thingy
2626

2727
def __eq__(self, other):
28-
return type(other) == type(self) and other.thingy == self.thingy
28+
return type(other) is type(self) and other.thingy == self.thingy
2929

3030
def __str__(self):
3131
return str(self.thingy)

tests/link/c/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def c_code_cache_version(self):
7171
return (1,)
7272

7373
def __eq__(self, other):
74-
return type(self) == type(other)
74+
return type(self) is type(other)
7575

7676
def __hash__(self):
7777
return hash(type(self))

tests/sparse/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def __init__(self, structured):
348348
self.structured = structured
349349

350350
def __eq__(self, other):
351-
return (type(self) == type(other)) and self.structured == other.structured
351+
return (type(self) is type(other)) and self.structured == other.structured
352352

353353
def __hash__(self):
354354
return hash(type(self)) ^ hash(self.structured)

tests/tensor/test_basic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3163,7 +3163,7 @@ def test_stack():
31633163
sx, sy = dscalar(), dscalar()
31643164

31653165
rval = inplace_func([sx, sy], stack([sx, sy]))(-4.0, -2.0)
3166-
assert type(rval) == np.ndarray
3166+
assert type(rval) is np.ndarray
31673167
assert [-4, -2] == list(rval)
31683168

31693169

tests/tensor/test_subtensor.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ def test_ok_list(self):
819819
assert np.allclose(val, good), (val, good)
820820

821821
# Test reuse of output memory
822-
if type(AdvancedSubtensor1) == AdvancedSubtensor1:
822+
if type(AdvancedSubtensor1) is AdvancedSubtensor1:
823823
op = AdvancedSubtensor1()
824824
# When idx is a TensorConstant.
825825
if hasattr(idx, "data"):

0 commit comments

Comments
 (0)