tobgu/pyrsistent

Mixin support?

Open

#130 ouverte le 24 avr. 2018

Voir sur GitHub
 (7 commentaires) (0 réactions) (0 assignés)Python (141 forks)batch import
enhancementhelp wanted

Métriques du dépôt

Stars
 (1 943 stars)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

Description

  1. Mixining two classes that both inherit from pyrsistent.PClass fails:
import pyrsistent

class TimestampMixin(pyrsistent.PClass):
    created_at = pyrsistent.field()
    deleted_at = pyrsistent.field()
    updated_at = pyrsistent.field()

class IDMixin(pyrsistent.PClass):
    id = pyrsistent.field()

class UserEntity(IDMixin, TimestampMixin):
    pass

produces:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-808a9f22c5af> in <module>()
      9     id = pyrsistent.field()
     10
---> 11 class UserEntity(IDMixin, TimestampMixin):
     12     pass

~/.local/share/virtualenvs/server-hvEypR1j/lib/python3.6/site-packages/pyrsistent/_pclass.py in __new__(mcs, name, bases, dct)
     20             dct['__slots__'] += ('__weakref__',)
     21
---> 22         return super(PClassMeta, mcs).__new__(mcs, name, bases, dct)
     23
     24 _MISSING_VALUE = object()

TypeError: multiple bases have instance lay-out conflict
  1. Using a mixin which doesn't inherit from PClass doesn't have it's fields registered:
import pyrsistent

class TimestampMixin:
    created_at = pyrsistent.field()
    deleted_at = pyrsistent.field()
    updated_at = pyrsistent.field()

class IDMixin(pyrsistent.PClass):
    id = pyrsistent.field()

class UserEntity(IDMixin, TimestampMixin):
    pass
assert list(UserEntity._pclass_fields) == ['id']  # expecting: id, created_at, deleted_at, updated_at

Is there a suggested way to use mixins without falling back to: type('UserEntity', (pyrsistent.PClass,), namespace) where namespace is effectively a user-manifest mixin?

Guide contributeur