Skip to content

Commit 46367e1

Browse files
__init__ fns have return type annotation None
1 parent 6bb7bec commit 46367e1

4 files changed

Lines changed: 24 additions & 63 deletions

File tree

src/libsemigroups_pybind11/detail/cxx_wrapper.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,14 @@ def __init__(
6565
required_kwargs=(),
6666
optional_kwargs=(),
6767
**kwargs,
68-
):
69-
if (
70-
len(args) == 1
71-
and len(kwargs) == 0
72-
and type(args[0]) in self._all_wrapped_cxx_types
73-
):
68+
) -> None:
69+
if len(args) == 1 and len(kwargs) == 0 and type(args[0]) in self._all_wrapped_cxx_types:
7470
# Copy constructor like construction directly from cxx object
7571
self._cxx_obj = args[0]
7672
self.py_template_params = self.py_template_params_from_cxx_obj()
7773
return
7874

79-
if (
80-
not len(required_kwargs)
81-
<= len(kwargs)
82-
<= len(required_kwargs) + len(optional_kwargs)
83-
):
75+
if not len(required_kwargs) <= len(kwargs) <= len(required_kwargs) + len(optional_kwargs):
8476
raise TypeError(
8577
f"expected between {len(required_kwargs)} and "
8678
f"{len(required_kwargs) + len(optional_kwargs)} "
@@ -119,9 +111,7 @@ def __getattr__(self: Self, name: str):
119111
def cxx_fn_wrapper(*args) -> Any:
120112
if len(args) == 1 and isinstance(args[0], list):
121113
args = args[0]
122-
return getattr(self._cxx_obj, name)(
123-
[to_cxx(x) for x in args]
124-
)
114+
return getattr(self._cxx_obj, name)([to_cxx(x) for x in args])
125115
return getattr(self._cxx_obj, name)(*(to_cxx(x) for x in args))
126116

127117
return cxx_fn_wrapper
@@ -136,18 +126,14 @@ def __copy__(self: Self) -> Self:
136126
if self._cxx_obj is not None:
137127
if hasattr(self._cxx_obj, "__copy__"):
138128
return to_py(self._cxx_obj.__copy__())
139-
raise NotImplementedError(
140-
f"{type(self._cxx_obj)} has no member named __copy__"
141-
)
129+
raise NotImplementedError(f"{type(self._cxx_obj)} has no member named __copy__")
142130
raise NameError("_cxx_obj has not been defined")
143131

144132
def __eq__(self: Self, that) -> bool:
145133
if self._cxx_obj is not None:
146134
if hasattr(self._cxx_obj, "__eq__"):
147135
return self._cxx_obj.__eq__(that._cxx_obj)
148-
raise NotImplementedError(
149-
f"{type(self._cxx_obj)} has no member named __eq__"
150-
)
136+
raise NotImplementedError(f"{type(self._cxx_obj)} has no member named __eq__")
151137
raise NameError("_cxx_obj has not been defined")
152138

153139
def py_template_params_from_cxx_obj(self: Self) -> tuple:
@@ -166,9 +152,9 @@ def init_cxx_obj(self: Self, *args) -> None:
166152
defined.
167153
"""
168154
assert self.py_template_params is not None
169-
self._cxx_obj = self._py_template_params_to_cxx_type[
170-
self.py_template_params
171-
](*(to_cxx(x) for x in args))
155+
self._cxx_obj = self._py_template_params_to_cxx_type[self.py_template_params](
156+
*(to_cxx(x) for x in args)
157+
)
172158

173159

174160
# TODO proper annotations
@@ -184,17 +170,13 @@ def cxx_mem_fn_wrapper(self, *args):
184170
# TODO move the first if-clause into to_cxx?
185171
if len(args) == 1 and isinstance(args[0], list):
186172
args = [[to_cxx(x) for x in args[0]]]
187-
result = getattr(to_cxx(self), cxx_mem_fn.__name__)(
188-
*(to_cxx(x) for x in args)
189-
)
173+
result = getattr(to_cxx(self), cxx_mem_fn.__name__)(*(to_cxx(x) for x in args))
190174
if result is to_cxx(self):
191175
return self
192176
if type(result) in _CXX_WRAPPED_TYPE_TO_PY_TYPE:
193177
cached_val = f"_cached_return_value_{cxx_mem_fn.__name__}"
194178
# TODO use args too in cached_val?
195-
if hasattr(self, cached_val) and result is to_cxx(
196-
getattr(self, cached_val)
197-
):
179+
if hasattr(self, cached_val) and result is to_cxx(getattr(self, cached_val)):
198180
return getattr(self, cached_val)
199181
result = _CXX_WRAPPED_TYPE_TO_PY_TYPE[type(result)](result)
200182
setattr(self, cached_val, result)
@@ -228,9 +210,7 @@ def copy_cxx_mem_fns(cxx_class: pybind11_type, py_class: CxxWrapper) -> None:
228210
that call the cxx member function on the _cxx_obj.
229211
"""
230212
for py_meth_name in dir(cxx_class):
231-
if (not py_meth_name.startswith("_")) and py_meth_name not in dir(
232-
py_class
233-
):
213+
if (not py_meth_name.startswith("_")) and py_meth_name not in dir(py_class):
234214
setattr(
235215
py_class,
236216
py_meth_name,

src/libsemigroups_pybind11/detail/decorators.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def copydoc(original):
1818
for example:
1919
2020
@copydoc(Transf1.__init__)
21-
def __init___(self):
21+
def __init___(self) -> None:
2222
pass
2323
"""
2424

src/libsemigroups_pybind11/presentation/__init__.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class Presentation(_CxxWrapper): # pylint: disable=missing-class-docstring
8989
def __eq__(self: Self, other: Self):
9090
return _to_cxx(self) == _to_cxx(other)
9191

92-
def __init__(self: Self, *args, **kwargs):
92+
def __init__(self: Self, *args, **kwargs) -> None:
9393
"""
9494
Construct a Presentation instance of the type specified by its argument.
9595
"""
@@ -114,10 +114,7 @@ def __init__(self: Self, *args, **kwargs):
114114
if len(args) == 1:
115115
if not (
116116
isinstance(args[0], (str, list))
117-
or (
118-
isinstance(self, InversePresentation)
119-
and isinstance(self, Presentation)
120-
)
117+
or (isinstance(self, InversePresentation) and isinstance(self, Presentation))
121118
):
122119
extra = ""
123120
if isinstance(self, InversePresentation):
@@ -126,12 +123,8 @@ def __init__(self: Self, *args, **kwargs):
126123
f"expected the argument to have type one of (str, list{extra}) "
127124
f"but found {type(args[0])}"
128125
)
129-
if isinstance(args[0], list) and not all(
130-
isinstance(x, int) for x in args[0]
131-
):
132-
raise ValueError(
133-
"expected the argument to consist of int values"
134-
)
126+
if isinstance(args[0], list) and not all(isinstance(x, int) for x in args[0]):
127+
raise ValueError("expected the argument to consist of int values")
135128
if isinstance(args[0], str):
136129
self.py_template_params = (str,)
137130
if isinstance(args[0], list):
@@ -171,9 +164,7 @@ class InversePresentation(Presentation):
171164
_py_template_params_to_cxx_type = {
172165
(List[int],): _InversePresentationWords,
173166
(str,): _InversePresentationStrings,
174-
(Presentation,): Union[
175-
_InversePresentationWords, _InversePresentationStrings
176-
],
167+
(Presentation,): Union[_InversePresentationWords, _InversePresentationStrings],
177168
}
178169

179170
_cxx_type_to_py_template_params = dict(
@@ -223,9 +214,7 @@ def __init__(self: Self, *args, **kwargs) -> None:
223214
length = _wrap_cxx_free_fn(_length)
224215
longest_rule = _wrap_cxx_free_fn(_longest_rule)
225216
longest_rule_length = _wrap_cxx_free_fn(_longest_rule_length)
226-
longest_subword_reducing_length = _wrap_cxx_free_fn(
227-
_longest_subword_reducing_length
228-
)
217+
longest_subword_reducing_length = _wrap_cxx_free_fn(_longest_subword_reducing_length)
229218
make_semigroup = _wrap_cxx_free_fn(_make_semigroup)
230219
normalize_alphabet = _wrap_cxx_free_fn(_normalize_alphabet)
231220
reduce_complements = _wrap_cxx_free_fn(_reduce_complements)
@@ -235,9 +224,7 @@ def __init__(self: Self, *args, **kwargs) -> None:
235224
remove_trivial_rules = _wrap_cxx_free_fn(_remove_trivial_rules)
236225
replace_subword = _wrap_cxx_free_fn(_replace_subword)
237226
replace_word = _wrap_cxx_free_fn(_replace_word)
238-
replace_word_with_new_generator = _wrap_cxx_free_fn(
239-
_replace_word_with_new_generator
240-
)
227+
replace_word_with_new_generator = _wrap_cxx_free_fn(_replace_word_with_new_generator)
241228
reverse = _wrap_cxx_free_fn(_reverse)
242229
shortest_rule = _wrap_cxx_free_fn(_shortest_rule)
243230
shortest_rule_length = _wrap_cxx_free_fn(_shortest_rule_length)

src/libsemigroups_pybind11/transf.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ def __repr__(self: Self) -> str:
191191
result = str(self)
192192
if len(result) < 72:
193193
return result
194-
return (
195-
f"<transformation of degree {self.degree()} and rank {self.rank()}>"
196-
)
194+
return f"<transformation of degree {self.degree()} and rank {self.rank()}>"
197195

198196
# We retain a separate __repr__ so that we can distinguish the cxx objects
199197
# and their python counterparts.
@@ -253,7 +251,7 @@ def _cxx_type_from_degree(n: int):
253251
return _PPerm4
254252

255253
@_copydoc(_PPerm1.__init__)
256-
def __init__(self: Self, *args):
254+
def __init__(self: Self, *args) -> None:
257255
if len(args) < 3:
258256
super().__init__(*args)
259257
return
@@ -269,9 +267,7 @@ def __repr__(self: Self) -> str:
269267
result = str(self)
270268
if len(result) < 72:
271269
return result
272-
return (
273-
f"<partial perm of degree {self.degree()} and rank {self.rank()}>"
274-
)
270+
return f"<partial perm of degree {self.degree()} and rank {self.rank()}>"
275271

276272
# We retain a separate __str__ so that we can distinguish the cxx objects
277273
# and their python counterparts.
@@ -353,9 +349,7 @@ def increase_degree_by(self: Self, n: int) -> Self:
353349
@staticmethod
354350
@_copydoc(_Perm1.one)
355351
def one(n: int) -> Self:
356-
result_type = Perm._py_template_params_to_cxx_type[
357-
Perm._py_template_params_from_degree(n)
358-
]
352+
result_type = Perm._py_template_params_to_cxx_type[Perm._py_template_params_from_degree(n)]
359353
return _to_py(result_type.one(n))
360354

361355

0 commit comments

Comments
 (0)