Skip to content

Handle errors in tri-state Python API boolean function by using the "p" parsing format specifier - #901

Merged
ntamas merged 7 commits into
igraph:mainfrom
szhorvat:fix/bool-arguments
Nov 29, 2026
Merged

ntamas merged 7 commits into
igraph:mainfrom
szhorvat:fix/bool-arguments

Conversation

@szhorvat

@szhorvat szhorvat commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

Boolean function in the Python API typically don't just return 1/0 for true/false, but also -1 for an error. This means that usage such as if (PyObject_IsTrue()) ... isn't proper.

This PR fixes some, but not all of these cases (the rest will come in a new PR). It contains fixes for those cases where the "p" parsing format specifier can handle this automatically. This is a very large number of mostly identical changes. Slightly different cases are in a second commit.

At the same time, I did a review of whether the touched parameters' default value in the C code matches the default value from the docs / Python signature. Three corrections came out of this review:

  • complementer() claims loops=False in the docs and signature but uses loops=True in reality. This was reported in Mistake in complementer signature #883. This behaviour and mismatch has been present in python-igraph for forever, which makes changing it now a difficult decision. What do you think @ntamas ? I think the right thing to do is to fix the behaviour and go with what the docs say, i.e. loops=False. This is done in this commit. This is the behaviour one would normally expect and what all other interfaces use by default.
  • topological_sorting() has an undocumented warning parameter that did nothing. This is leftover from when C/igraph 0.8 had such a warning parameter, which got dropped in C/igraph 0.9. It is now removed without deprecation.
  • GraphBase.biconnected_components stated the wrong default in the docs for return_articulation_points, this is now changed to match the actual behaviour. Should not be controversial.

Boolean arguments were parsed as objects with the "O" format code and
converted later with PyObject_IsTrue(). Its return value -1, which
signals that the conversion raised an exception, was used as if it were
true, and the exception was left pending. This happens for example when
a NumPy array with more than one element is passed as a boolean
argument. The call then typically fails with an unrelated error, e.g. a
SystemError or a spurious out-of-memory error from simplify(), or
succeeds while leaving the exception set.

Parse these arguments with the "p" format code instead, which applies
the same truth test during argument parsing and raises the exception
there. The change is mechanical: for each affected argument, the
PyObject* variable becomes an int with the same default, "O" becomes "p"
at its position in the format string, and PyObject_IsTrue(x) becomes x.
Only arguments that are used for nothing else were converted.

Assisted-by: Claude Opus 5.5
… "p"

The optional edgemaps argument was parsed with "O" into a variable that
had no default value, so it was read uninitialized when the argument was
omitted, and errors from PyObject_IsTrue() were ignored. Parse it with
"p" directly into the existing with_edgemaps flag, which defaults to 0.

Assisted-by: Claude Opus 5.5
Assisted-by: Claude Opus 5.5
Graph.complementer() is documented with loops=False, which is also the
default in the C core's functions.yaml and in R/igraph, but the C glue
code defaulted to true. Use false, as documented.

This changes the result of complementer() called without arguments:
loop edges are now only added when loops=True is passed.

Fixes igraph#883.

Assisted-by: Claude Opus 5.5
@szhorvat
szhorvat force-pushed the fix/bool-arguments branch 2 times, most recently from d022f44 to 21f74e7 Compare November 28, 2026 18:42
The GraphBase docstring signature stated return_articulation_points=True,
but the default in the C glue code is false. Graph.biconnected_components(),
the public wrapper, already documents and uses false.

Since the definition of biconnectedness varies between textbooks, also
document igraph's conventions in both docstrings, following the C
documentation: edge directions are ignored, two vertices connected by an
edge are considered biconnected, single-vertex components are not, so
isolated vertices belong to no component and a single component does not
imply that the graph is biconnected. Define articulation points and refer
to is_biconnected() and articulation_points().

Assisted-by: Claude Opus 5.5
The undocumented warnings argument of Graph.topological_sorting()
temporarily disabled igraph warnings around the C call. Since igraph 0.9,
igraph_topological_sorting() raises an error instead of a warning for
graphs with cycles, so the argument has had no effect. A search of public
code on GitHub found no callers passing it.

Remove the argument and correct the docstring, which still claimed that
a partial sorting is returned with a warning for graphs that are not
acyclic. Following the C documentation, state that self-loops are allowed
and that undirected graphs and graphs with other cycles raise an error,
and refer to is_dag() and feedback_arc_set().

Assisted-by: Claude Opus 5.5
@szhorvat
szhorvat requested review from ntamas and a balanced review from Copilot November 28, 2026 19:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@szhorvat
szhorvat marked this pull request as ready for review November 28, 2026 19:10
@szhorvat
szhorvat requested a lite review from Copilot November 28, 2026 19:14

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A critical active finding shows that weighted calls to Graph.assortativity_nominal() can reject or ignore the caller’s weights because the format string does not parse the weights argument.

1 open issue

Review effort: Lite

Comment thread src/_igraph/graphobject.c Resolved
The keyword list of Graph.assortativity_nominal() has four entries, but
the format string had only three conversions, so the weights argument
was never parsed and every call passing it raised a SystemError. Add the
missing conversion.

Note that the C core does not implement weighted nominal assortativity
yet, so non-null weights now raise a NotImplementedError from the C core
instead.

Assisted-by: Claude Opus 5.5

@ntamas ntamas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM apart from that single comment I posted.

Comment thread src/_igraph/graphobject.c
static char *kwlist[] = { "loops", NULL };
igraphmodule_GraphObject *result_o;
PyObject *o = Py_True;
int o = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This changes the default from true to false, FYI. Is this what you wanted?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ah I just read the PR description now. This is okay then.

@ntamas

ntamas commented Nov 29, 2026

Copy link
Copy Markdown
Member

Copilot has a point in its review but it's outside the scope of this PR. Will you take care of this or shall I fix it later?

@ntamas
ntamas merged commit c1d964d into igraph:main Nov 29, 2026
11 checks passed
@szhorvat

Copy link
Copy Markdown
Member Author

Copilot has a point in its review but it's outside the scope of this PR. Will you take care of this or shall I fix it later?

It was fixed in the same PR in this commit: 1bd1293

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants