Purify

raw JSON →
0.3.0 verified Fri May 01 auth: no python

A library for creating Pythonic object-mutator transforms as pure functions. Version 0.3.0 supports Python 3.10–3.14. Current release cadence is irregular.

pip install purify
error AttributeError: 'NoneType' object has no attribute 'name'
cause Calling a mutating method on an instance and expecting the original to change. Because purify returns a new object, the original is unchanged.
fix
Reassign the result: p = p.update_name('Bob') instead of p.update_name('Bob').
error TypeError: __init__() should return None, not 'Person'
cause Attempting to use @purify on a class that already returns a new instance from __init__ or uses a custom __new__. Purify expects __init__ to set attributes only.
fix
Remove any return statements from __init__ and avoid overriding __new__.
error ImportError: cannot import name 'purify' from 'purify'
cause Older versions of purify (pre-0.2.0) had a different module structure. The correct import is 'from purify import purify' (the decorator and the package share the same name).
fix
Use 'from purify import purify' and ensure you have purify >=0.2.0.
breaking Purify 0.3.0 drops support for Python < 3.10. If you are on Python 3.9 or older, pin to <0.3.0.
fix Upgrade to Python >=3.10 or pin purify<0.3.0.
gotcha The @purify decorator only works on classes with a well-defined __init__ that uses simple attribute assignment. Classes with complex initialization or metaclasses may not be supported.
fix Ensure your class __init__ only sets attributes directly (e.g., self.x = x). Avoid property setters or custom __setattr__.

Decorate a class with @purify to make all instance methods return new objects instead of mutating the original.

from purify import purify

@purify
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age

p = Person('Alice', 30)
# Now p.name = 'Alice', p.age = 30
# Under the hood, mutating methods return new instances