Skip to content

Commit 4c351eb

Browse files
author
Release Manager
committed
sagemathgh-38032: `sage.combinat.finite_state_machine`: Deprecate `is_...` functions <!-- ^ Please provide a concise and informative title. --> <!-- ^ Don't put issue numbers in the title, do this in the PR description below. --> <!-- ^ For example, instead of "Fixes sagemath#12345" use "Introduce new method to calculate 1 + 2". --> <!-- v Describe your changes below in detail. --> <!-- v Why is this change required? What problem does it solve? --> <!-- v If this PR resolves an open issue, please link to it here. For example, "Fixes sagemath#12345". --> ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [ ] 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. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#38032 Reported by: Matthias Köppe Reviewer(s): Kwankyu Lee, Matthias Köppe
2 parents 7b8a6f9 + 1db622b commit 4c351eb

File tree

1 file changed

+70
-34
lines changed

1 file changed

+70
-34
lines changed

src/sage/combinat/finite_state_machine.py

+70-34
Original file line numberDiff line numberDiff line change
@@ -1168,8 +1168,13 @@ def is_FSMState(S):
11681168

11691169
sage: from sage.combinat.finite_state_machine import is_FSMState, FSMState
11701170
sage: is_FSMState(FSMState('A'))
1171+
doctest:warning...
1172+
DeprecationWarning: The function is_FSMState is deprecated; use 'isinstance(..., FSMState)' instead.
1173+
See https://github.com/sagemath/sage/issues/38032 for details.
11711174
True
11721175
"""
1176+
from sage.misc.superseded import deprecation
1177+
deprecation(38032, "The function is_FSMState is deprecated; use 'isinstance(..., FSMState)' instead.")
11731178
return isinstance(S, FSMState)
11741179

11751180

@@ -1923,7 +1928,7 @@ def __eq__(self, other):
19231928
sage: A == B
19241929
True
19251930
"""
1926-
if not is_FSMState(other):
1931+
if not isinstance(other, FSMState):
19271932
return False
19281933
return self.label() == other.label()
19291934

@@ -2177,8 +2182,13 @@ def is_FSMTransition(T):
21772182

21782183
sage: from sage.combinat.finite_state_machine import is_FSMTransition, FSMTransition
21792184
sage: is_FSMTransition(FSMTransition('A', 'B'))
2185+
doctest:warning...
2186+
DeprecationWarning: The function is_FSMTransition is deprecated; use 'isinstance(..., FSMTransition)' instead.
2187+
See https://github.com/sagemath/sage/issues/38032 for details.
21802188
True
21812189
"""
2190+
from sage.misc.superseded import deprecation
2191+
deprecation(38032, "The function is_FSMTransition is deprecated; use 'isinstance(..., FSMTransition)' instead.")
21822192
return isinstance(T, FSMTransition)
21832193

21842194

@@ -2241,11 +2251,11 @@ def __init__(self, from_state, to_state,
22412251
sage: FSMTransition('A', 'B', 0, 1)
22422252
Transition from 'A' to 'B': 0|1
22432253
"""
2244-
if is_FSMState(from_state):
2254+
if isinstance(from_state, FSMState):
22452255
self.from_state = from_state
22462256
else:
22472257
self.from_state = FSMState(from_state)
2248-
if is_FSMState(to_state):
2258+
if isinstance(to_state, FSMState):
22492259
self.to_state = to_state
22502260
else:
22512261
self.to_state = FSMState(to_state)
@@ -2438,7 +2448,7 @@ def __eq__(self, other):
24382448
sage: t1 == t2
24392449
True
24402450
"""
2441-
if not is_FSMTransition(other):
2451+
if not isinstance(other, FSMTransition):
24422452
return False
24432453
return self.from_state == other.from_state \
24442454
and self.to_state == other.to_state \
@@ -2494,12 +2504,17 @@ def is_FiniteStateMachine(FSM):
24942504

24952505
sage: from sage.combinat.finite_state_machine import is_FiniteStateMachine
24962506
sage: is_FiniteStateMachine(FiniteStateMachine())
2507+
doctest:warning...
2508+
DeprecationWarning: The function is_FiniteStateMachine is deprecated; use 'isinstance(..., FiniteStateMachine)' instead.
2509+
See https://github.com/sagemath/sage/issues/38032 for details.
24972510
True
24982511
sage: is_FiniteStateMachine(Automaton())
24992512
True
25002513
sage: is_FiniteStateMachine(Transducer())
25012514
True
25022515
"""
2516+
from sage.misc.superseded import deprecation
2517+
deprecation(38032, "The function is_FiniteStateMachine is deprecated; use 'isinstance(..., FiniteStateMachine)' instead.")
25032518
return isinstance(FSM, FiniteStateMachine)
25042519

