Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 15 additions & 10 deletions src/libsemigroups_pybind11/detail/cxx_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@
from typing import Any, Callable
from typing_extensions import Self

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.

I'm not sure about these changes, this is a bit misleading, and looks weird. I think maybe what we want is type(Undefined).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Ah I see. I was a bit confused as to why UNDEFINED was being used here. So we want this to point to pybind11_builtins.pybind11_type?

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.

Yes exactly although I'm not sure how to get hold of that "correctly"

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've updated this following from our discussion yesterday, with a comment about why we have done what we've done. An additional step would be to add runtime checks like isinstance(cxx_type, type(Undefined)) if we really care about type correctness in these functions.


from _libsemigroups_pybind11 import ( # pylint: disable=no-name-in-module
UNDEFINED as _UNDEFINED,
)

pybind11_type = type(_UNDEFINED)
# We would really like Pybind11Type to point to pybind11_builtins.pybind11_type.
# However, this is not easily importable into python; see the discussions in
# https://github.com/pybind/pybind11/issues/3945 and
# https://github.com/pybind/cmake_example/pull/164.
# A proposed workaround would be to do the following:
# Pybind11Type = TypeVar('Pybind11Type', bound=type(Undefined))
# but that isn't allowed, since function calls aren't permitted in TypeVar
# expressions. Hence, the next best thing is to use the parent class of
# pybind11_builtins.pybind11_type, which is simply type. Further runtime checks
# can be added latter if required.
Pybind11Type = type


def to_cxx(x: Any) -> Any:
Expand All @@ -49,7 +55,7 @@ def to_py(x: Any, *args) -> Any:
_CXX_WRAPPED_TYPE_TO_PY_TYPE = {}


def register_cxx_wrapped_type(cxx_type: pybind11_type, py_type: type) -> None:
def register_cxx_wrapped_type(cxx_type: Pybind11Type, py_type: type) -> None:
"""
Function for adding to the _CXX_WRAPPED_TYPE_TO_PY_TYPE dictionary.
"""
Expand All @@ -59,7 +65,6 @@ def register_cxx_wrapped_type(cxx_type: pybind11_type, py_type: type) -> None:

