Skip to content

Commit d3d315a

Browse files
committed
add missing annotations, correct existing docstrings
1 parent d911fcf commit d3d315a

File tree

10 files changed

+70
-59
lines changed

10 files changed

+70
-59
lines changed

pyformlang/cfg/tests/test_cfg.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def test_reverse(self) -> None:
377377
assert len(new_cfg.variables) == 1
378378
assert len(new_cfg.terminals) == 2
379379
assert len(new_cfg.productions) == 2
380-
assert not (not new_cfg)
380+
assert new_cfg
381381
assert new_cfg.contains([ter_b, ter_b, ter_a, ter_a])
382382

383383
def test_emptiness(self) -> None:
@@ -974,7 +974,7 @@ def test_start_symbol(self) -> None:
974974
assert not cfg.start_symbol
975975

976976

977-
def get_example_text_duplicate():
977+
def get_example_text_duplicate() -> str:
978978
"""Duplicate text."""
979979
text = """
980980
E -> T E’

pyformlang/cfg/tests/test_llone_parser.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,12 +311,12 @@ def test_get_llone_rightmost_derivation(self) -> None:
311311

312312
def test_save_tree(self) -> None:
313313
text = """
314-
E -> T E'
315-
E' -> + T E' | epsilon
316-
T -> F T'
317-
T' -> * F T' | epsilon
318-
F -> ( E ) | id
319-
"""
314+
E -> T E'
315+
E' -> + T E' | epsilon
316+
T -> F T'
317+
T' -> * F T' | epsilon
318+
F -> ( E ) | id
319+
"""
320320
cfg = CFG.from_text(text, start_symbol="E")
321321
llone_parser = LLOneParser(cfg)
322322
parse_tree = llone_parser.get_llone_parse_tree(

pyformlang/cfg/tests/test_recursive_decent_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111

1212
@pytest.fixture
13-
def parser():
13+
def parser() -> RecursiveDecentParser:
1414
cfg = CFG.from_text("""
1515
E -> S + S
1616
E -> S * S
1717
S -> ( E )
1818
S -> int
1919
""")
20-
yield RecursiveDecentParser(cfg)
20+
return RecursiveDecentParser(cfg)
2121

2222

2323
class TestRecursiveDecentParser:

pyformlang/fcfg/tests/test_feature_structure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
)
1111

1212

13-
def _get_agreement_subject_number_person():
13+
def _get_agreement_subject_number_person() -> FeatureStructure:
1414
fs2 = FeatureStructure.from_text(
1515
"AGREEMENT=(1)[NUMBER=sg, PERSON=3], SUBJECT=[AGREEMENT->(1)]"
1616
)
@@ -205,7 +205,7 @@ def test_copy(self) -> None:
205205
copy_of_copy = fs1_copy2.copy()
206206
self._assertions_test_copy(copy_of_copy)
207207

208-
def _assertions_test_copy(self, fs1_copy) -> None:
208+
def _assertions_test_copy(self, fs1_copy: FeatureStructure) -> None:
209209
assert (
210210
fs1_copy.get_feature_by_path(["AGREEMENT", "NUMBER"]).value == "sg"
211211
)

pyformlang/finite_automaton/tests/test_deterministic_finite_automaton.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def test_dfa_generating_no_words(self) -> None:
296296
assert not accepted_words
297297

298298

299-
def get_example0():
299+
def get_example0() -> DeterministicFiniteAutomaton:
300300
"""Gives a dfa."""
301301
dfa = DeterministicFiniteAutomaton()
302302
state0 = State(0)
@@ -317,7 +317,7 @@ def get_example0():
317317
return dfa
318318

319319

320-
def get_example0_bis():
320+
def get_example0_bis() -> DeterministicFiniteAutomaton:
321321
"""Gives a dfa."""
322322
dfa = DeterministicFiniteAutomaton()
323323
dfa.add_start_state(0)
@@ -330,7 +330,7 @@ def get_example0_bis():
330330
return dfa
331331

332332

