Skip to content

Commit 1d59c96

Browse files
committed
ENH: Add from_bytes method for subclasses with known codes
1 parent 2f2900d commit 1d59c96

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

nibabel/nifti1.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import numpy as np
2222
import numpy.linalg as npl
23-
from typing_extensions import TypeVar # PY312
23+
from typing_extensions import Self, TypeVar # PY312
2424

2525
from . import analyze # module import
2626
from .arrayproxy import get_obj_dtype
@@ -328,6 +328,12 @@ def __init__(
328328
self.code = code # type: ignore[assignment]
329329
self._content = content
330330

331+
@classmethod
332+
def from_bytes(cls, content: bytes) -> Self:
333+
if not hasattr(cls, 'code'):
334+
raise NotImplementedError('from_bytes() requires a class attribute `code`')
335+
return cls(cls.code, content)
336+
331337
# Handle (de)serialization of extension content
332338
# Subclasses may implement these methods to provide an alternative
333339
# view of the extension content. If left unimplemented, the content
@@ -509,6 +515,8 @@ class Nifti1DicomExtension(Nifti1Extension[DicomDataset]):
509515
header.
510516
"""
511517

518+
code = 2
519+
512520
def __init__(
513521
self,
514522
code: ty.Union[int, str],

0 commit comments

Comments
 (0)