class CxxWrapper:
# pylint: disable=missing-class-docstring
# pylint: disable=protected-access, no-member
def __init__(
self: Self,
*args,
Expand Down Expand Up @@ -182,7 +187,7 @@ def init_cxx_obj(self: Self, *args) -> None:


# TODO proper annotations
def wrap_cxx_mem_fn(cxx_mem_fn: pybind11_type) -> Callable:
def wrap_cxx_mem_fn(cxx_mem_fn: Pybind11Type) -> Callable:
"""
This function creates a wrapper around the pybind11 c++ member function
<cxx_mem_fn> that automatically wraps and unwraps CxxWrapper types, and
Expand Down Expand Up @@ -217,7 +222,7 @@ def cxx_mem_fn_wrapper(self, *args):


# TODO proper annotations
def wrap_cxx_free_fn(cxx_free_fn: pybind11_type) -> Callable:
def wrap_cxx_free_fn(cxx_free_fn: Pybind11Type) -> Callable:
"""
This function creates a wrapper around the pybind11 c++ free function
<cxx_free_fn> that automatically wraps and unwraps CxxWrapper types. The
Expand All @@ -232,7 +237,7 @@ def cxx_free_fn_wrapper(*args):
return cxx_free_fn_wrapper


def copy_cxx_mem_fns(cxx_class: pybind11_type, py_class: CxxWrapper) -> None:
def copy_cxx_mem_fns(cxx_class: Pybind11Type, py_class: CxxWrapper) -> None:
"""
Copy all the non-special methods of *cxx_class* into methods of *py_class*
that call the cxx member function on the _cxx_obj.
Expand Down
10 changes: 6 additions & 4 deletions src/libsemigroups_pybind11/froidure_pin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@
# The module doc string is what appears at the top of the helper function
# doc page, and so is omitted.

# pylint: disable=missing-module-docstring
"""
This page contains the documentation for various helper functions for
manipulating :any:`FroidurePin` objects. All such functions
are contained in the submodule ``libsemigroups_pybind11.froidure_pin``.
"""

from typing import TypeVar as _TypeVar, Iterator as _Iterator
from typing_extensions import Self as _Self

from _libsemigroups_pybind11 import ( # pylint: disable=no-name-in-module,unused-import
from _libsemigroups_pybind11 import ( # pylint: disable=no-name-in-module
BMat as _BMat,
BMat8 as _BMat8,
Bipartition as _Bipartition,
FroidurePinBMat as _FroidurePinBMat,
FroidurePinBMat8 as _FroidurePinBMat8,
FroidurePinBase as _FroidurePinBase,
FroidurePinBipartition as _FroidurePinBipartition,
FroidurePinIntMat as _FroidurePinIntMat,
FroidurePinMaxPlusMat as _FroidurePinMaxPlusMat,
Expand Down Expand Up @@ -68,7 +71,6 @@
Transf1 as _Transf1,
Transf2 as _Transf2,
Transf4 as _Transf4,
Undefined as _Undefined,
froidure_pin_current_minimal_factorisation as _froidure_pin_current_minimal_factorisation,
froidure_pin_current_normal_forms as _froidure_pin_current_normal_forms,
froidure_pin_current_position as _froidure_pin_current_position,
Expand Down
4 changes: 1 addition & 3 deletions src/libsemigroups_pybind11/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@

from typing_extensions import Self as _Self, Union as _Union

from _libsemigroups_pybind11 import ( # pylint: disable=no-name-in-module,unused-import
from _libsemigroups_pybind11 import ( # pylint: disable=no-name-in-module
BMat as _BMat,
IntMat as _IntMat,
MaxPlusMat as _MaxPlusMat,
MaxPlusTruncMat as _MaxPlusTruncMat,
MinPlusMat as _MinPlusMat,
MinPlusTruncMat as _MinPlusTruncMat,
NEGATIVE_INFINITY as _NEGATIVE_INFINITY,
NTPMat as _NTPMat,
NegativeInfinity as _NegativeInfinity,
POSITIVE_INFINITY as _POSITIVE_INFINITY,
PositiveInfinity as _PositiveInfinity,
ProjMaxPlusMat as _ProjMaxPlusMat,
matrix_row_space_size as _row_space_size,
Expand Down
9 changes: 3 additions & 6 deletions tests/cong_common.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# -*- coding: utf-8 -*-
# pylint: disable=no-name-in-module, missing-function-docstring
# pylint: disable=missing-class-docstring, invalid-name

# Copyright (c) 2021-2024 J. D. Mitchell
#
# Distributed under the terms of the GPL license version 3.
Expand All @@ -13,16 +10,16 @@
CongruenceCommon.
"""

# pylint: disable=no-name-in-module, missing-function-docstring, invalid-name

from libsemigroups_pybind11 import (
Presentation,
congruence_kind,
presentation,
)


def check_congruence_common_return_policy(TestType):
def check_congruence_common_return_policy(
TestType,
): # pylint: disable=missing-function-docstring, invalid-name
p = Presentation("ab")
presentation.add_rule(p, "abab", "a" * 6)
presentation.add_rule(p, "ba", "ababbb")
Expand Down
19 changes: 9 additions & 10 deletions tests/runner.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# -*- coding: utf-8 -*-
# pylint: disable=no-name-in-module, missing-function-docstring
# pylint: disable=missing-class-docstring, invalid-name

# Copyright (c) 2021-2024 J. D. Mitchell
#
# Distributed under the terms of the GPL license version 3.
Expand All @@ -17,12 +14,14 @@

from libsemigroups_pybind11 import ReportGuard

n = 0
N = 0


def check_runner(x, t=timedelta(microseconds=1000)):
global n # pylint: disable=global-statement
n = 0
def check_runner(
x, t=timedelta(microseconds=1000)
): # pylint: disable=missing-function-docstring
global N # pylint: disable=global-statement
N = 0
ReportGuard(False)

assert not x.stopped()
Expand All @@ -47,9 +46,9 @@ def check_runner(x, t=timedelta(microseconds=1000)):
pass

def func():
global n # pylint: disable=global-statement
n += 1
return n >= 2
global N # pylint: disable=global-statement
N += 1
return N >= 2

x.run_until(func)

Expand Down
15 changes: 7 additions & 8 deletions tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
arising from action.*pp in libsemigroups.
"""

# pylint: disable=no-name-in-module, missing-function-docstring, invalid-name
# pylint: disable=redefined-outer-name, no-member
# pylint: disable=missing-function-docstring

import pytest

Expand All @@ -30,8 +29,8 @@
from libsemigroups_pybind11.bmat8 import row_space_basis, col_space_basis


@pytest.fixture
def right_actions():
@pytest.fixture(name="right_actions")
def fixture_right_actions():
seed = row_space_basis(
BMat8([[1, 1, 1, 0], [1, 1, 0, 0], [0, 1, 0, 1], [0, 1, 0, 0]])
)
Expand Down Expand Up @@ -142,8 +141,8 @@ def right_actions():
return result


@pytest.fixture
def left_actions():
@pytest.fixture(name="left_actions")
def fixture_left_actions():
seed = col_space_basis(
BMat8([[1, 1, 1, 0], [1, 1, 0, 0], [0, 1, 0, 1], [0, 1, 0, 0]])
)
Expand Down Expand Up @@ -205,8 +204,8 @@ def left_actions():
return result


@pytest.fixture
def extreme_left_actions():
@pytest.fixture(name="extreme_left_actions")
def fixture_extreme_left_actions():
result = []
result.append(
LeftAction(
Expand Down
30 changes: 15 additions & 15 deletions tests/test_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
arising from adapters.*pp in libsemigroups.
"""

# pylint: disable=no-name-in-module, missing-function-docstring, invalid-name
# pylint: disable=missing-function-docstring

import pytest

Expand All @@ -25,25 +25,25 @@


def test_adapters_017():
A, B, C = bmat8.random(), BMat8(0), bmat8.one()
right = ImageRightAction(point=A, element=A)
a, b, c = bmat8.random(), BMat8(0), bmat8.one()
right = ImageRightAction(point=a, element=a)
# Point1, Point2, Element -> Point1 = Point2 ^ Element
B = right(A, C)
assert B == bmat8.row_space_basis(A)
b = right(a, c)
assert b == bmat8.row_space_basis(a)

B = right(C, A)
assert B == bmat8.row_space_basis(A)
b = right(c, a)
assert b == bmat8.row_space_basis(a)

left = ImageLeftAction(point=A, element=A)
B = left(A, C)
assert B == bmat8.col_space_basis(A)
left = ImageLeftAction(point=a, element=a)
b = left(a, c)
assert b == bmat8.col_space_basis(a)

B = left(C, A)
assert B == bmat8.col_space_basis(A)
b = left(c, a)
assert b == bmat8.col_space_basis(a)

B = right(A, A)
C = left(bmat8.transpose(A), bmat8.transpose(A))
assert B == bmat8.transpose(C)
b = right(a, a)
c = left(bmat8.transpose(a), bmat8.transpose(a))
assert b == bmat8.transpose(c)


def test_image_right_action_pperm_pperm():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_bipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
This module contains some tests for bipartitions.
"""

# pylint: disable=no-name-in-module, missing-function-docstring, invalid-name
# pylint: disable=missing-function-docstring, invalid-name

from itertools import chain

Expand Down
2 changes: 1 addition & 1 deletion tests/test_bmat8.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
arising from bmat8.*pp in libsemigroups.
"""

# pylint: disable=no-name-in-module, missing-function-docstring, invalid-name
# pylint: disable=missing-function-docstring

import pytest
from libsemigroups_pybind11 import BMat8, LibsemigroupsError
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cong.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
This file contains tests for Congruence from libsemigroups_pybind11.
"""

# pylint: disable=no-name-in-module, missing-function-docstring, invalid-name
# pylint: disable=missing-function-docstring

from datetime import timedelta

Expand Down
7 changes: 3 additions & 4 deletions tests/test_forest.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
# The full license is in the file LICENSE, distributed with this software.

# pylint: disable=missing-function-docstring, redefined-outer-name
# pylint: disable=missing-function-docstring

"""
This module contains some tests for the libsemigroups_pybind11 functionality
Expand All @@ -18,13 +18,12 @@
from libsemigroups_pybind11 import Forest, UNDEFINED


@pytest.fixture
@pytest.fixture(name="f")
def forest_fixture():
return Forest([UNDEFINED, 0, 1, 2, 3], [UNDEFINED, 0, 0, 0, 0])


def test_forest_return_policy(forest_fixture):
f = forest_fixture
def test_forest_return_policy(f):
assert f.add_nodes(2) is f
assert f.init(2) is f
assert f.labels() is not f.labels()
Expand Down
12 changes: 6 additions & 6 deletions tests/test_froidure_pin.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
# pylint: disable=no-name-in-module, missing-function-docstring
# pylint: disable=missing-class-docstring, invalid-name, redefined-outer-name

# Copyright (c) 2021-2024 J. D. Mitchell
#
Expand All @@ -12,6 +10,8 @@
This module contains some tests for FroidurePin
"""

# pylint: disable=missing-function-docstring, invalid-name

from datetime import timedelta
import pytest

Expand Down Expand Up @@ -285,13 +285,13 @@ def check_froidure_pin_transf2(T):
]


@pytest.fixture
def checks_for_generators():
@pytest.fixture(name="checks_for_generators")
def fixture_checks_for_generators():
return (check_constructors,)


@pytest.fixture
def checks_for_froidure_pin():
@pytest.fixture(name="checks_for_froidure_pin")
def fixture_checks_for_froidure_pin():
return (
check_settings,
check_mem_compare,
Expand Down
7 changes: 3 additions & 4 deletions tests/test_gabow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
libsemigroups_pybind11.
"""

# pylint: disable=missing-function-docstring, redefined-outer-name
# pylint: disable=missing-function-docstring

import pytest

Expand All @@ -22,7 +22,7 @@
)


@pytest.fixture
@pytest.fixture(name="wg")
def word_graph_fixture():
w = WordGraph(17, 31)
for i in range(17):
Expand Down Expand Up @@ -115,8 +115,7 @@ def test_004():
assert list(g.roots()) == [32, 65, 98, 131, 164, 197, 230, 263, 296, 329]


def test_gabow_return_policy(word_graph_fixture):
wg = word_graph_fixture
def test_gabow_return_policy(wg):
g = Gabow(wg)
assert g.component(0) is not g.component(0)
assert g.component_of(0) is not g.component_of(0)
Expand Down
Loading