python - Metaclass conflict when separating classes into separate files -
edit
the problem in importing. should have done write: from someinterface import someinterface
. should write module name in lowercase someinterface.py
per python styleguide (pep 8).
i have file model.py
defines classes related db instantiates base.
# model.py metadata = metadata() declarativebase = declarative_base() metadata = declarativebase.metadata class bar(declarativebase): __tablename__ = 'bar' __table_args__ = {} # column , relation definitions
the file model.py
autogenerated can't touch it. did instead create file called modelaugmented.py
add functionality of model classes via inheritance.
# modelaugmented.py model import * import someinterface class baraugmented(bar, someinterface): pass # someinterface.py class someinterface(object): some_method(): pass
the problem i'm having classes baraugmented
, i'm getting following error:
typeerror: error when calling metaclass bases metaclass conflict: metaclass of derived class must (non-strict) subclass of metaclasses of bases
i error when someinterface
in separate file instead of being inside modelaugmented.py
.
i understand metaclass someinterface
, bar
different. problem can't figure out how resolve problem. tried solution suggested in triple inheritance causes metaclass conflict... sometimes works in example given, not in case. not sure if sqlalchmey has it.
class metaab(type(declarativebase), type(someinterface)): pass class baraugmented(bar, someinterface): __metaclass__ = metaab
but error:
typeerror: error when calling metaclass bases multiple bases have instance lay-out conflict
using sqlalchemy 0.8 , python 2.7.
ok, there must i'm missing, because created similar file layout yours (i think) , works in machine. appreciate kept question short , simple, maybe missing little detail alters... something? dunno... (maybe someinterface
has abc.abstract
metaclass?) if update question, please let me know trough comment answer, , i'll try update answer.
here goes:
file stack29a.py
(equivalent model.py
):
from sqlalchemy import create_engine, column, integer, string sqlalchemy.ext.declarative import declarative_base sqlalchemy.orm import sessionmaker, scoped_session dsn = "mysql://root:foobar@localhost/so_tests?charset=utf8" engine = create_engine(dsn) session = scoped_session(sessionmaker(bind=engine)) session = session() declarativebase = declarative_base() class bar(declarativebase): __tablename__ = 'bar' _id = column('_id', integer, primary_key=true) email = column('email', string(50))
file stack29b.py
(equivalent someinterface.py
):
class someinterface(object): def some_method(self): print "hellou"
file stack29c.py
(equivalent modelaugmented.py
):
from stack29a import bar stack29b import someinterface class baraugmented(bar, someinterface): pass
file stack29d.py
(like kind of main.py
: table creator , sample):
from stack29c import baraugmented stack29a import session, engine, declarativebase if __name__ == "__main__": declarativebase.metadata.create_all(engine) b1 = baraugmented() b1.email = "foo@bar.baz" b2 = baraugmented() b2.email = "baz@bar.foo" session.add_all([b1, b2]) session.commit() b3 = session.query(baraugmented)\ .filter(baraugmented.email == "foo@bar.baz")\ .first() print "b3.email: %s" % b3.email b3.some_method()
if run "main" file (stack29d.py
) works expected:
(venv_so)borrajax@borrajax:~/documents/tests$ python ./stack29d.py b3.email: foo@bar.baz hellou
Comments
Post a Comment