Skip to content

Commit

Permalink
Use super() now that we support only Python 3
Browse files Browse the repository at this point in the history
However leave calls in cdef functions as is; 0-argument super() does
not work in cdef or cpdef Cython functions (see cython/cython#3726).

The explicit superclass calls in libctabixproxies.pyx were jumping
straight to the grandparent class. Using `super()` instead uses
NamedTupleProxy's versions of these methods, but it doesn't declare
any so calls use the grandparent's methods unchanged. Update the
calls in cdef functions similarly for explicitness.

TODO TestVCFFromVariantFile similarly directly calls the grandparent's
setUp()/tearDown() methods, but changes behaviour as it skips those of
the direct parent. This deserves clarification.
  • Loading branch information
jmarshall committed Nov 16, 2023
1 parent 33120e2 commit c328702
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 44 deletions.
4 changes: 2 additions & 2 deletions linker_tests/link_pre_489/cy_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def is_pip_install():
class CyExtension(Extension):
def __init__(self, *args, **kwargs):
self._init_func = kwargs.pop("init_func", None)
Extension.__init__(self, *args, **kwargs)
super().__init__(*args, **kwargs)

def extend_includes(self, includes):
self.include_dirs.extend(includes)
Expand Down Expand Up @@ -83,4 +83,4 @@ def build_extension(self, ext):

ext.extra_link_args += ['-Wl,-rpath,$ORIGIN']

build_ext.build_extension(self, ext)
super().build_extension(ext)
24 changes: 7 additions & 17 deletions pysam/libcalignmentfile.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2076,8 +2076,7 @@ cdef class IteratorRowRegion(IteratorRow):
if not samfile.has_index():
raise ValueError("no index available for iteration")

IteratorRow.__init__(self, samfile,
multiple_iterators=multiple_iterators)
super().__init__(samfile, multiple_iterators=multiple_iterators)
with nogil:
self.iter = sam_itr_queryi(
self.index,
Expand Down Expand Up @@ -2133,9 +2132,7 @@ cdef class IteratorRowHead(IteratorRow):
AlignmentFile samfile,
int n,
int multiple_iterators=False):

IteratorRow.__init__(self, samfile,
multiple_iterators=multiple_iterators)
super().__init__(samfile, multiple_iterators=multiple_iterators)

self.max_rows = n
self.current_row = 0
Expand Down Expand Up @@ -2183,11 +2180,8 @@ cdef class IteratorRowAll(IteratorRow):
"""

def __init__(self, AlignmentFile samfile,
int multiple_iterators=False):

IteratorRow.__init__(self, samfile,
multiple_iterators=multiple_iterators)
def __init__(self, AlignmentFile samfile, int multiple_iterators=False):
super().__init__(samfile, multiple_iterators=multiple_iterators)

def __iter__(self):
return self
Expand Down Expand Up @@ -2226,11 +2220,8 @@ cdef class IteratorRowAllRefs(IteratorRow):
"""

def __init__(self, AlignmentFile samfile,
multiple_iterators=False):

IteratorRow.__init__(self, samfile,
multiple_iterators=multiple_iterators)
def __init__(self, AlignmentFile samfile, multiple_iterators=False):
super().__init__(samfile, multiple_iterators=multiple_iterators)

if not samfile.has_index():
raise ValueError("no index available for fetch")
Expand Down Expand Up @@ -2291,8 +2282,7 @@ cdef class IteratorRowSelection(IteratorRow):
"""

def __init__(self, AlignmentFile samfile, positions, int multiple_iterators=True):

IteratorRow.__init__(self, samfile, multiple_iterators=multiple_iterators)
super().__init__(samfile, multiple_iterators=multiple_iterators)

self.positions = positions
self.current_pos = 0
Expand Down
6 changes: 2 additions & 4 deletions pysam/libctabix.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,8 @@ cdef class TabixIteratorParsed(TabixIterator):
Returns parsed data.
"""

def __init__(self,
Parser parser):

TabixIterator.__init__(self)
def __init__(self, Parser parser):
super().__init__()
self.parser = parser

def __next__(self):
Expand Down
9 changes: 4 additions & 5 deletions pysam/libctabixproxies.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ cdef class GTFProxy(NamedTupleProxy):
toDot(self.frame),
self.attributes))
else:
return TupleProxy.__str__(self)
return super().__str__()

