Skip to main content

A stupid workaround when using multiple inheritance and __slots__

Project description

stupid

A stupid workaround when using multiple inheritance and __slots__

installation

pip install stupid

example

from hashlib import sha512
from stupid import StupidData

class User(StupidData):
    name: str
                       
class AccessData(StupidData):
    access_code: sha512

class ApiPayload(User, AccessData):
    pass

payload = ApiPayload("root", sha512()) 
payload = ApiPayload(name="root", access_code=sha512())

__slots__ are autogenerated and works with multiple inheritance as shown above. As __slots__ are present assigning new attributes is not allowed.

>>> payload = ApiPayload(name="root", access_code=sha512())
>>> payload.extra = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'ApiPayload' object has no attribute 'extra'

It's also a dataclass.

>>> from dataclasses import is_dataclass
>>> is_dataclass(ApiPayload)
True

isinstance check also works.

>>> isinstance(payload, User)
True
>>> isinstance(payload, AccessData)
True

issubclass

>>> from stupid import StupidData
>>> class A(StupidData): pass
>>> class B(A): pass
>>> class C:pass
>>>
>>> issubclass(B, A)
True
>>> issubclass(B, C)
False

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

stupid-1.1.1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

stupid-1.1.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file stupid-1.1.1.tar.gz.

File metadata

  • Download URL: stupid-1.1.1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.3 Linux/5.0.0-38-generic

File hashes

Hashes for stupid-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9074ab40d033ecfc6fccf6c3777423de97ecbd9fc82d8876964845cfd7d7bfbc
MD5 c9c5bc7215f27100c4cfc9fb577c18e2
BLAKE2b-256 29816652964f29d2ff2cc624cd698e55a40e74ea7806f2d71181b48c49b24005

See more details on using hashes here.

File details

Details for the file stupid-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: stupid-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.7.3 Linux/5.0.0-38-generic

File hashes

Hashes for stupid-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7ec68e5226c803cfbdfbe327a9e24f50d4a26f0df3a0e772d0d8494e8213bd5c
MD5 593baff99b695bd87f82a71d9008d406
BLAKE2b-256 578e6debd67e5696ca681cfa6683899ddca8b084973b0dae81ac09dbf3ff699a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page