Skip to content

Commit 359d25c

Browse files
committed
fix(workflows/lint): fix docs dependency resolving
1 parent e6ecb3f commit 359d25c

File tree

10 files changed

+60
-55
lines changed

10 files changed

+60
-55
lines changed

docs/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
--requirement ../requirements.txt
22

3-
sphinx
3+
sphinx ~= 8.0
44
sphinx-autoapi
55
sphinx-autobuild
66
sphinx-autodoc-typehints

include/optree/pymacros.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ limitations under the License.
2929
# error "pybind11 2.12.0 or newer is required."
3030
#endif
3131

32+
// NOLINTNEXTLINE[bugprone-macro-parentheses]
33+
#define NONZERO_OR_EMPTY(MACRO) ((MACRO + 0 != 0) || (0 - MACRO - 1 >= 0))
34+
3235
namespace py = pybind11;
3336

3437
#if !defined(Py_ALWAYS_INLINE)
@@ -83,6 +86,9 @@ Py_Declare_ID(__qualname__); // type.__qualname__
8386
Py_Declare_ID(__name__); // type.__name__
8487
Py_Declare_ID(sort); // list.sort
8588
Py_Declare_ID(copy); // dict.copy
89+
Py_Declare_ID(OrderedDict); // OrderedDict
90+
Py_Declare_ID(defaultdict); // defaultdict
91+
Py_Declare_ID(deque); // deque
8692
Py_Declare_ID(default_factory); // defaultdict.default_factory
8793
Py_Declare_ID(maxlen); // deque.maxlen
8894
Py_Declare_ID(_fields); // namedtuple._fields
@@ -91,6 +97,3 @@ Py_Declare_ID(_asdict); // namedtuple._asdict
9197
Py_Declare_ID(n_fields); // structseq.n_fields
9298
Py_Declare_ID(n_sequence_fields); // structseq.n_sequence_fields
9399
Py_Declare_ID(n_unnamed_fields); // structseq.n_unnamed_fields
94-
95-
// NOLINTNEXTLINE[bugprone-macro-parentheses]
96-
#define NONZERO_OR_EMPTY(MACRO) ((MACRO + 0 != 0) || (0 - MACRO - 1 >= 0))

include/optree/registry.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,18 @@ class PyTreeTypeRegistry {
9898

9999
using RegistrationPtr = std::shared_ptr<const Registration>;
100100

101-
// Register a new custom type. Objects of `cls` will be treated as container node types in
101+
// Register a new custom type. Objects of type `cls` will be treated as container node types in
102102
// PyTrees.
103103
static void Register(const py::object &cls,
104104
const py::function &flatten_func,
105105
const py::function &unflatten_func,
106106
const py::object &path_entry_type,
107107
const std::string &registry_namespace = "");
108108

109+
// Unregister a previously registered custom type.
109110
static void Unregister(const py::object &cls, const std::string &registry_namespace = "");
110111

111-
// Find the custom type registration for `type`. Returns nullptr if none exists.
112+
// Find the custom type registration for `type`. Return nullptr if none exists.
112113
template <bool NoneIsLeaf>
113114
[[nodiscard]] static RegistrationPtr Lookup(const py::object &cls,
114115
const std::string &registry_namespace);
@@ -136,13 +137,18 @@ class PyTreeTypeRegistry {
136137
[[nodiscard]] static RegistrationPtr UnregisterImpl(const py::object &cls,
137138
const std::string &registry_namespace);
138139

139-
// Clear the registry on cleanup.
140+
// Clear the registry on cleanup for the current interpreter.
140141
static void Clear();
141142

142-
std::unordered_map<py::handle, RegistrationPtr> m_registrations{};
143-
std::unordered_map<std::pair<std::string, py::handle>, RegistrationPtr> m_named_registrations{};
143+
using RegistrationsMap = std::unordered_map<py::handle, RegistrationPtr>;
144+
using NamedRegistrationsMap =
145+
std::unordered_map<std::pair<std::string, py::handle>, RegistrationPtr>;
146+
using BuiltinsTypesSet = std::unordered_set<py::handle>;
144147

145-
static inline std::unordered_set<py::handle> sm_builtins_types{};
148+
RegistrationsMap m_registrations{};
149+
NamedRegistrationsMap m_named_registrations{};
150+
151+
static inline BuiltinsTypesSet sm_builtins_types{};
146152
static inline read_write_mutex sm_mutex{};
147153
};
148154

include/optree/synchronization.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ using scoped_lock = std::scoped_lock<mutex>;
6060
using scoped_recursive_lock = std::scoped_lock<recursive_mutex>;
6161

6262
#if (defined(__APPLE__) /* header <shared_mutex> is not available on macOS build target */ && \
63-
PY_VERSION_HEX < /* Python 3.12.0 */ 0x030C00F0)
63+
PY_VERSION_HEX < 0x030C00F0 /* Python 3.12.0 */)
6464

