Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: UnboundLocalError arising from headerless .hap files #229

Merged
merged 3 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading