Skip to content

Commit

Permalink
add missing annotations, correct existing docstrings
Browse files Browse the repository at this point in the history
  • Loading branch information
bygu4 committed Jan 4, 2025
1 parent d911fcf commit d3d315a
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 59 deletions.
4 changes: 2 additions & 2 deletions pyformlang/cfg/tests/test_cfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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’
Expand Down
12 changes: 6 additions & 6 deletions pyformlang/cfg/tests/test_llone_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions pyformlang/cfg/tests/test_recursive_decent_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pyformlang/fcfg/tests/test_feature_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)]"
)
Expand Down Expand Up @@ -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"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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)]
Expand All @@ -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(
Expand All @@ -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)]
Expand Down
32 changes: 20 additions & 12 deletions pyformlang/finite_automaton/tests/test_epsilon_nfa.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for epsilon NFA."""

from typing import List, Tuple
import copy
import networkx

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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("+")
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -625,16 +632,17 @@ 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})
enfa.add_transitions([(0, "a", 1), (1, "b", 2), (2, "b", 2), (3, "c", 4)])
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)
Expand All @@ -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)
Expand All @@ -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)]
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand All @@ -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}
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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}
Expand Down
34 changes: 17 additions & 17 deletions pyformlang/fst/tests/test_fst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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"]))
Expand All @@ -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"]))
Expand All @@ -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"]))
Expand Down
Loading

1 comment on commit d3d315a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
pyformlang
   __init__.py90100% 
pyformlang/cfg
   __init__.py30100% 
   cfg.py46622 99%
   cfg_variable_converter.py6544 94%
   cyk_table.py790100% 
   formal_grammar.py6911 99%
   llone_parser.py16333 98%
   parse_tree.py6511 98%
   recursive_decent_parser.py6122 97%
   set_queue.py150100% 
   utils.py250100% 
pyformlang/cfg/tests
   __init__.py00100% 
   test_cfg.py62622 99%
   test_llone_parser.py11711 99%
   test_production.py210100% 
   test_recursive_decent_parser.py2511 96%
   test_terminal.py330100% 
   test_variable.py180100% 
pyformlang/fcfg
   __init__.py40100% 
   fcfg.py13111 99%
   feature_production.py250100% 
   feature_structure.py19133 98%
   state.py360100% 
pyformlang/fcfg/tests
   __init__.py00100% 
   test_fcfg.py1690100% 
   test_feature_structure.py1590100% 
pyformlang/finite_automaton
   __init__.py80100% 
   deterministic_finite_automaton.py18333 98%
   deterministic_transition_function.py2411 96%
   doubly_linked_list.py350100% 
   doubly_linked_node.py100100% 
   epsilon_nfa.py21011 99%
   finite_automaton.py23111 99%
   hopcroft_processing_list.py240100% 
   nondeterministic_finite_automaton.py400100% 
   nondeterministic_transition_function.py480100% 
   partition.py400100% 
   transition_function.py320100% 
   utils.py300100% 
pyformlang/finite_automaton/tests
   __init__.py00100% 
   test_deterministic_finite_automaton.py2960100% 
   test_deterministic_transition_function.py8955 94%
   test_epsilon.py130100% 
   test_epsilon_nfa.py5960100% 
   test_nondeterministic_finite_automaton.py1600100% 
   test_nondeterministic_transition_function.py790100% 
   test_state.py280100% 
   test_symbol.py290100% 
pyformlang/fst
   __init__.py20100% 
   fst.py18611 99%
   transition_function.py3833 92%
   utils.py250100% 
pyformlang/fst/tests
   __init__.py00100% 
   test_fst.py1910100% 
pyformlang/indexed_grammar
   __init__.py90100% 
   consumption_rule.py3522 94%
   duplication_rule.py320100% 
   end_rule.py3311 97%
   indexed_grammar.py27111 99%
   production_rule.py3511 97%
   reduced_rule.py290100% 
   rule_ordering.py730100% 
   rules.py770100% 
   utils.py410100% 
pyformlang/indexed_grammar/tests
   __init__.py00100% 
   test_indexed_grammar.py2500100% 
   test_rules.py350100% 
pyformlang/objects
   __init__.py50100% 
   base_epsilon.py1511 93%
   base_terminal.py70100% 
   formal_object.py240100% 
pyformlang/objects/cfg_objects
   __init__.py60100% 
   cfg_object.py50100% 
   epsilon.py30100% 
   production.py4111 98%
   terminal.py100100% 
   utils.py140100% 
   variable.py130100% 
pyformlang/objects/finite_automaton_objects
   __init__.py50100% 
   epsilon.py30100% 
   finite_automaton_object.py50100% 
   state.py70100% 
   symbol.py50100% 
   utils.py140100% 
pyformlang/objects/pda_objects
   __init__.py60100% 
   epsilon.py30100% 
   pda_object.py50100% 
   stack_symbol.py70100% 
   state.py70100% 
   symbol.py50100% 
   utils.py2111 95%
pyformlang/objects/regex_objects
   __init__.py20100% 
   regex_objects.py650100% 
   utils.py220100% 
pyformlang/pda
   __init__.py40100% 
   pda.py31722 99%
   transition_function.py3933 92%
   utils.py5322 96%
pyformlang/pda/tests
   __init__.py00100% 
   test_pda.py3020100% 
pyformlang/regular_expression
   __init__.py30100% 
   python_regex.py26966 98%
   regex.py2811414 95%
   regex_reader.py15944 97%
pyformlang/regular_expression/tests
   __init__.py00100% 
   test_python_regex.py2780100% 
   test_regex.py4110100% 
pyformlang/rsa
   __init__.py30100% 
   box.py512525 51%
   recursive_automaton.py7299 88%
pyformlang/rsa/tests
   __init__.py00100% 
   test_rsa.py370100% 
pyformlang/tests
   __init__.py00100% 
TOTAL874610999% 

Tests Skipped Failures Errors Time
310 0 💤 0 ❌ 0 🔥 8.613s ⏱️

Please sign in to comment.