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 theNotImplementedsingleton)- 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
exemptkeyword 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'
Metadata
Release files for disinheritance 1.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| disinheritance-1.1.2.tar.gz | 5.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| disinheritance-1.1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.0 kB
Release files / disinheritance-1.1.2.tar.gz
| Download URL | disinheritance-1.1.2.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b3c178ff7172dd3f457faf86a05c3a6a41709066b4dd799e86d79abd15488115
|
|
BLAKE2b-256 checksum How to use checksums |
0a4fda08273fe4117e5d8ac0567d3c3f19c98e93ea4a7324c265360cb93dd02f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 10, 2025.
Transparency logRelease files / disinheritance-1.1.2-py3-none-any.whl
| Download URL | disinheritance-1.1.2-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2c8d928e5566e21c55bf7e6c6cac159985264513a47d205972f173f30ac70712
|
|
BLAKE2b-256 checksum How to use checksums |
10b36c2fa4b8332a34a22ae3e22a3253ba0ef0a3e01b96f24e77085c98015a8a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Jul 10, 2025.
Transparency log