Commit 93d0164
Fix validate_activation crash on builtin functions
validate_activation in neat/activations.py had an internal contradiction:
the isinstance check on line 104 explicitly admitted BuiltinFunctionType,
but line 109 then accessed function.__code__.co_argcount, which crashes
with AttributeError on C-implemented CPython builtins because they do
not have a __code__ attribute.
Calling config.genome_config.add_activation('my_abs', abs) — the exact
pattern documented in docs/customization.rst — raised:
AttributeError: 'builtin_function_or_method' object has no attribute
'__code__'
instead of the documented InvalidActivationFunction.
The sister function validate_aggregation in neat/aggregations.py already
handled this case correctly using inspect.signature with a
TypeError/ValueError fallback for builtins. Rewrite validate_activation
to mirror that pattern: try inspect.signature first, fall back to
accepting BuiltinFunctionType when signature inspection fails, and use
signature.bind(object()) to verify the callable accepts exactly one
positional argument.
This is also a strict upgrade over an __code__-based check: inspect
can actually introspect most CPython builtins (abs, len, sum, round,
divmod all return Signature objects) and can therefore correctly
reject wrong-arity builtins like divmod, which the old check could
never have caught.
Delete the misleading "# avoid deprecated use of inspect" comment. The
inspect module is not deprecated — that comment appears to be a fossil
from when this code used inspect.getargspec (which was removed in
Python 3.11). inspect.signature is the current, non-deprecated API
and is already used successfully by validate_aggregation in the same
codebase.
Add 8 regression tests in tests/test_coverage_gaps.py under a new
TestValidateActivation class. validate_activation previously had zero
test coverage, which is why this bug escaped notice. Tests cover:
plain functions, lambdas, builtins with introspectable signatures
(abs — the original regression), builtins without introspectable
signatures (max, min — fallback path), wrong-arity pure functions,
zero-arg functions, wrong-arity builtins (divmod), and non-callable
objects. Full suite: 629 passed (was 621), 6 skipped, 0 failures.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent b660ed8 commit 93d0164
2 files changed
+78
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
106 | 107 | | |
107 | 108 | | |
108 | 109 | | |
109 | | - | |
110 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
111 | 126 | | |
112 | 127 | | |
113 | 128 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
86 | 87 | | |
87 | 88 | | |
88 | 89 | | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
89 | 150 | | |
90 | 151 | | |
91 | 152 | | |
| |||
0 commit comments