Skip to content

Commit 7972df3

Browse files
committed
refactor: only set annotations if datclass init
Signed-off-by: Nathaniel Starkman <nstarman@users.noreply.github.com>
1 parent b5444d4 commit 7972df3

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

equinox/_module.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -300,27 +300,28 @@ def __new__(
300300
# checkers.
301301
# Note that mutating the `__init__.__annotations__` is okay, as it was created
302302
# by the dataclass decorator on the previous line, so nothing else owns it.
303-
for f in dataclasses.fields(cls):
304-
if f.name not in cls.__init__.__annotations__:
305-
continue # Odd behaviour, so skip.
303+
if has_dataclass_init:
304+
for f in dataclasses.fields(cls):
305+
if not f.init:
306+
continue
306307

307-
if (converter := f.metadata.get("converter")) is None:
308-
continue # No converter, so skip.
308+
if (converter := f.metadata.get("converter")) is None:
309+
continue # No converter, so skip.
309310

310-
try:
311-
signature = inspect.signature(converter)
312-
except ValueError:
313-
# e.g. `inspect.signature(str)` fails
314-
converter_annotation = Any
315-
else:
316-
parameters = list(signature.parameters.values())
317-
if len(parameters) == 0:
318-
# No idea what happened, but play it safe.
311+
try:
312+
signature = inspect.signature(converter)
313+
except ValueError:
314+
# e.g. `inspect.signature(str)` fails
319315
converter_annotation = Any
320316
else:
321-
converter_annotation = parameters[0].annotation
322-
if converter_annotation is inspect.Signature.empty:
317+
parameters = list(signature.parameters.values())
318+
if len(parameters) == 0:
319+
# No idea what happened, but play it safe.
323320
converter_annotation = Any
321+
else:
322+
converter_annotation = parameters[0].annotation
323+
if converter_annotation is inspect.Signature.empty:
324+
converter_annotation = Any
324325
cls.__init__.__annotations__[f.name] = converter_annotation
325326

326327
# Registering here records that the `dataclass(...)` call has happened.

0 commit comments

Comments
 (0)