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/attributes 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

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.1.tar.gz (5.3 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.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: disinheritance-1.1.1.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for disinheritance-1.1.1.tar.gz
Algorithm Hash digest
SHA256 4e9f4fe792a231a9628746a411c4bf384535f64304588750320835602b5b66bf
MD5 0949d74ec595aa4b362d3885240fbd27
BLAKE2b-256 325b10e5e266bca7a05d4ba5e4b813c3a9839ff9043f1549d1190fe76bb5a753

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for disinheritance-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 8287b1448b00cbac5096ebba03d68f6205830de9966abbadcc835fb7b8eef4f8
MD5 72c0d5164a6c6698761a186c48f6b306
BLAKE2b-256 add9373e19849300c8f384ec0bd4980635101ba143ec64419f484132ac6d2143

See more details on using hashes here.

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