Closed
Description
I've been able to create a small example to reproduce this issue. It seems to be a combination of using an Entity subclass and accessing member functions.
Error
Traceback (most recent call last):
File "test.py", line 64, in <module>
print(list(list_location(['com.example'])))
File "test.py", line 48, in <genexpr>
return (x.as_tuple() for x in Location.get_by_path(path).children)
File "test.py", line 35, in as_tuple
if not self.parent:
File "<string>", line 2, in __get__
File "C:\Users\niklas\.virtualenvs\fatartifacts-LoWBpE4v\lib\site-packages\pony\utils\utils.py", line 58, in cut_traceback
return func(*args, **kwargs)
File "C:\Users\niklas\.virtualenvs\fatartifacts-LoWBpE4v\lib\site-packages\pony\orm\core.py", line 2099, in __get__
value = attr.get(obj)
File "C:\Users\niklas\.virtualenvs\fatartifacts-LoWBpE4v\lib\site-packages\pony\orm\core.py", line 2111, in get
seeds = obj._session_cache_.seeds[val._pk_attrs_]
AttributeError: 'NoneType' object has no attribute 'seeds'
Code
from pony import orm
db = orm.Database('sqlite', ':memory:')
class Location(db.Entity):
name = orm.Optional(str)
parent = orm.Optional('Location', reverse='children')
children = orm.Set('Location', cascade_delete=True)
orm.composite_index(name, parent)
@classmethod
def get_root(cls) -> 'Location':
root = cls.get(name='', parent=None)
if not root:
root = cls(name='', parent=None)
return root
@classmethod
def get_by_path(cls, path):
current = cls.get_root()
for i in range(len(path)):
current = cls.get(name=path[i], parent=current)
if not current:
raise ValueError(path)
return current
@classmethod
def from_path(cls, path):
parent = cls.get_by_path(path[:-1])
entity = cls(name=path[-1], parent=parent)
return entity
def as_tuple(self):
if not self.parent:
return (None, self.name)
return (self.parent.as_tuple(), self.name)
# XXX Remove this subclass and the error does not happen.
class Object(Location):
pass
@orm.db_session
def list_location(path):
# XXX Or don't use as_tuple() here.
return (x.as_tuple() for x in Location.get_by_path(path).children)
@orm.db_session
def create_location(path):
Location.from_path(path)
db.generate_mapping(create_tables=True)
with orm.db_session:
Location.get_root()
# XXX Or use this context manager.
# with orm.db_session:
print(list(list_location([])))
create_location(['com.example'])
create_location(['com.example', 'foobar'])
print(list(list_location(['com.example'])))
Unfortunately, none of the ways above to prevent the issue are an option in my use case. :( Any idea how to fix this?