Skip to content

Commit

Permalink
generate stub files from docstrings (#212)
Browse files Browse the repository at this point in the history
This commit refines type annotations in the clingo module and adds a
script to generate stub files from the docstrings. The stub files can
be used with systems like mypy. This commit also hides the Slice class
be calling it _Slice and renames Tuple to Tuple_ so that it does not
clash with the typing.Tuple.
  • Loading branch information
rkaminsk committed Apr 29, 2020
1 parent 7be84ee commit a5004a6
Show file tree
Hide file tree
Showing 11 changed files with 1,037 additions and 213 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ examples/clingo/pydoc/clingo.html
cmake/FindBISON.cmake
doc/py/clingo
doc/py/Gemfile.lock
.mypy_cache/
30 changes: 15 additions & 15 deletions app/clingo/tests/python/observer-replace.lp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#script (python)

import sys
from clingo import Function, Tuple
from clingo import Function, Tuple_

class Observer:
def __init__(self):
Expand Down Expand Up @@ -36,47 +36,47 @@ class Observer:
def _rule(self, choice, head, body):
head = sorted(set([ self.__map(atm) for atm in head ]))
body = sorted(set([ self.__map(lit) for lit in body ]))
self.__reified.append(Function("rule", [choice, Tuple(head), Tuple(body)]))
self.__reified.append(Function("rule", [choice, Tuple_(head), Tuple_(body)]))

def _weight_rule(self, choice, head, lower_bound, body):
head = sorted(set([ self.__map(atm) for atm in head ]))
body = sorted(set([ Tuple([self.__map(lit), weight]) for lit, weight in body ]))
self.__reified.append(Function("weight_rule", [choice, Tuple(head), lower_bound, Tuple(body)]))
body = sorted(set([ Tuple_([self.__map(lit), weight]) for lit, weight in body ]))
self.__reified.append(Function("weight_rule", [choice, Tuple_(head), lower_bound, Tuple_(body)]))

def _minimize(self, priority, literals):
literals = sorted(set([ Tuple([self.__map(lit), weight]) for lit, weight in literals ]))
self.__reified.append(Function("minimize", [priority, Tuple(literals)]))
literals = sorted(set([ Tuple_([self.__map(lit), weight]) for lit, weight in literals ]))
self.__reified.append(Function("minimize", [priority, Tuple_(literals)]))

def _project(self, atoms):
atoms = sorted(set([ self.__map(atm) for atm in atoms ]))
self.__reified.append(Function("project", [Tuple(atoms)]))
self.__reified.append(Function("project", [Tuple_(atoms)]))

def output_atom(self, symbol, atom):
self.__symbols[atom] = symbol
self.__reified.append(Function("output_atom", [symbol]))

def _output_term(self, symbol, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("output_term", [symbol, Tuple(condition)]))
self.__reified.append(Function("output_term", [symbol, Tuple_(condition)]))

def _output_csp(self, symbol, value, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("output_csp", [symbol, value, Tuple(condition)]))
self.__reified.append(Function("output_csp", [symbol, value, Tuple_(condition)]))

def _external(self, atom, value):
self.__reified.append(Function("external", [self.__map(atom), str(value)]))

def _assume(self, literals):
literals = sorted(set([ self.__map(lit) for lit in literals ]))
self.__reified.append(Function("assume", [Tuple(literals)]))
self.__reified.append(Function("assume", [Tuple_(literals)]))

def _heuristic(self, atom, type, bias, priority, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("heuristic", [self.__map(atom), str(type), bias, Tuple(condition)]))
self.__reified.append(Function("heuristic", [self.__map(atom), str(type), bias, Tuple_(condition)]))

def _acyc_edge(self, node_u, node_v, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("acyc_edge", [node_u, node_v, Tuple(condition)]))
self.__reified.append(Function("acyc_edge", [node_u, node_v, Tuple_(condition)]))

def theory_term_number(self, term_id, number):
self.__terms[term_id] = lambda: number
Expand All @@ -88,13 +88,13 @@ class Observer:
self.__terms[term_id] = lambda: Function(self.__terms[name_id_or_type]().name, [self.__terms[i]() for i in arguments])

def theory_element(self, element_id, terms, condition):
self.__elems[element_id] = lambda: Function("elem", [Tuple([self.__terms[i]() for i in terms]), Tuple(sorted(set([ self.__map(lit) for lit in condition ])))])
self.__elems[element_id] = lambda: Function("elem", [Tuple_([self.__terms[i]() for i in terms]), Tuple_(sorted(set([ self.__map(lit) for lit in condition ])))])

def _theory_atom(self, atom_id_or_zero, term_id, elements):
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple(sorted(set([ self.__elems[e]() for e in elements ])))]);
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple_(sorted(set([ self.__elems[e]() for e in elements ])))]);

def _theory_atom_with_guard(self, atom_id_or_zero, term_id, elements, operator_id, right_hand_side_id):
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple(sorted(set([ self.__elems[e]() for e in elements ]))), self.__terms[operator_id](), self.__terms[right_hand_side_id]()]);
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple_(sorted(set([ self.__elems[e]() for e in elements ]))), self.__terms[operator_id](), self.__terms[right_hand_side_id]()]);

def end_step(self):
self.__reified.append(Function("end_step", []))
Expand Down
30 changes: 15 additions & 15 deletions app/clingo/tests/python/observer.lp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#script (python)

from clingo import Function, Tuple
from clingo import Function, Tuple_