333-
def get_dfa_example():
333+
def get_dfa_example() -> DeterministicFiniteAutomaton:
334334
"""An example of DFA."""
335335
dfa1 = DeterministicFiniteAutomaton()
336336
dfa1.add_transitions(
@@ -341,7 +341,7 @@ def get_dfa_example():
341341
return dfa1
342342

343343

344-
def get_dfa_example_for_word_generation():
344+
def get_dfa_example_for_word_generation() -> DeterministicFiniteAutomaton:
345345
"""DFA example for the word generation test."""
346346
dfa = DeterministicFiniteAutomaton()
347347
states = [State(x) for x in range(4)]
@@ -364,7 +364,7 @@ def get_dfa_example_for_word_generation():
364364
return dfa
365365

366366

367-
def get_cyclic_dfa_example():
367+
def get_cyclic_dfa_example() -> DeterministicFiniteAutomaton:
368368
"""Gets DFA example with several cycles on path to final."""
369369
dfa = DeterministicFiniteAutomaton(start_state=0, final_states={3})
370370
dfa.add_transitions(
@@ -380,7 +380,7 @@ def get_cyclic_dfa_example():
380380
return dfa
381381

382382

383-
def get_dfa_example_without_accepted_words():
383+
def get_dfa_example_without_accepted_words() -> DeterministicFiniteAutomaton:
384384
"""DFA example accepting no words."""
385385
dfa = DeterministicFiniteAutomaton()
386386
states = [State(x) for x in range(4)]

pyformlang/finite_automaton/tests/test_epsilon_nfa.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Tests for epsilon NFA."""
22

3+
from typing import List, Tuple
34
import copy
45
import networkx
56

@@ -101,6 +102,7 @@ def test_union0(self) -> None:
101102

102103
def test_union1(self) -> None:
103104
"""Tests the union of three ENFAs.
105+
104106
Union is (a*b)|(ab+)|c.
105107
"""
106108
enfa0 = get_enfa_example0()
@@ -133,6 +135,7 @@ def test_concatenate0(self) -> None:
133135

134136
def test_concatenate1(self) -> None:
135137
"""Tests the concatenation of three ENFAs.
138+
136139
Concatenation is a*bc((ab+)|c).
137140
"""
138141
enfa0 = get_enfa_example0()
@@ -162,6 +165,7 @@ def test_kleene0(self) -> None:
162165

163166
def test_kleene1(self) -> None:
164167
"""Tests the kleene star of an ENFA.
168+
165169
Expression is ((ab+)|c)*.
166170
"""
167171
enfa = get_enfa_example2()
@@ -560,7 +564,8 @@ def test_max_length_zero_not_accepting_empty_string(self) -> None:
560564
assert not accepted_words
561565

562566

563-
def get_digits_enfa():
567+
def get_digits_enfa() -> Tuple[EpsilonNFA, List[Symbol],
568+
Symbol, Symbol, Symbol, Symbol]:
564569
"""An epsilon NFA to recognize digits."""
565570
epsilon = Epsilon()
566571
plus = Symbol("+")
@@ -593,8 +598,9 @@ def get_digits_enfa():
593598
return enfa, digits, epsilon, plus, minus, point
594599

595600

596-
def get_enfa_example0():
597-
"""Gives an example ENFA
601+
def get_enfa_example0() -> EpsilonNFA:
602+
"""Gives an example ENFA.
603+
598604
Accepts a*b.
599605
"""
600606
enfa0 = EpsilonNFA()
@@ -611,8 +617,9 @@ def get_enfa_example0():
611617
return enfa0
612618

613619

614-
def get_enfa_example1():
615-
"""Gives an example ENFA
620+
def get_enfa_example1() -> EpsilonNFA:
621+
"""Gives an example ENFA.
622+
616623
Accepts c.
617624
"""
618625
enfa1 = EpsilonNFA()
@@ -625,16 +632,17 @@ def get_enfa_example1():
625632
return enfa1
626633

627634

628-
def get_enfa_example2():
629-
"""Gives an example ENFA
635+
def get_enfa_example2() -> EpsilonNFA:
636+
"""Gives an example ENFA.
637+
630638
Accepts (ab+)|c.
631639
"""
632640
enfa = EpsilonNFA(start_states={0, 3}, final_states={2, 4})
633641
enfa.add_transitions([(0, "a", 1), (1, "b", 2), (2, "b", 2), (3, "c", 4)])
634642
return enfa
635643

636644

637-
def get_enfa_example0_bis():
645+
def get_enfa_example0_bis() -> EpsilonNFA:
638646
"""A non minimal NFA, equivalent to example0."""
639647
enfa0 = EpsilonNFA()
640648
state3 = State(3)
@@ -657,7 +665,7 @@ def get_enfa_example0_bis():
657665
return enfa0
658666

659667

660-
def get_example_non_minimal():
668+
def get_example_non_minimal() -> EpsilonNFA:
661669
"""A non minimal example a.a*.b."""
662670
enfa0 = EpsilonNFA()
663671
state0 = State(0)
@@ -684,7 +692,7 @@ def get_example_non_minimal():
684692
return enfa0
685693

686694

687-
def get_enfa_example_for_word_generation():
695+
def get_enfa_example_for_word_generation() -> EpsilonNFA:
688696
"""ENFA example for the word generation test."""
689697
enfa = EpsilonNFA()
690698
states = [State(x) for x in range(9)]
@@ -717,7 +725,7 @@ def get_enfa_example_for_word_generation():
717725
return enfa
718726

719727

720-
def get_cyclic_enfa_example():
728+
def get_cyclic_enfa_example() -> EpsilonNFA:
721729
"""ENFA example with a cycle on the path to the final state."""
722730
enfa = EpsilonNFA()
723731
states = [State(x) for x in range(4)]
@@ -738,7 +746,7 @@ def get_cyclic_enfa_example():
738746
return enfa
739747

740748

741-
def get_epsilon_cycle_enfa_example():
749+
def get_epsilon_cycle_enfa_example() -> EpsilonNFA:
742750
"""ENFA example with an epsilon cycle."""
743751
enfa = EpsilonNFA()
744752
states = [State(x) for x in range(4)]

pyformlang/finite_automaton/tests/test_nondeterministic_finite_automaton.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,8 @@ def test_start_state_at_the_end_generation(self) -> None:
163163
assert len(accepted_words) == 5
164164

165165

166-
def get_nfa_example_for_word_generation():
167-
"""Gets Nondeterministic Finite Automaton \
168-
example for the word generation test.
169-
"""
166+
def get_nfa_example_for_word_generation() -> NondeterministicFiniteAutomaton:
167+
"""Gets NFA example for the word generation test."""
170168
nfa = NondeterministicFiniteAutomaton(
171169
start_states={0, 4}, final_states={3, 4, 6, 8}
172170
)
@@ -186,7 +184,7 @@ def get_nfa_example_for_word_generation():
186184
return nfa
187185

188186

189-
def get_nfa_example_with_duplicates():
187+
def get_nfa_example_with_duplicates() -> NondeterministicFiniteAutomaton:
190188
"""Gets NFA example with duplicate word chains."""
191189
nfa = NondeterministicFiniteAutomaton(
192190
start_states={0, 1, 5, 6}, final_states={3, 4, 8}
@@ -205,7 +203,7 @@ def get_nfa_example_with_duplicates():
205203
return nfa
206204

207205

208-
def get_cyclic_nfa_example():
206+
def get_cyclic_nfa_example() -> NondeterministicFiniteAutomaton:
209207
"""Gets NFA example with several cycles on path to final."""
210208
nfa = NondeterministicFiniteAutomaton(start_states={0, 5}, final_states={4})
211209
nfa.add_transitions(
@@ -223,7 +221,8 @@ def get_cyclic_nfa_example():
223221
return nfa
224222

225223

226-
def get_nfa_example_with_final_state_at_start():
224+
def get_nfa_example_with_final_state_at_start() \
225+
-> NondeterministicFiniteAutomaton:
227226
"""Gets NFA example with final state at start."""
228227
nfa = NondeterministicFiniteAutomaton(start_states={0, 5}, final_states={0})
229228
nfa.add_transitions(
@@ -239,7 +238,8 @@ def get_nfa_example_with_final_state_at_start():
239238
return nfa
240239

241240

242-
def get_nfa_example_with_start_state_at_the_end():
241+
def get_nfa_example_with_start_state_at_the_end() \
242+
-> NondeterministicFiniteAutomaton:
243243
"""Gets NFA example with start state at the end."""
244244
nfa = NondeterministicFiniteAutomaton(
245245
start_states={0, 3, 4}, final_states={3}

pyformlang/fst/tests/test_fst.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99

1010

1111
@pytest.fixture
12-
def fst0():
13-
fst0 = FST()
14-
fst0.add_start_state("q0")
15-
fst0.add_transition("q0", "a", "q1", ["b"])
16-
fst0.add_final_state("q1")
17-
yield fst0
12+
def fst0() -> FST:
13+
fst = FST()
14+
fst.add_start_state("q0")
15+
fst.add_transition("q0", "a", "q1", ["b"])
16+
fst.add_final_state("q1")
17+
return fst
1818

1919

2020
@pytest.fixture
21-
def fst1():
22-
fst1 = FST()
23-
fst1.add_start_state("q1")
24-
fst1.add_transition("q1", "b", "q2", ["c"])
25-
fst1.add_final_state("q2")
26-
yield fst1
21+
def fst1() -> FST:
22+
fst = FST()
23+
fst.add_start_state("q1")
24+
fst.add_transition("q1", "b", "q2", ["c"])
25+
fst.add_final_state("q2")
26+
return fst
2727

2828

2929
class TestFST:
@@ -93,14 +93,14 @@ def test_translate(self) -> None:
9393
assert ["b", "c"] in translation
9494
assert ["b"] + ["c"] * 9 in translation
9595

96-
def test_union(self, fst0, fst1) -> None:
96+
def test_union(self, fst0: FST, fst1: FST) -> None:
9797
"""Tests the union."""
9898
fst_union = fst0.union(fst1)
9999
self._make_test_fst_union(fst_union)
100100
fst_union = fst0 | fst1
101101
self._make_test_fst_union(fst_union)
102102

103-
def _make_test_fst_union(self, fst_union) -> None:
103+
def _make_test_fst_union(self, fst_union: FST) -> None:
104104
assert len(fst_union.start_states) == 2
105105
assert len(fst_union.final_states) == 2
106106
assert fst_union.get_number_transitions() == 2
@@ -111,7 +111,7 @@ def _make_test_fst_union(self, fst_union) -> None:
111111
translation = list(fst_union.translate(["a", "b"]))
112112
assert translation == []
113113

114-
def test_concatenate(self, fst0, fst1) -> None:
114+
def test_concatenate(self, fst0: FST, fst1: FST) -> None:
115115
"""Tests the concatenation."""
116116
fst_concatenate = fst0 + fst1
117117
translation = list(fst_concatenate.translate(["a", "b"]))
@@ -121,7 +121,7 @@ def test_concatenate(self, fst0, fst1) -> None:
121121
translation = list(fst_concatenate.translate(["b"]))
122122
assert translation == []
123123

124-
def test_concatenate2(self, fst0, fst1) -> None:
124+
def test_concatenate2(self, fst0: FST, fst1: FST) -> None:
125125
"""Tests the concatenation."""
126126
fst_concatenate = fst0 + fst1 + fst1
127127
translation = list(fst_concatenate.translate(["a", "b", "b"]))
@@ -131,7 +131,7 @@ def test_concatenate2(self, fst0, fst1) -> None:
131131
translation = list(fst_concatenate.translate(["b"]))
132132
assert translation == []
133133

134-
def test_kleene_start(self, fst0) -> None:
134+
def test_kleene_start(self, fst0: FST) -> None:
135135
"""Tests the kleene star on a fst."""
136136
fst_star = fst0.kleene_star()
137137
translation = list(fst_star.translate(["a"]))

0 commit comments

Comments
 (0)