Skip to content

Commit 94b4cec

Browse files
committed
BF: fix for circular import error in gifti
Suggested fix for nipygh-392 caused by circular import. The image class needs the parser class but the parser class needs the image class.
1 parent 90cd1f1 commit 94b4cec

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

nibabel/gifti/gifti.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,15 @@ class GiftiImage(xml.XmlSerializable, FileBasedImage):
419419
valid_exts = ('.gii',)
420420
files_types = (('image', '.gii'),)
421421

422+
# The parser will in due course be a GiftiImageParser, but we can't set
423+
# that now, because it would result in a circular import. We set it after
424+
# the class has been defined, at the end of the class definition.
425+
parser = None
426+
422427
def __init__(self, header=None, extra=None, file_map=None, meta=None,
423428
labeltable=None, darrays=None, version="1.0"):
424429
super(GiftiImage, self).__init__(header=header, extra=extra,
425430
file_map=file_map)
426-
# placed here temporarily for git diff purposes
427-
from .parse_gifti_fast import GiftiImageParser
428-
GiftiImage.parser = GiftiImageParser
429-
430431
if darrays is None:
431432
darrays = []
432433
if meta is None:
@@ -606,3 +607,8 @@ def from_filename(klass, filename, buffer_size=35000000):
606607
file_map = klass.filespec_to_file_map(filename)
607608
img = klass.from_file_map(file_map, buffer_size=buffer_size)
608609
return img
610+
611+
612+
# Now GiftiImage is defined, we can import the parser module and set the parser
613+
from .parse_gifti_fast import GiftiImageParser
614+
GiftiImage.parser = GiftiImageParser

0 commit comments

Comments
 (0)