Skip to main content

Direct management of inherited methods/attributes in subclassed object types

Project description

Python Disinheritance

Disinheritance is the removal of inherited methods and attributes to prevent (i.e., "disinherit") unwanted capabilities in subclass types. Nominally, the approach to disinheritance in Python is by individually overriding the unwanted methods and attributes of a subclass. However, depending on the behavior of a parent class, this can be a laborious process, especially if a large amount of parent methods and attributes are unwanted. Another approach is by structuring types and subclassed types to ensure limited inheritance (e.g., with "mixin" use), which could require more complexity in creating appropriate subclass types.

When other approaches are impractical, the disinheritance module may be a viable alternative. The disinherit decorator object automatically overrides unwanted inherited methods and attributes and prevents their use in subclass instances, except where specified as exemptions. Features of disinherit include:

  • Exemptions can be one or more type methods or even entire types in the subclass MRO (though exemptions are managed internally by type/containing type and declaration order)
  • Exemptions can be from anywhere in the MRO and will be applied to the subclass as overrides (unless already overriden in the subclass)
  • dir() call on a subclass instance will not include disinherited methods/attributes
  • Attempts at attribute retrieval of disinherited methods/attributes from an instance produce attribute errors
  • help() call on a subclass type will show disinherited methods/attributes as not implemented (i.e., as the NotImplemented singleton)
  • Invalid exemptions (i.e., types and type methods/attributes not in the MRO) are silently ignored
  • Subclass __dir__ and __getattribute__ methods are wrapped to both maintain original functionality and account for disinherited methods/attributes
  • Exemptions are explicitly specified using an exempt keyword argument for the sake of clarity and deliberate use in style
  • Note: type attributes cannot be directly referenced for exemptions where an attribute does not back-reference the containing type

Example:

from disinheritance import disinherit

@disinherit()
class StrStandin(str):
    """object does not function like a regular str"""

@disinherit(exempt=str.upper)
class StrUpper(str):
    """object allows upper method"""
>>> standin = StrStandin('hello, world!')
>>> upperonly = StrUpper('spam and eggs')
>>> dir(standin)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__',
'__getattribute__', '__getstate__', '__hash__', '__init__', '__init_subclass__',
'__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> standin.upper()
Traceback (most recent call last):
  File "<pyshell#87>", line 1, in <module>
    value.upper()
  File ".../disinheritance.py", line 188, in __getattribute__
    raise error
AttributeError: 'StrStandin' object has no attribute 'upper'
>>> upperonly.upper()
'SPAM AND EGGS'

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

disinheritance-1.1.2.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

disinheritance-1.1.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file disinheritance-1.1.2.tar.gz.

File metadata

  • Download URL: disinheritance-1.1.2.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for disinheritance-1.1.2.tar.gz
Algorithm Hash digest
SHA256 b3c178ff7172dd3f457faf86a05c3a6a41709066b4dd799e86d79abd15488115
MD5 d3f2843e64c79c57ae18edbe2ac9e4e2
BLAKE2b-256 0a4fda08273fe4117e5d8ac0567d3c3f19c98e93ea4a7324c265360cb93dd02f

See more details on using hashes here.

Provenance

The following attestation bundles were made for disinheritance-1.1.2.tar.gz:

Publisher: python-publish.yml on stannielson/Python-Disinheritance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file disinheritance-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: disinheritance-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for disinheritance-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2c8d928e5566e21c55bf7e6c6cac159985264513a47d205972f173f30ac70712
MD5 90215ea0a6da39ec986dde62f30eb2c4
BLAKE2b-256 10b36c2fa4b8332a34a22ae3e22a3253ba0ef0a3e01b96f24e77085c98015a8a

See more details on using hashes here.

Provenance

The following attestation bundles were made for disinheritance-1.1.2-py3-none-any.whl:

Publisher: python-publish.yml on stannielson/Python-Disinheritance

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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