def invert(self, int lcontig):
'''invert coordinates to negative strand coordinates
Expand Down Expand Up @@ -732,7 +732,7 @@ cdef class BedProxy(NamedTupleProxy):
nbytes does not include the terminal '\0'.
'''
TupleProxy.update(self, buffer, nbytes)
NamedTupleProxy.update(self, buffer, nbytes)

if self.nfields < 3:
raise ValueError(
Expand All @@ -754,11 +754,10 @@ cdef class BedProxy(NamedTupleProxy):
# def __get__( self ): return self.end

def __str__(self):

cdef int save_fields = self.nfields
# ensure fields to use correct format
self.nfields = self.bedfields
retval = TupleProxy.__str__(self)
retval = super().__str__()
self.nfields = save_fields
return retval

Expand Down Expand Up @@ -801,7 +800,7 @@ cdef class VCFProxy(NamedTupleProxy):
nbytes does not include the terminal '\0'.
'''
TupleProxy.update(self, buffer, nbytes)
NamedTupleProxy.update(self, buffer, nbytes)

self.contig = self.fields[0]
# vcf counts from 1 - correct here
Expand Down
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ class cythonize_sdist(sdist):
def run(self):
from Cython.Build import cythonize
cythonize(self.distribution.ext_modules)
sdist.run(self)
super().run()


# Override Cythonised build_ext command to customise macOS shared libraries.
Expand All @@ -237,7 +237,7 @@ class CyExtension(Extension):
def __init__(self, *args, **kwargs):
self._init_func = kwargs.pop("init_func", None)
self._prebuild_func = kwargs.pop("prebuild_func", None)
Extension.__init__(self, *args, **kwargs)
super().__init__(*args, **kwargs)

def extend_includes(self, includes):
self.include_dirs.extend(includes)
Expand Down Expand Up @@ -274,7 +274,7 @@ def run(self):
ldshared = os.environ.get('LDSHARED', sysconfig.get_config_var('LDSHARED'))
os.environ['LDSHARED'] = ldshared.replace('-bundle', '')

build_ext.run(self)
super().run()
try:
if HTSLIB_MODE != 'separate':
self.check_ext_symbol_conflicts()
Expand Down Expand Up @@ -318,7 +318,7 @@ def build_extension(self, ext):
if isinstance(ext, CyExtension) and ext._prebuild_func:
ext._prebuild_func(ext, self.force)

build_ext.build_extension(self, ext)
super().build_extension(ext)


class clean_ext(Command):
Expand Down
18 changes: 6 additions & 12 deletions tests/tabix_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,7 @@ class TestGZFile(IterationTest):
with_comments = True

def setUp(self):

IterationTest.setUp(self)
super().setUp()
self.gzfile = pysam.GZIterator(self.filename)

def testAll(self):
Expand All @@ -249,7 +248,7 @@ class TestIterationWithoutComments(IterationTest):
"example.gtf.gz")

def setUp(self):
IterationTest.setUp(self)
super().setUp()
self.tabix = pysam.TabixFile(self.filename)

def tearDown(self):
Expand Down Expand Up @@ -374,9 +373,6 @@ class TestIterationWithComments(TestIterationWithoutComments):

filename = os.path.join(TABIX_DATADIR, "example_comments.gtf.gz")

def setUp(self):
TestIterationWithoutComments.setUp(self)


class TestIterators(unittest.TestCase):
filename = os.path.join(TABIX_DATADIR, "example.gtf.gz")
Expand Down Expand Up @@ -592,15 +588,14 @@ class TestVCFFromTabix(TestVCF):
"filter", "info", "format")

def setUp(self):

TestVCF.setUp(self)
super().setUp()

self.tabix = pysam.TabixFile(self.tmpfilename + ".gz")
self.compare = load_and_convert(self.filename)

def tearDown(self):
self.tabix.close()
TestVCF.tearDown(self)
super().tearDown()

def testRead(self):

Expand Down Expand Up @@ -701,14 +696,13 @@ class TestVCFFromVCF(TestVCF):
missing_quality = -1

def setUp(self):

TestVCF.setUp(self)
super().setUp()

self.vcf = pysam.VCF()
self.compare = load_and_convert(self.filename, encode=False)

def tearDown(self):
TestVCF.tearDown(self)
super().tearDown()
self.vcf.close()

def open_vcf(self, fn):
Expand Down

0 comments on commit c328702

Please sign in to comment.