diff --git a/pyformlang/cfg/tests/test_cfg.py b/pyformlang/cfg/tests/test_cfg.py index f214572..c0e3cdd 100644 --- a/pyformlang/cfg/tests/test_cfg.py +++ b/pyformlang/cfg/tests/test_cfg.py @@ -377,7 +377,7 @@ def test_reverse(self) -> None: assert len(new_cfg.variables) == 1 assert len(new_cfg.terminals) == 2 assert len(new_cfg.productions) == 2 - assert not (not new_cfg) + assert new_cfg assert new_cfg.contains([ter_b, ter_b, ter_a, ter_a]) def test_emptiness(self) -> None: @@ -974,7 +974,7 @@ def test_start_symbol(self) -> None: assert not cfg.start_symbol -def get_example_text_duplicate(): +def get_example_text_duplicate() -> str: """Duplicate text.""" text = """ E -> T E’ diff --git a/pyformlang/cfg/tests/test_llone_parser.py b/pyformlang/cfg/tests/test_llone_parser.py index 4ef2a43..fa3db46 100644 --- a/pyformlang/cfg/tests/test_llone_parser.py +++ b/pyformlang/cfg/tests/test_llone_parser.py @@ -311,12 +311,12 @@ def test_get_llone_rightmost_derivation(self) -> None: def test_save_tree(self) -> None: text = """ - E -> T E' - E' -> + T E' | epsilon - T -> F T' - T' -> * F T' | epsilon - F -> ( E ) | id - """ + E -> T E' + E' -> + T E' | epsilon + T -> F T' + T' -> * F T' | epsilon + F -> ( E ) | id + """ cfg = CFG.from_text(text, start_symbol="E") llone_parser = LLOneParser(cfg) parse_tree = llone_parser.get_llone_parse_tree( diff --git a/pyformlang/cfg/tests/test_recursive_decent_parser.py b/pyformlang/cfg/tests/test_recursive_decent_parser.py index 176ae39..fcc5e5d 100644 --- a/pyformlang/cfg/tests/test_recursive_decent_parser.py +++ b/pyformlang/cfg/tests/test_recursive_decent_parser.py @@ -10,14 +10,14 @@ @pytest.fixture -def parser(): +def parser() -> RecursiveDecentParser: cfg = CFG.from_text(""" E -> S + S E -> S * S S -> ( E ) S -> int """) - yield RecursiveDecentParser(cfg) + return RecursiveDecentParser(cfg) class TestRecursiveDecentParser: diff --git a/pyformlang/fcfg/tests/test_feature_structure.py b/pyformlang/fcfg/tests/test_feature_structure.py index 8656870..9ebd8c8 100644 --- a/pyformlang/fcfg/tests/test_feature_structure.py +++ b/pyformlang/fcfg/tests/test_feature_structure.py @@ -10,7 +10,7 @@ ) -def _get_agreement_subject_number_person(): +def _get_agreement_subject_number_person() -> FeatureStructure: fs2 = FeatureStructure.from_text( "AGREEMENT=(1)[NUMBER=sg, PERSON=3], SUBJECT=[AGREEMENT->(1)]" ) @@ -205,7 +205,7 @@ def test_copy(self) -> None: copy_of_copy = fs1_copy2.copy() self._assertions_test_copy(copy_of_copy) - def _assertions_test_copy(self, fs1_copy) -> None: + def _assertions_test_copy(self, fs1_copy: FeatureStructure) -> None: assert ( fs1_copy.get_feature_by_path(["AGREEMENT", "NUMBER"]).value == "sg" ) diff --git a/pyformlang/finite_automaton/tests/test_deterministic_finite_automaton.py b/pyformlang/finite_automaton/tests/test_deterministic_finite_automaton.py index 3ab5900..1b24df7 100644 --- a/pyformlang/finite_automaton/tests/test_deterministic_finite_automaton.py +++ b/pyformlang/finite_automaton/tests/test_deterministic_finite_automaton.py @@ -296,7 +296,7 @@ def test_dfa_generating_no_words(self) -> None: assert not accepted_words -def get_example0(): +def get_example0() -> DeterministicFiniteAutomaton: """Gives a dfa.""" dfa = DeterministicFiniteAutomaton() state0 = State(0) @@ -317,7 +317,7 @@ def get_example0(): return dfa -def get_example0_bis(): +def get_example0_bis() -> DeterministicFiniteAutomaton: """Gives a dfa.""" dfa = DeterministicFiniteAutomaton() dfa.add_start_state(0) @@ -330,7 +330,7 @@ def get_example0_bis(): return dfa -def get_dfa_example(): +def get_dfa_example() -> DeterministicFiniteAutomaton: """An example of DFA.""" dfa1 = DeterministicFiniteAutomaton() dfa1.add_transitions( @@ -341,7 +341,7 @@ def get_dfa_example(): return dfa1 -def get_dfa_example_for_word_generation(): +def get_dfa_example_for_word_generation() -> DeterministicFiniteAutomaton: """DFA example for the word generation test.""" dfa = DeterministicFiniteAutomaton() states = [State(x) for x in range(4)] @@ -364,7 +364,7 @@ def get_dfa_example_for_word_generation(): return dfa -def get_cyclic_dfa_example(): +def get_cyclic_dfa_example() -> DeterministicFiniteAutomaton: """Gets DFA example with several cycles on path to final.""" dfa = DeterministicFiniteAutomaton(start_state=0, final_states={3}) dfa.add_transitions( @@ -380,7 +380,7 @@ def get_cyclic_dfa_example(): return dfa -def get_dfa_example_without_accepted_words(): +def get_dfa_example_without_accepted_words() -> DeterministicFiniteAutomaton: """DFA example accepting no words.""" dfa = DeterministicFiniteAutomaton() states = [State(x) for x in range(4)] diff --git a/pyformlang/finite_automaton/tests/test_epsilon_nfa.py b/pyformlang/finite_automaton/tests/test_epsilon_nfa.py index db8e3c3..e480389 100644 --- a/pyformlang/finite_automaton/tests/test_epsilon_nfa.py +++ b/pyformlang/finite_automaton/tests/test_epsilon_nfa.py @@ -1,5 +1,6 @@ """Tests for epsilon NFA.""" +from typing import List, Tuple import copy import networkx @@ -101,6 +102,7 @@ def test_union0(self) -> None: def test_union1(self) -> None: """Tests the union of three ENFAs. + Union is (a*b)|(ab+)|c. """ enfa0 = get_enfa_example0() @@ -133,6 +135,7 @@ def test_concatenate0(self) -> None: def test_concatenate1(self) -> None: """Tests the concatenation of three ENFAs. + Concatenation is a*bc((ab+)|c). """ enfa0 = get_enfa_example0() @@ -162,6 +165,7 @@ def test_kleene0(self) -> None: def test_kleene1(self) -> None: """Tests the kleene star of an ENFA. + Expression is ((ab+)|c)*. """ enfa = get_enfa_example2() @@ -560,7 +564,8 @@ def test_max_length_zero_not_accepting_empty_string(self) -> None: assert not accepted_words -def get_digits_enfa(): +def get_digits_enfa() -> Tuple[EpsilonNFA, List[Symbol], + Symbol, Symbol, Symbol, Symbol]: """An epsilon NFA to recognize digits.""" epsilon = Epsilon() plus = Symbol("+") @@ -593,8 +598,9 @@ def get_digits_enfa(): return enfa, digits, epsilon, plus, minus, point -def get_enfa_example0(): - """Gives an example ENFA +def get_enfa_example0() -> EpsilonNFA: + """Gives an example ENFA. + Accepts a*b. """ enfa0 = EpsilonNFA() @@ -611,8 +617,9 @@ def get_enfa_example0(): return enfa0 -def get_enfa_example1(): - """Gives an example ENFA +def get_enfa_example1() -> EpsilonNFA: + """Gives an example ENFA. + Accepts c. """ enfa1 = EpsilonNFA() @@ -625,8 +632,9 @@ def get_enfa_example1(): return enfa1 -def get_enfa_example2(): - """Gives an example ENFA +def get_enfa_example2() -> EpsilonNFA: + """Gives an example ENFA. + Accepts (ab+)|c. """ enfa = EpsilonNFA(start_states={0, 3}, final_states={2, 4}) @@ -634,7 +642,7 @@ def get_enfa_example2(): return enfa -def get_enfa_example0_bis(): +def get_enfa_example0_bis() -> EpsilonNFA: """A non minimal NFA, equivalent to example0.""" enfa0 = EpsilonNFA() state3 = State(3) @@ -657,7 +665,7 @@ def get_enfa_example0_bis(): return enfa0 -def get_example_non_minimal(): +def get_example_non_minimal() -> EpsilonNFA: """A non minimal example a.a*.b.""" enfa0 = EpsilonNFA() state0 = State(0) @@ -684,7 +692,7 @@ def get_example_non_minimal(): return enfa0 -def get_enfa_example_for_word_generation(): +def get_enfa_example_for_word_generation() -> EpsilonNFA: """ENFA example for the word generation test.""" enfa = EpsilonNFA() states = [State(x) for x in range(9)] @@ -717,7 +725,7 @@ def get_enfa_example_for_word_generation(): return enfa -def get_cyclic_enfa_example(): +def get_cyclic_enfa_example() -> EpsilonNFA: """ENFA example with a cycle on the path to the final state.""" enfa = EpsilonNFA() states = [State(x) for x in range(4)] @@ -738,7 +746,7 @@ def get_cyclic_enfa_example(): return enfa -def get_epsilon_cycle_enfa_example(): +def get_epsilon_cycle_enfa_example() -> EpsilonNFA: """ENFA example with an epsilon cycle.""" enfa = EpsilonNFA() states = [State(x) for x in range(4)] diff --git a/pyformlang/finite_automaton/tests/test_nondeterministic_finite_automaton.py b/pyformlang/finite_automaton/tests/test_nondeterministic_finite_automaton.py index 7f36944..4d77f6b 100644 --- a/pyformlang/finite_automaton/tests/test_nondeterministic_finite_automaton.py +++ b/pyformlang/finite_automaton/tests/test_nondeterministic_finite_automaton.py @@ -163,10 +163,8 @@ def test_start_state_at_the_end_generation(self) -> None: assert len(accepted_words) == 5 -def get_nfa_example_for_word_generation(): - """Gets Nondeterministic Finite Automaton \ - example for the word generation test. - """ +def get_nfa_example_for_word_generation() -> NondeterministicFiniteAutomaton: + """Gets NFA example for the word generation test.""" nfa = NondeterministicFiniteAutomaton( start_states={0, 4}, final_states={3, 4, 6, 8} ) @@ -186,7 +184,7 @@ def get_nfa_example_for_word_generation(): return nfa -def get_nfa_example_with_duplicates(): +def get_nfa_example_with_duplicates() -> NondeterministicFiniteAutomaton: """Gets NFA example with duplicate word chains.""" nfa = NondeterministicFiniteAutomaton( start_states={0, 1, 5, 6}, final_states={3, 4, 8} @@ -205,7 +203,7 @@ def get_nfa_example_with_duplicates(): return nfa -def get_cyclic_nfa_example(): +def get_cyclic_nfa_example() -> NondeterministicFiniteAutomaton: """Gets NFA example with several cycles on path to final.""" nfa = NondeterministicFiniteAutomaton(start_states={0, 5}, final_states={4}) nfa.add_transitions( @@ -223,7 +221,8 @@ def get_cyclic_nfa_example(): return nfa -def get_nfa_example_with_final_state_at_start(): +def get_nfa_example_with_final_state_at_start() \ + -> NondeterministicFiniteAutomaton: """Gets NFA example with final state at start.""" nfa = NondeterministicFiniteAutomaton(start_states={0, 5}, final_states={0}) nfa.add_transitions( @@ -239,7 +238,8 @@ def get_nfa_example_with_final_state_at_start(): return nfa -def get_nfa_example_with_start_state_at_the_end(): +def get_nfa_example_with_start_state_at_the_end() \ + -> NondeterministicFiniteAutomaton: """Gets NFA example with start state at the end.""" nfa = NondeterministicFiniteAutomaton( start_states={0, 3, 4}, final_states={3} diff --git a/pyformlang/fst/tests/test_fst.py b/pyformlang/fst/tests/test_fst.py index 707e52a..578ba49 100644 --- a/pyformlang/fst/tests/test_fst.py +++ b/pyformlang/fst/tests/test_fst.py @@ -9,21 +9,21 @@ @pytest.fixture -def fst0(): - fst0 = FST() - fst0.add_start_state("q0") - fst0.add_transition("q0", "a", "q1", ["b"]) - fst0.add_final_state("q1") - yield fst0 +def fst0() -> FST: + fst = FST() + fst.add_start_state("q0") + fst.add_transition("q0", "a", "q1", ["b"]) + fst.add_final_state("q1") + return fst @pytest.fixture -def fst1(): - fst1 = FST() - fst1.add_start_state("q1") - fst1.add_transition("q1", "b", "q2", ["c"]) - fst1.add_final_state("q2") - yield fst1 +def fst1() -> FST: + fst = FST() + fst.add_start_state("q1") + fst.add_transition("q1", "b", "q2", ["c"]) + fst.add_final_state("q2") + return fst class TestFST: @@ -93,14 +93,14 @@ def test_translate(self) -> None: assert ["b", "c"] in translation assert ["b"] + ["c"] * 9 in translation - def test_union(self, fst0, fst1) -> None: + def test_union(self, fst0: FST, fst1: FST) -> None: """Tests the union.""" fst_union = fst0.union(fst1) self._make_test_fst_union(fst_union) fst_union = fst0 | fst1 self._make_test_fst_union(fst_union) - def _make_test_fst_union(self, fst_union) -> None: + def _make_test_fst_union(self, fst_union: FST) -> None: assert len(fst_union.start_states) == 2 assert len(fst_union.final_states) == 2 assert fst_union.get_number_transitions() == 2 @@ -111,7 +111,7 @@ def _make_test_fst_union(self, fst_union) -> None: translation = list(fst_union.translate(["a", "b"])) assert translation == [] - def test_concatenate(self, fst0, fst1) -> None: + def test_concatenate(self, fst0: FST, fst1: FST) -> None: """Tests the concatenation.""" fst_concatenate = fst0 + fst1 translation = list(fst_concatenate.translate(["a", "b"])) @@ -121,7 +121,7 @@ def test_concatenate(self, fst0, fst1) -> None: translation = list(fst_concatenate.translate(["b"])) assert translation == [] - def test_concatenate2(self, fst0, fst1) -> None: + def test_concatenate2(self, fst0: FST, fst1: FST) -> None: """Tests the concatenation.""" fst_concatenate = fst0 + fst1 + fst1 translation = list(fst_concatenate.translate(["a", "b", "b"])) @@ -131,7 +131,7 @@ def test_concatenate2(self, fst0, fst1) -> None: translation = list(fst_concatenate.translate(["b"])) assert translation == [] - def test_kleene_start(self, fst0) -> None: + def test_kleene_start(self, fst0: FST) -> None: """Tests the kleene star on a fst.""" fst_star = fst0.kleene_star() translation = list(fst_star.translate(["a"])) diff --git a/pyformlang/indexed_grammar/tests/test_indexed_grammar.py b/pyformlang/indexed_grammar/tests/test_indexed_grammar.py index 8ef444d..29d9be2 100644 --- a/pyformlang/indexed_grammar/tests/test_indexed_grammar.py +++ b/pyformlang/indexed_grammar/tests/test_indexed_grammar.py @@ -1,6 +1,8 @@ """Testing of indexed grammar, with manual rules.""" -from pyformlang.indexed_grammar import Rules +from typing import Iterable + +from pyformlang.indexed_grammar import Rules, ReducedRule from pyformlang.indexed_grammar import ConsumptionRule from pyformlang.indexed_grammar import EndRule from pyformlang.indexed_grammar import ProductionRule @@ -343,7 +345,8 @@ def test_removal_useless(self) -> None: ) def test_intersection0(self) -> None: - """Tests the intersection of indexed grammar with regex + """Tests the intersection of indexed grammar with regex. + Long to run! """ l_rules = [ @@ -389,7 +392,7 @@ def test_intersection1(self) -> None: assert not intersection.is_empty() -def get_example_rules(): +def get_example_rules() -> Iterable[ReducedRule]: """Duplicate example of rules.""" l_rules = [ # Initialization rules ProductionRule("S", "Cinit", "end"), diff --git a/pyformlang/regular_expression/tests/test_python_regex.py b/pyformlang/regular_expression/tests/test_python_regex.py index 5808e41..55b6a1a 100644 --- a/pyformlang/regular_expression/tests/test_python_regex.py +++ b/pyformlang/regular_expression/tests/test_python_regex.py @@ -201,7 +201,7 @@ def test_shortcut_word(self) -> None: assert regex.accepts(["a", "A"]) assert regex.accepts(["a", "f"]) - def _test_compare(self, regex, s_test) -> None: + def _test_compare(self, regex: str, s_test: str) -> None: r_pyformlang = PythonRegex(regex) r_python = re.compile(regex) assert (r_python.fullmatch(s_test) is not None) == r_pyformlang.accepts(