25052520

@@ -3094,7 +3109,7 @@ def __init__(self,
30943109

30953110
self._allow_composition_ = True
30963111

3097-
if is_FiniteStateMachine(data):
3112+
if isinstance(data, FiniteStateMachine):
30983113
if initial_states is not None:
30993114
raise ValueError(
31003115
"initial_states cannot be specified when copying "
@@ -3163,7 +3178,7 @@ def __init__(self,
31633178
if isinstance(iter_transitions, Mapping):
31643179
for (st, transition) in iter_transitions.items():
31653180
self.add_state(st)
3166-
if is_FSMTransition(transition):
3181+
if isinstance(transition, FSMTransition):
31673182
self.add_transition(transition)
31683183
elif isinstance(transition, Mapping):
31693184
self.add_transition(sf, st, **transition)
@@ -3176,7 +3191,7 @@ def __init__(self,
31763191
if isinstance(transition, Iterable):
31773192
L = [sf]
31783193
L.extend(transition)
3179-
elif is_FSMTransition(transition):
3194+
elif isinstance(transition, FSMTransition):
31803195
L = transition
31813196
else:
31823197
L = [sf, transition]
@@ -3187,7 +3202,7 @@ def __init__(self,
31873202
# data is a something that is iterable,
31883203
# items are transitions
31893204
for transition in data:
3190-
if is_FSMTransition(transition):
3205+
if isinstance(transition, FSMTransition):
31913206
self.add_transition(transition)
31923207
elif isinstance(transition, Mapping):
31933208
self.add_transition(transition)
@@ -3598,7 +3613,7 @@ def __or__(self, other):
35983613
...
35993614
TypeError: Can only add finite state machine
36003615
"""
3601-
if is_FiniteStateMachine(other):
3616+
if isinstance(other, FiniteStateMachine):
36023617
return self.disjoint_union(other)
36033618
else:
36043619
raise TypeError("Can only add finite state machine")
@@ -3628,7 +3643,7 @@ def __and__(self, other):
36283643
...
36293644
NotImplementedError
36303645
"""
3631-
if is_FiniteStateMachine(other):
3646+
if isinstance(other, FiniteStateMachine):
36323647
return self.intersection(other)
36333648

36343649
def __imul__(self, other):
@@ -3879,7 +3894,7 @@ def __call__(self, *args, **kwargs):
38793894
"""
38803895
if not args:
38813896
raise TypeError("Called with too few arguments.")
3882-
if is_FiniteStateMachine(args[0]):
3897+
if isinstance(args[0], FiniteStateMachine):
38833898
return self.composition(*args, **kwargs)
38843899
if isinstance(args[0], Iterable):
38853900
if 'full_output' not in kwargs:
@@ -3969,7 +3984,7 @@ def __eq__(self, other):
39693984
sage: F == G
39703985
True
39713986
"""
3972-
if not is_FiniteStateMachine(other):
3987+
if not isinstance(other, FiniteStateMachine):
39733988
return False
39743989
if len(self._states_) != len(other._states_):
39753990
return False
@@ -4052,9 +4067,9 @@ def __contains__(self, item):
40524067
sage: FSMTransition('A', 'B', 0) in F
40534068
True
40544069
"""
4055-
if is_FSMState(item):
4070+
if isinstance(item, FSMState):
40564071
return self.has_state(item)
4057-
if is_FSMTransition(item):
4072+
if isinstance(item, FSMTransition):
40584073
return self.has_transition(item)
40594074
return False
40604075

@@ -5527,7 +5542,7 @@ def what(s, switch):
55275542
return s.label()
55285543
else:
55295544
return s
5530-
switch = is_FSMState(state)
5545+
switch = isinstance(state, FSMState)
55315546

55325547
try:
55335548
return self._states_dict_[what(state, switch)]
@@ -5566,7 +5581,7 @@ def transition(self, transition):
55665581
sage: id(t) == id(F.transition(('A', 'B', 0)))
55675582
True
55685583
"""
5569-
if not is_FSMTransition(transition):
5584+
if not isinstance(transition, FSMTransition):
55705585
transition = FSMTransition(*transition)
55715586
for s in self.iter_transitions(transition.from_state):
55725587
if s == transition:
@@ -5625,7 +5640,7 @@ def has_transition(self, transition):
56255640
...
56265641
TypeError: Transition is not an instance of FSMTransition.
56275642
"""
5628-
if is_FSMTransition(transition):
5643+
if isinstance(transition, FSMTransition):
56295644
return transition in self.iter_transitions()
56305645
raise TypeError("Transition is not an instance of FSMTransition.")
56315646

@@ -6510,7 +6525,7 @@ def add_state(self, state):
65106525
except LookupError:
65116526
pass
65126527
# at this point we know that we have a new state
6513-
if is_FSMState(state):
6528+
if isinstance(state, FSMState):
65146529
s = state
65156530
else:
65166531
s = FSMState(state)
@@ -6610,7 +6625,7 @@ def add_transition(self, *args, **kwargs):
66106625
if len(args) + len(kwargs) == 1:
66116626
if len(args) == 1:
66126627
d = args[0]
6613-
if is_FSMTransition(d):
6628+
if isinstance(d, FSMTransition):
66146629
return self._add_fsm_transition_(d)
66156630
else:
66166631
d = next(iter(kwargs.values()))
@@ -7353,8 +7368,8 @@ def concatenation(self, other):
73537368
....: if C(w)]
73547369
[[0, 1]]
73557370
sage: from sage.combinat.finite_state_machine import (
7356-
....: is_Automaton, is_Transducer)
7357-
sage: is_Automaton(C)
7371+
....: Automaton, Transducer)
7372+
sage: isinstance(C, Automaton)
73587373
True
73597374

73607375
Concatenation of two transducers::
@@ -7377,7 +7392,7 @@ def concatenation(self, other):
73777392
([0, 1], [1, 0]),
73787393
([1, 0], [2, 1]),
73797394
([1, 1], [2, 0])]
7380-
sage: is_Transducer(C)
7395+
sage: isinstance(C, Transducer)
73817396
True
73827397

73837398

@@ -7446,10 +7461,10 @@ def concatenation(self, other):
74467461
TypeError: A finite state machine can only be concatenated
74477462
with a another finite state machine.
74487463
"""
7449-
if not is_FiniteStateMachine(other):
7464+
if not isinstance(other, FiniteStateMachine):
74507465
raise TypeError('A finite state machine can only be concatenated '
74517466
'with a another finite state machine.')
7452-
if is_Automaton(other) != is_Automaton(self):
7467+
if isinstance(other, Automaton) != isinstance(self, Automaton):
74537468
raise TypeError('Cannot concatenate finite state machines of '
74547469
'different types.')
74557470

@@ -7532,7 +7547,7 @@ def kleene_star(self):
75327547
Transition from 1 to 2: 1|-]
75337548
sage: from sage.combinat.finite_state_machine import (
75347549
....: is_Automaton, is_Transducer)
7535-
sage: is_Automaton(B)
7550+
sage: isinstance(B, Automaton)
75367551
True
75377552
sage: [w for w in ([], [0, 1], [0, 1, 0], [0, 1, 0, 1], [0, 1, 1, 1])
75387553
....: if B(w)]
@@ -7550,7 +7565,7 @@ def kleene_star(self):
75507565
[Transition from 0 to 1: 0|1,
75517566
Transition from 0 to 1: 1|0,
75527567
Transition from 1 to 0: -|-]
7553-
sage: is_Transducer(S)
7568+
sage: isinstance(S, Transducer)
75547569
True
75557570
sage: for w in ([], [0], [1], [0, 0], [0, 1]):
75567571
....: print("{} {}".format(w, S.process(w)))
@@ -7827,10 +7842,10 @@ def default_final_function(*args):
78277842
if isinstance(other, Iterable):
78287843
machines = [self]
78297844
machines.extend(other)
7830-
if not all(is_FiniteStateMachine(m) for m in machines):
7845+
if not all(isinstance(m, FiniteStateMachine) for m in machines):
78317846
raise ValueError("other must be a finite state machine "
78327847
"or a list of finite state machines.")
7833-
elif is_FiniteStateMachine(other):
7848+
elif isinstance(other, FiniteStateMachine):
78347849
machines = [self, other]
78357850
else:
78367851
raise ValueError("other must be a finite state machine or "
@@ -8082,9 +8097,9 @@ def composition(self, other, algorithm=None,
80828097
....: is_Automaton, is_Transducer)
80838098
sage: T = Transducer([(0, 0, 0, 0)], initial_states=[0])
80848099
sage: A = Automaton([(0, 0, 0)], initial_states=[0])
8085-
sage: is_Transducer(T.composition(T, algorithm='direct'))
8100+
sage: isinstance(T.composition(T, algorithm='direct'), Transducer)
80868101
True
8087-
sage: is_Transducer(T.composition(T, algorithm='explorative'))
8102+
sage: isinstance(T.composition(T, algorithm='explorative'), Transducer)
80888103
True
80898104
sage: T.composition(A, algorithm='direct')
80908105
Traceback (most recent call last):
@@ -8102,9 +8117,9 @@ def composition(self, other, algorithm=None,
81028117
Traceback (most recent call last):
81038118
...
81048119
TypeError: Composition with automaton is not possible.
8105-
sage: is_Automaton(A.composition(T, algorithm='direct'))
8120+
sage: isinstance(A.composition(T, algorithm='direct'), Automaton)
81068121
True
8107-
sage: is_Automaton(A.composition(T, algorithm='explorative'))
8122+
sage: isinstance(A.composition(T, algorithm='explorative'), Automaton)
81088123
True
81098124

81108125
Non-deterministic final output cannot be handled::
@@ -10926,12 +10941,20 @@ def is_Automaton(FSM):
1092610941

1092710942
sage: from sage.combinat.finite_state_machine import is_FiniteStateMachine, is_Automaton
1092810943
sage: is_Automaton(FiniteStateMachine())
10944+
doctest:warning...
10945+
DeprecationWarning: The function is_Automaton is deprecated; use 'isinstance(..., Automaton)' instead.
10946+
See https://github.com/sagemath/sage/issues/38032 for details.
1092910947
False
1093010948
sage: is_Automaton(Automaton())
1093110949
True
1093210950
sage: is_FiniteStateMachine(Automaton())
10951+
doctest:warning...
10952+
DeprecationWarning: The function is_FiniteStateMachine is deprecated; use 'isinstance(..., FiniteStateMachine)' instead.
10953+
See https://github.com/sagemath/sage/issues/38032 for details.
1093310954
True
1093410955
"""
10956+
from sage.misc.superseded import deprecation
10957+
deprecation(38032, "The function is_Automaton is deprecated; use 'isinstance(..., Automaton)' instead.")
1093510958
return isinstance(FSM, Automaton)
1093610959

1093710960

@@ -11145,7 +11168,7 @@ def intersection(self, other, only_accessible_components=True):
1114511168
sage: a1.remove_epsilon_transitions() # not tested (since not implemented yet)
1114611169
sage: a1.intersection(a2) # not tested
1114711170
"""
11148-
if not is_Automaton(other):
11171+
if not isinstance(other, Automaton):
1114911172
raise TypeError(
1115011173
"Only an automaton can be intersected with an automaton.")
1115111174

@@ -12131,12 +12154,20 @@ def is_Transducer(FSM):
1213112154

1213212155
sage: from sage.combinat.finite_state_machine import is_FiniteStateMachine, is_Transducer
1213312156
sage: is_Transducer(FiniteStateMachine())
12157+
doctest:warning...
12158+
DeprecationWarning: The function is_Transducer is deprecated; use 'isinstance(..., Transducer)' instead.
12159+
See https://github.com/sagemath/sage/issues/38032 for details.
1213412160
False
1213512161
sage: is_Transducer(Transducer())
1213612162
True
1213712163
sage: is_FiniteStateMachine(Transducer())
12164+
doctest:warning...
12165+
DeprecationWarning: The function is_FiniteStateMachine is deprecated; use 'isinstance(..., FiniteStateMachine)' instead.
12166+
See https://github.com/sagemath/sage/issues/38032 for details.
1213812167
True
1213912168
"""
12169+
from sage.misc.superseded import deprecation
12170+
deprecation(38032, "The function is_Transducer is deprecated; use 'isinstance(..., Transducer)' instead.")
1214012171
return isinstance(FSM, Transducer)
1214112172

1214212173

@@ -12345,7 +12376,7 @@ def intersection(self, other, only_accessible_components=True):
1234512376
Transducers*, chapter in *Handbook of Finite State Based Models and
1234612377
Applications*, edited by Jiacun Wang, Chapman and Hall/CRC, 2012.
1234712378
"""
12348-
if not is_Transducer(other):
12379+
if not isinstance(other, Transducer):
1234912380
raise TypeError(
1235012381
"Only a transducer can be intersected with a transducer.")
1235112382

@@ -13874,8 +13905,13 @@ def is_FSMProcessIterator(PI):
1387413905

1387513906
sage: from sage.combinat.finite_state_machine import is_FSMProcessIterator, FSMProcessIterator
1387613907
sage: is_FSMProcessIterator(FSMProcessIterator(FiniteStateMachine([[0, 0, 0, 0]], initial_states=[0]), []))
13908+
doctest:warning...
13909+
DeprecationWarning: The function is_FSMProcessIterator is deprecated; use 'isinstance(..., FSMProcessIterator)' instead.
13910+
See https://github.com/sagemath/sage/issues/38032 for details.
1387713911
True
1387813912
"""
13913+
from sage.misc.superseded import deprecation
13914+
deprecation(38032, "The function is_FSMProcessIterator is deprecated; use 'isinstance(..., FSMProcessIterator)' instead.")
1387913915
return isinstance(PI, FSMProcessIterator)
1388013916

1388113917

0 commit comments

Comments
 (0)