Skip to content

Commit

Permalink
fix: UnboundLocalError arising from headerless .hap files (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
aryarm authored Nov 29, 2023
1 parent 56dddf4 commit a499b0c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
8 changes: 4 additions & 4 deletions haptools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# handles py3.7, since importlib.metadata was introduced in py3.8
from importlib_metadata import version, PackageNotFoundError

try:
__version__ = version(__name__)
except PackageNotFoundError:
__version__ = "unknown"
try:
__version__ = version(__name__)
except PackageNotFoundError:
__version__ = "unknown"
4 changes: 2 additions & 2 deletions haptools/data/haplotypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def fmt_str(self) -> str:

class classproperty(object):
"""
A daad-simple read-only decorator that combines the functionality of
A dead-simple read-only decorator that combines the functionality of
@classmethod and @property
Stolen from https://stackoverflow.com/a/13624858/16815703
Expand Down Expand Up @@ -1231,7 +1231,7 @@ def __iter__(
# These are usually just comment lines, so we can ignore it
pass
else:
if header_lines:
if header_lines is not None:
metas, extras = self.check_header(header_lines)
types = self._get_field_types(extras, metas.get("order"))
header_lines = None
Expand Down
14 changes: 14 additions & 0 deletions tests/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,6 +1184,20 @@ def test_load(self):
haps = Haplotypes.load(DATADIR / "basic.hap.gz")
assert expected == haps.data

def test_load_no_header(self):
expected = self._basic_haps()

# what if we remove the header line, can we still load it?
# let's try to make a version without the header
no_header_file = DATADIR / "basic_no_header.hap"
with open(DATADIR / "basic.hap", "r") as fr, open(no_header_file, "w") as fw:
fw.writelines([ln for ln in fr.readlines() if not ln.startswith("#\t")])

haps = Haplotypes.load(no_header_file)
assert expected == haps.data

no_header_file.unlink()

def test_iterate(self):
exp_full = self._basic_haps()

Expand Down

0 comments on commit a499b0c

Please sign in to comment.