def __init__(self, constants, ordering_attr=None, ordering_func=None, reorder_on_append=False): self.constants = constants OrderingList.__init__(self, ordering_attr=ordering_attr, ordering_func=ordering_func, reorder_on_append=reorder_on_append)
def test_picklability(self): from sqlalchemy.ext.orderinglist import OrderingList olist = OrderingList("order", reorder_on_append=True) olist.append(DummyItem()) for loads, dumps in picklers(): pck = dumps(olist) copy = loads(pck) self.assert_(copy == olist) self.assert_(copy.__dict__ == olist.__dict__)
class A: __tablename__ = "a" id = Column(Integer, primary_key=True) bs = relationship(B, collection_class=OrderingList("ordering")) bs_w_list: List[B] = relationship( B, collection_class=OrderingList("ordering") ) # EXPECTED: Left hand assignment 'cs: "List[B]"' not compatible with ORM mapped expression of type "Mapped[List[C]]" # noqa cs: List[B] = relationship(C, uselist=True) # EXPECTED: Left hand assignment 'cs_2: "B"' not compatible with ORM mapped expression of type "Mapped[List[C]]" # noqa cs_2: B = relationship(C, uselist=True)
class Variable(Base): """ A variable in a file (c.f. netCDF) """ __tablename__ = 'variable' id = Column(Integer, primary_key=True) meta_id = Column(Integer, ForeignKey('metadata.id')) #: Variable name name = Column(String) #: Variable type type = Column(String) #: :py:class:`Metadata` this is part of meta = relationship('Metadata', back_populates='variables') #: :py:class:`Attribute` s of this variable (dict) attributes = relationship( 'Attribute', secondary=var_to_attr, collection_class=attribute_mapped_collection('key'), ) #: :py:class:`Dimension` s of this variable dimensions = relationship( 'Dimension', secondary=var_to_dim, order_by='var_to_dim.c.ndim', collection_class=OrderingList('var_to_dim.c.ndim'), )
def ordering_list(attr, count_from=None, **kw): kw = _unsugar_count_from(count_from=count_from, **kw) constants = kw.pop('constants', None) if constants: return lambda: OrderingListWithConstants(constants, attr, **kw) else: return lambda: OrderingList(attr, **kw)
class A: __tablename__ = "a" id = Column(Integer, primary_key=True) # EXPECTED: Can't infer type from ORM mapped expression assigned to attribute 'parents'; please specify a Python type or Mapped[<python type>] on the left hand side. # noqa parents = relationship("A", collection_class=OrderingList("ordering")) parent_id = Column(Integer, ForeignKey("a.id")) ordering = Column(Integer)
id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey("a.id")) ordering = Column(Integer) @mapper_registry.mapped class A: __tablename__ = "a" id = Column(Integer, primary_key=True) bs = relationship(B, collection_class=OrderingList("ordering")) bs_w_list: List[B] = relationship( B, collection_class=OrderingList("ordering") ) # EXPECTED: Left hand assignment 'cs: "List[B]"' not compatible with ORM mapped expression of type "Mapped[List[C]]" # noqa cs: List[B] = relationship(C, uselist=True) # EXPECTED: Left hand assignment 'cs_2: "B"' not compatible with ORM mapped expression of type "Mapped[List[C]]" # noqa cs_2: B = relationship(C, uselist=True) b1 = B(ordering=10) # in this case, the plugin infers OrderingList as the type. not great a1 = A(bs=OrderingList(b1)) # so we want to support being able to override it at least a2 = A(bs_w_list=[b1])
def remove(self, entity): self._unset_constants(entity) OrderingList.remove(self, entity)
def pop(self, index=-1): entity = OrderingList.pop(self, index=index) self._unset_constants(entity) return entity
def insert(self, index, entity): self._set_constants(entity) OrderingList.insert(self, index, entity)
def append(self, entity): self._set_constants(entity) OrderingList.append(self, entity)