6565
# undef HAVE_READ_WRITE_LOCK
6666

include/optree/treespec.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ class PyTreeSpec {
288288

289289
private:
290290
using RegistrationPtr = PyTreeTypeRegistry::RegistrationPtr;
291-
using ThreadedIdentity = std::pair<const optree::PyTreeSpec *, std::thread::id>;
291+
using ThreadedIdentity = std::pair<const PyTreeSpec *, std::thread::id>;
292292

293293
struct Node {
294294
PyTreeKind kind = PyTreeKind::Leaf;
@@ -333,16 +333,16 @@ class PyTreeSpec {
333333
// The registry namespace used to resolve the custom pytree node types.
334334
std::string m_namespace{};
335335

336-
// Helper that returns the string representation of a node kind.
336+
// Return the string representation of a node kind.
337337
[[nodiscard]] static std::string NodeKindToString(const Node &node);
338338

339-
// Helper that manufactures an instance of a node given its children.
339+
// Manufacture an instance of a node given its children.
340340
[[nodiscard]] static py::object MakeNode(
341341
const Node &node,
342342
const py::object children[], // NOLINT[hicpp-avoid-c-arrays]
343343
const size_t &num_children);
344344

345-
// Helper that identifies the path entry class for a node.
345+
// Identify the path entry class for a node.
346346
[[nodiscard]] static py::object GetPathEntryType(const Node &node);
347347

348348
// Recursive helper used to implement Flatten().

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ test = [
8181
"typing-extensions == 4.12.0; python_version >= '3.13' and platform_system == 'Windows'",
8282
]
8383
docs = [
84-
"sphinx",
84+
"sphinx ~= 8.0",
8585
"sphinx-autoapi",
8686
"sphinx-autobuild",
8787
"sphinx-autodoc-typehints",

src/optree.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ void BuildModule(py::module_ &mod) { // NOLINT[runtime/references]
5252
std::string(__FILE_RELPATH_FROM_PROJECT_ROOT__) + ")";
5353
mod.attr("Py_TPFLAGS_BASETYPE") = py::int_(Py_TPFLAGS_BASETYPE);
5454

55-
// NOLINTNEXTLINE[bugprone-macro-parentheses]
56-
#define NONZERO_OR_EMPTY(MACRO) ((MACRO + 0 != 0) || (0 - MACRO - 1 >= 0))
57-
5855
// Meta information during build
5956
py::dict BUILDTIME_METADATA{};
6057
BUILDTIME_METADATA["PY_VERSION"] = py::str(PY_VERSION);
@@ -99,8 +96,6 @@ void BuildModule(py::module_ &mod) { // NOLINT[runtime/references]
9996
BUILDTIME_METADATA["GLIBCXX_USE_CXX11_ABI"] = py::bool_(false);
10097
#endif
10198

102-
#undef NONZERO_OR_EMPTY
103-
10499
mod.attr("BUILDTIME_METADATA") = std::move(BUILDTIME_METADATA);
105100
py::exec(
106101
R"py(
@@ -156,7 +151,7 @@ void BuildModule(py::module_ &mod) { // NOLINT[runtime/references]
156151
py::arg("namespace") = "")
157152
.def("flatten",
158153
&PyTreeSpec::Flatten,
159-
"Flattens a pytree.",
154+
"Flatten a pytree.",
160155
py::arg("tree"),
161156
py::pos_only(),
162157
py::arg("leaf_predicate") = std::nullopt,
@@ -528,10 +523,13 @@ void BuildModule(py::module_ &mod) { // NOLINT[runtime/references]
528523

529524
} // namespace optree
530525

526+
// NOLINTBEGIN[cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-vararg]
531527
#if PYBIND11_VERSION_HEX >= 0x020D00F0 // pybind11 2.13.0
532-
// NOLINTNEXTLINE[cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-vararg]
533-
PYBIND11_MODULE(_C, mod, py::mod_gil_not_used()) { optree::BuildModule(mod); }
528+
PYBIND11_MODULE(_C, mod, py::mod_gil_not_used())
534529
#else
535-
// NOLINTNEXTLINE[cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-vararg]
536-
PYBIND11_MODULE(_C, mod) { optree::BuildModule(mod); }
530+
PYBIND11_MODULE(_C, mod)
537531
#endif
532+
// NOLINTEND[cppcoreguidelines-pro-bounds-pointer-arithmetic,cppcoreguidelines-pro-type-vararg]
533+
{
534+
optree::BuildModule(mod);
535+
}

tests/helpers.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import functools
2121
import gc
2222
import itertools
23+
import os
2324
import platform
25+
import subprocess
2426
import sys
2527
import sysconfig
2628
import time
@@ -148,6 +150,27 @@ def wrapper(*args, **kwargs):
148150
return wrapper
149151

150152

153+
def check_script_in_subprocess(script, /, *, output, env=None, cwd=TEST_ROOT, rerun=1):
154+
if env is None:
155+
env = os.environ
156+
env = {
157+
key: value for key, value in env.items() if not key.startswith(('PYTHON', 'PYTEST', 'COV_'))
158+
}
159+
result = ''
160+
for _ in range(rerun):
161+
result = subprocess.check_output(
162+
[sys.executable, '-Walways', '-Werror', '-c', script],
163+
stderr=subprocess.STDOUT,
164+
text=True,
165+
encoding='utf-8',
166+
cwd=cwd,
167+
env=env,
168+
)
169+
if output is not None:
170+
assert result == output
171+
return result
172+
173+
151174
MISSING = object()
152175

153176

tests/test_ops.py

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
import functools
2020
import itertools
2121
import operator
22-
import os
2322
import pickle
2423
import platform
2524
import re
26-
import subprocess
2725
import sys
2826
from collections import OrderedDict, defaultdict, deque
2927

@@ -34,7 +32,6 @@
3432
GLOBAL_NAMESPACE,
3533
IS_LEAF_FUNCTIONS,
3634
LEAVES,
37-
TEST_ROOT,
3835
TREE_ACCESSORS,
3936
TREE_PATHS,
4037
TREES,
@@ -45,6 +42,7 @@
4542
Py_DEBUG,
4643
always,
4744
assert_equal_type_and_value,
45+
check_script_in_subprocess,
4846
is_list,
4947
is_none,
5048
is_tuple,
@@ -60,22 +58,7 @@
6058
@skipif_android
6159
@skipif_ios
6260
def test_import_no_warnings():
63-
env = {
64-
key: value
65-
for key, value in os.environ.items()
66-
if not key.startswith(('PYTHON', 'PYTEST', 'COV_'))
67-
}
68-
assert (
69-
subprocess.check_output(
70-
[sys.executable, '-Walways', '-Werror', '-c', 'import optree'],
71-
stderr=subprocess.STDOUT,
72-
text=True,
73-
encoding='utf-8',
74-
cwd=TEST_ROOT,
75-
env=env,
76-
)
77-
== ''
78-
)
61+
check_script_in_subprocess('import optree', output='')
7962

8063

8164
def test_max_depth():

tests/test_treespec.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
MyAnotherDict,
4444
MyDict,
4545
Py_DEBUG,
46+
check_script_in_subprocess,
4647
disable_systrace,
4748
gc_collect,
4849
parametrize,
@@ -70,11 +71,6 @@ def test_treespec_construct():
7071
with pytest.raises(TypeError, match=re.escape('No constructor defined!')):
7172
treespec.__init__()
7273

73-
env = {
74-
key: value
75-
for key, value in os.environ.items()
76-
if not key.startswith(('PYTHON', 'PYTEST', 'COV_'))
77-
}
7874
script = textwrap.dedent(
7975
r"""
8076
import signal
@@ -95,11 +91,7 @@ def test_treespec_construct():
9591
returncode = 0
9692
try:
9793
with tempfile.TemporaryDirectory() as tmpdir:
98-
subprocess.check_call(
99-
[sys.executable, '-Walways', '-Werror', '-c', script],
100-
cwd=tmpdir,
101-
env=env,
102-
)
94+
check_script_in_subprocess(script, cwd=tmpdir, output=None)
10395
except subprocess.CalledProcessError as ex:
10496
returncode = abs(ex.returncode)
10597
if 128 < returncode < 256:

0 commit comments

Comments
 (0)