Skip to content

Commit eb9b5ad

Browse files
author
Release Manager
committed
sagemathgh-38790: avoid some type comparisons in combinat just a few minor changes, about using `isinstance` when possible ### 📝 Checklist - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. URL: sagemath#38790 Reported by: Frédéric Chapoton Reviewer(s): Travis Scrimshaw
2 parents 83b30af + 86aa871 commit eb9b5ad

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

src/sage/combinat/cluster_algebra_quiver/quiver.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ def __init__(self, data, frozen=None, user_labels=None):
232232
values))
233233

234234
# constructs a quiver from a mutation type
235-
if type( data ) in [QuiverMutationType_Irreducible,QuiverMutationType_Reducible]:
235+
if isinstance(data, (QuiverMutationType_Irreducible,
236+
QuiverMutationType_Reducible)):
236237
if frozen is not None:
237238
print('The input specifies a mutation type, so the'
238239
' additional parameter frozen is ignored.'
@@ -1950,10 +1951,15 @@ def is_mutation_finite( self, nr_of_checks=None, return_path=False ):
19501951
path = None
19511952
elif not return_path and self._mutation_type == 'undetermined infinite mutation type':
19521953
is_finite = False
1953-
elif type( self._mutation_type ) in [QuiverMutationType_Irreducible, QuiverMutationType_Reducible] and self._mutation_type.is_mutation_finite():
1954+
elif (isinstance(self._mutation_type, (QuiverMutationType_Irreducible,
1955+
QuiverMutationType_Reducible))
1956+
and self._mutation_type.is_mutation_finite()):
19541957
is_finite = True
19551958
path = None
1956-
elif not return_path and type( self._mutation_type ) in [QuiverMutationType_Irreducible, QuiverMutationType_Reducible] and not self._mutation_type.is_mutation_finite():
1959+
elif (not return_path and isinstance(self._mutation_type,
1960+
(QuiverMutationType_Irreducible,
1961+
QuiverMutationType_Reducible))
1962+
and not self._mutation_type.is_mutation_finite()):
19571963
is_finite = False
19581964
else:
19591965
# turning dg_component into a canonical form

src/sage/combinat/cluster_algebra_quiver/quiver_mutation_type.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ def __call__(self, *args):
6767
_mutation_type_error(data)
6868

6969
# check for reducible types
70-
if all(type(data_component) in [list, tuple, QuiverMutationType_Irreducible] for data_component in data):
70+
if all(isinstance(data_component, (list, tuple,
71+
QuiverMutationType_Irreducible))
72+
for data_component in data):
7173
if len(data) == 1:
7274
return QuiverMutationType(data[0])
7375
else:

src/sage/combinat/core.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def __init__(self, parent, core):
102102
raise ValueError("%s is not a %s-core" % (part, k))
103103
CombinatorialElement.__init__(self, parent, core)
104104

105-
def __eq__(self, other):
105+
def __eq__(self, other) -> bool:
106106
"""
107107
Test for equality.
108108
@@ -123,7 +123,7 @@ def __eq__(self, other):
123123
self.parent().k == other.parent().k)
124124
return False
125125

126-
def __ne__(self, other):
126+
def __ne__(self, other) -> bool:
127127
"""
128128
Test for un-equality.
129129
@@ -169,7 +169,7 @@ def __hash__(self):
169169
self._hash = hash(tuple(self._list)) + hash(self.parent().k)
170170
return self._hash
171171

172-
def _latex_(self):
172+
def _latex_(self) -> str:
173173
r"""
174174
Output the LaTeX representation of this core as a partition.
175175
@@ -449,7 +449,7 @@ def weak_le(self, other):
449449
...
450450
ValueError: the two cores do not have the same k
451451
"""
452-
if type(self) is type(other):
452+
if isinstance(other, Core):
453453
if self.k() != other.k():
454454
raise ValueError("the two cores do not have the same k")
455455
else:
@@ -504,14 +504,14 @@ def strong_le(self, other):
504504
...
505505
ValueError: the two cores do not have the same k
506506
"""
507-
if type(self) is type(other):
507+
if isinstance(other, Core):
508508
if self.k() != other.k():
509509
raise ValueError("the two cores do not have the same k")
510510
else:
511511
other = Core(other, self.k())
512512
return other.contains(self)
513513

514-
def contains(self, other):
514+
def contains(self, other) -> bool:
515515
r"""
516516
Check whether ``self`` contains ``other``.
517517

src/sage/combinat/root_system/integrable_representations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,7 +1046,7 @@ def modular_characteristic(self, mu=None):
10461046
sage: [V.modular_characteristic(x) for x in V.dominant_maximal_weights()]
10471047
[11/56, -1/280, 111/280]
10481048
"""
1049-
if type(mu) is tuple:
1049+
if isinstance(mu, tuple):
10501050
n = mu
10511051
else:
10521052
n = self.from_weight(mu)

src/sage/combinat/shard_order.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def __le__(self, other):
127127
sage: e1 <= e0
128128
False
129129
"""
130-
if type(self) is not type(other) or len(self) != len(other):
130+
if not isinstance(other, ShardPosetElement) or len(self) != len(other):
131131
raise TypeError("these are not comparable")
132132
if self.runs == other.runs:
133133
return True

0 commit comments

Comments
 (0)