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

Fix type annotations in swig-wrappers (again) #2365

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions python/sdist/amici/swig.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
self._annotation_from_docstring(node)

# Has a return type annotation?
if node.returns:
if node.returns and isinstance(node.returns, ast.Constant):

Check warning on line 62 in python/sdist/amici/swig.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/swig.py#L62

Added line #L62 was not covered by tests
node.returns = self._new_annot(node.returns.value)

# Has arguments?
Expand Down Expand Up @@ -112,8 +112,11 @@
"""Add annotations based on docstring.

If any argument or return type of the function is not annotated, but
the corresponding docstring contains a type hint, the type hint is used
as the annotation.
the corresponding docstring contains a type hint (``:rtype:`` or
``:type:``), the type hint is used as the annotation.

Swig sometimes generates ``:type solver: :py:class:`Solver`` instead of
``:type solver: Solver``. Those need special treatment.
"""
docstring = ast.get_docstring(node, clean=False)
if not docstring or "*Overload 1:*" in docstring:
Expand All @@ -124,11 +127,19 @@
lines_to_remove = set()

for line_no, line in enumerate(docstring):
if match := re.match(r"\W*:rtype:\W*(.+)", line):
if (

Check warning on line 130 in python/sdist/amici/swig.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/swig.py#L130

Added line #L130 was not covered by tests
match := re.match(
r"\s*:rtype:\s*(?::py:class:`)?(\w+)`?\s+$", line
)
) and not match.group(1).startswith(":"):
node.returns = ast.Constant(match.group(1))
lines_to_remove.add(line_no)

if match := re.match(r"\W*:type:\W*(\w+):\W*(.+)", line):
if (

Check warning on line 138 in python/sdist/amici/swig.py

View check run for this annotation

Codecov / codecov/patch

python/sdist/amici/swig.py#L138

Added line #L138 was not covered by tests
match := re.match(
r"\s*:type\s*(\w+):\W*(?::py:class:`)?(\w+)`?\s+$", line
)
) and not match.group(1).startswith(":"):
for arg in node.args.args:
if arg.arg == match.group(1):
arg.annotation = ast.Constant(match.group(2))
Expand Down
Loading