mapper inheritance check should use issubclass()

Issue #271 resolved
Former user created an issue

I'm using the experimental mapper inheritance. I have three classes for extracting data from a file. It looks approximately like this:

class GetData(object):
  def __init__(self, filename):
    self.filename = filename
  def get_range(self, start, end):
    raise NotImplementedError

class MMap(GetData):
  def get_range(self, start, end):
    f = open(self.filename)
    m = mmap.mmap(f.fileno(), os.path.getsize(self.filename))
    return m[start:end](start:end)

class FromFile(GetData):
  def _open(self):
    raise NotImplementedError
  def getrange(self, start, end):
    f = self.open()
    f.seek(start)
    return f.read(end-start)

class FlatFile(FromFile):
  def _open(self):
    return open(self.filename)

class GZFile(FromFile):
  def _open(self):
    import gzip
    return gzip.GZipFile(self.filename)

I then set up mappers

getdata_mapper = mapper(GetData, filedata_table,
     polymorphic_on = filedata_table.c.filetype)
mmap_mapper = mapper(MMap, inherits=getdata_mapper, polymorphic_identity="mmap")
gzip_mapper = mapper(GZFile, inherits=getdata_mapper, polymorphic_identity="gzip")
flat_mapper = mapper(FlatFile, inherits=getdata_mapper, polymorphic_identity="flat")

This does not work.

  File "build/bdist.darwin-7.9.0-Power_Macintosh/egg/sqlalchemy/orm/mapper.py", line 188, in _do_compile
  File "build/bdist.darwin-7.9.0-Power_Macintosh/egg/sqlalchemy/orm/mapper.py", line 230, in _compile_inheritance
sqlalchemy.exceptions.ArgumentError: Class 'GZipFile' does not inherit from 'GetData'

The exception is raised here

            if self.class_.__mro__[1](1) != self.inherits.class_:
                raise exceptions.ArgumentError("Class '%s' does not inherit fro
m '%s'" % (self.class_.__name__, self.inherits.class_.__name__))

I think this should use 'issubclass()' instead of an explicit test to the second element in the mro.

Comments (1)

  1. Log in to comment