class Observer:
def __init__(self):
Expand Down Expand Up @@ -35,47 +35,47 @@ class Observer:
def _rule(self, choice, head, body):
head = sorted(set([ self.__map(atm) for atm in head ]))
body = sorted(set([ self.__map(lit) for lit in body ]))
self.__reified.append(Function("rule", [choice, Tuple(head), Tuple(body)]))
self.__reified.append(Function("rule", [choice, Tuple_(head), Tuple_(body)]))

def _weight_rule(self, choice, head, lower_bound, body):
head = sorted(set([ self.__map(atm) for atm in head ]))
body = sorted(set([ Tuple([self.__map(lit), weight]) for lit, weight in body ]))
self.__reified.append(Function("weight_rule", [choice, Tuple(head), lower_bound, Tuple(body)]))
body = sorted(set([ Tuple_([self.__map(lit), weight]) for lit, weight in body ]))
self.__reified.append(Function("weight_rule", [choice, Tuple_(head), lower_bound, Tuple_(body)]))

def _minimize(self, priority, literals):
literals = sorted(set([ Tuple([self.__map(lit), weight]) for lit, weight in literals ]))
self.__reified.append(Function("minimize", [priority, Tuple(literals)]))
literals = sorted(set([ Tuple_([self.__map(lit), weight]) for lit, weight in literals ]))
self.__reified.append(Function("minimize", [priority, Tuple_(literals)]))

def _project(self, atoms):
atoms = sorted(set([ self.__map(atm) for atm in atoms ]))
self.__reified.append(Function("project", [Tuple(atoms)]))
self.__reified.append(Function("project", [Tuple_(atoms)]))

def output_atom(self, symbol, atom):
self.__symbols[atom] = symbol
self.__reified.append(Function("output_atom", [symbol]))

def _output_term(self, symbol, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("output_term", [symbol, Tuple(condition)]))
self.__reified.append(Function("output_term", [symbol, Tuple_(condition)]))

def _output_csp(self, symbol, value, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("output_csp", [symbol, value, Tuple(condition)]))
self.__reified.append(Function("output_csp", [symbol, value, Tuple_(condition)]))

def _external(self, atom, value):
self.__reified.append(Function("external", [self.__map(atom), str(value)]))

def _assume(self, literals):
literals = sorted(set([ self.__map(lit) for lit in literals ]))
self.__reified.append(Function("assume", [Tuple(literals)]))
self.__reified.append(Function("assume", [Tuple_(literals)]))

def _heuristic(self, atom, type, bias, priority, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("heuristic", [self.__map(atom), str(type), bias, Tuple(condition)]))
self.__reified.append(Function("heuristic", [self.__map(atom), str(type), bias, Tuple_(condition)]))

def _acyc_edge(self, node_u, node_v, condition):
condition = sorted(set([ self.__map(lit) for lit in condition ]))
self.__reified.append(Function("acyc_edge", [node_u, node_v, Tuple(condition)]))
self.__reified.append(Function("acyc_edge", [node_u, node_v, Tuple_(condition)]))

def theory_term_number(self, term_id, number):
self.__terms[term_id] = lambda: number
Expand All @@ -87,13 +87,13 @@ class Observer:
self.__terms[term_id] = lambda: Function(self.__terms[name_id_or_type]().name, [self.__terms[i]() for i in arguments])

def theory_element(self, element_id, terms, condition):
self.__elems[element_id] = lambda: Function("elem", [Tuple([self.__terms[i]() for i in terms]), Tuple(sorted(set([ self.__map(lit) for lit in condition ])))])
self.__elems[element_id] = lambda: Function("elem", [Tuple_([self.__terms[i]() for i in terms]), Tuple_(sorted(set([ self.__map(lit) for lit in condition ])))])

def _theory_atom(self, atom_id_or_zero, term_id, elements):
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple(sorted(set([ self.__elems[e]() for e in elements ])))]);
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple_(sorted(set([ self.__elems[e]() for e in elements ])))]);

def _theory_atom_with_guard(self, atom_id_or_zero, term_id, elements, operator_id, right_hand_side_id):
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple(sorted(set([ self.__elems[e]() for e in elements ]))), self.__terms[operator_id](), self.__terms[right_hand_side_id]()]);
self.__symbols[atom_id_or_zero] = Function("theory", [self.__terms[term_id](), Tuple_(sorted(set([ self.__elems[e]() for e in elements ]))), self.__terms[operator_id](), self.__terms[right_hand_side_id]()]);

def end_step(self):
self.__reified.append(Function("end_step", []))
Expand Down
2 changes: 1 addition & 1 deletion app/clingo/tests/python/statistics.lp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def tosymbol(x):
if isinstance(x, dict):
ret = []
for key in sorted(x):
ret.append(clingo.Tuple([key, tosymbol(x[key])]))
ret.append(clingo.Tuple_([key, tosymbol(x[key])]))
return clingo.Function("dict", ret)
elif isinstance(x, list):
ret = []
Expand Down
5 changes: 5 additions & 0 deletions app/pyclingo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ if (PYCLINGO_INSTALL_DIR)
install(TARGETS pyclingo
RUNTIME DESTINATION ${PYCLINGO_INSTALL_DIR}
LIBRARY DESTINATION ${PYCLINGO_INSTALL_DIR})
install(DIRECTORY ${CMAKE_SOURCE_DIR}/libpyclingo/clingo/
DESTINATION ${PYCLINGO_INSTALL_DIR}/clingo
FILES_MATCHING
PATTERN "*.pyi"
PATTERN "*.typed")
endif()
Loading

0 comments on commit a5004a6

Please sign in to comment.