Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stubs for clingo python module. #212

Closed
wants to merge 26 commits into from
Closed

Add stubs for clingo python module. #212

wants to merge 26 commits into from

Conversation

rkaminsk
Copy link
Member

@rkaminsk rkaminsk commented Apr 29, 2020

This pull request continues the work in #188 to add stubs for clingo's python module. The stubs are generated from the information in the docstrings and can be updated with the scratch/mypy.py script.

With this a python program using the clingo module can be type checked. For example running mypy on the program

import clingo

ctl = clingo.Control()

ctl.ground([("part", [10])])

in file test.py results in

$ mypy test.py
test.py:5: error: List item 0 has incompatible type "int"; expected "Symbol"
Found 1 error in 1 file (checked 1 source file)

because clingo.Number(10) should be used instead.

  • Parse docstrings to generate stub files.
  • Install stub files along with python module.
  • A lot of annotations use List where just an Iterable is required. This has to be fixed to make annotations usable in practice.

There might be more issues regarding annotations because it is the first time that they are really used but this is probably best fixed in future commits when type checking real projects.

0x326 and others added 25 commits April 28, 2020 22:30
I had to rename clingo.Tuple to clingo.Tuple_ because the name clashed
with the name of the type annotation. Since the name Tuple is so
general, renaming is probably the better strategy.

The main thing that is still missing is to generate the stubs for the
clingo.ast module to get rid of the last error messages. Even replacing
those with typing.Any might work.

Otherwise, some minor error message with mypy are left, which can be
fixed by updating the type signatures.
mypy runs without errors on the generated stubs now. The only thing that
is left is to refine the type annotations in the docstrings a bit. The
main task is to get some nice base class for enumerations.
@rkaminsk rkaminsk added this to the v5.5.0 milestone Apr 29, 2020
@rkaminsk rkaminsk self-assigned this Apr 29, 2020
This replaces the too specific List with Sequence or Iterable depending
on the context.
rkaminsk added a commit that referenced this pull request Apr 29, 2020
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.
@rkaminsk rkaminsk closed this Apr 29, 2020
@rkaminsk rkaminsk deleted the feature/mypy branch April 29, 2020 18:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants