question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

  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?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
tobgucommented, Jun 13, 2018

That’s OK, I’ll leave the issue open, label it as an enhancement that someone can grab if they like. There are a bunch of similar issues that I myself don’t have time to work on but which I happily would accept PRs for.

0reactions
gelatinouscube42commented, Aug 5, 2022

Is this behavior (of type 1 above) supported now?

I’ve been trying various tricks to get inheritance of fields to work, and this seems like it would have been an answer for my use case. Not sure where to start going about tooling around in the code itself to implement it if not yet there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mixin and @include - Sass
Mixins allow you to define styles that can be re-used throughout your stylesheet. They make it easy to avoid using non-semantic classes like...
Read more >
Sass @mixin and @include - W3Schools
The @mixin directive lets you create CSS code that is to be reused throughout the website. The @include directive is created to let...
Read more >
Documentation - Mixins - TypeScript
TypeScript's best mixin support is done via the class expression pattern. You can read more about how this pattern works in JavaScript here....
Read more >
Mixin - Wikipedia
In object-oriented programming languages, a mixin (or mix-in) is a class that contains ... or to work around lack of support for multiple...
Read more >
"supports" SASS mixin that provides minimal DRY benefits
"supports" SASS mixin that provides minimal DRY benefits - supports.scss.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found