API¶
Package accretive
¶
Data structures which can grow but never shrink - once values are set, they become immutable. This behavior is useful for configuration registries, plugin systems, and other scenarios requiring grow-only collections with immutability guarantees.
Dictionary
: A dict-like structure where entries can be added but not modified or removed once set. Variants include:ProducerDictionary
: Auto-generates values for missing keysValidatorDictionary
: Validates entries before additionProducerValidatorDictionary
: Combines both behaviors
Namespace
: Similar totypes.SimpleNamespace
but with immutable attributes after assignment.Module
: A module type that enforces attribute immutability after assignment.Object
: Base class for objects with accretive attributes.Class
: Metaclass for creating classes with accretive class attributes.
Module accretive.dictionaries
¶
Accretive dictionaries.
Dictionaries which can grow but never shrink. Once an entry is added, it cannot be modified or removed.
AbstractDictionary
: Base class defining the accretive dictionary interface. Implementations must provide__getitem__
,__iter__
,__len__
, and storage methods.Dictionary
: Standard implementation of an accretive dictionary. Supports all usual dict operations except those that would modify or remove existing entries.ProducerDictionary
: Automatically generates values for missing keys using a supplied factory function. Similar tocollections.defaultdict
but with accretive behavior.ValidatorDictionary
: Validates entries before addition using a supplied predicate function.ProducerValidatorDictionary
: Combines producer and validator behaviors. Generated values must pass validation before being added.
>>> from accretive import Dictionary
>>> d = Dictionary( apples = 12, bananas = 6 )
>>> d[ 'cherries' ] = 42 # Add new entry
>>> d[ 'apples' ] = 14 # Attempt modification
Traceback (most recent call last):
...
accretive.exceptions.EntryImmutabilityError: Cannot alter or remove existing entry for 'apples'.
>>> del d[ 'bananas' ] # Attempt removal
Traceback (most recent call last):
...
accretive.exceptions.EntryImmutabilityError: Cannot alter or remove existing entry for 'bananas'.
>>> from accretive import ProducerDictionary
>>> d = ProducerDictionary( list ) # list() called for missing keys
>>> d[ 'new' ]
[]
>>> d[ 'new' ].append( 1 ) # List is mutable, but entry is fixed
>>> d[ 'new' ] = [ ] # Attempt modification
Traceback (most recent call last):
...
accretive.exceptions.EntryImmutabilityError: Cannot alter or remove existing entry for 'new'.
>>> from accretive import ValidatorDictionary
>>> d = ValidatorDictionary( lambda k, v: isinstance( v, int ) )
>>> d[ 'valid' ] = 42 # Passes validation
>>> d[ 'invalid' ] = 'str' # Fails validation
Traceback (most recent call last):
...
accretive.exceptions.EntryValidityError: Cannot add invalid entry with key, 'invalid', and value, 'str', to dictionary.
- class accretive.dictionaries.AbstractDictionary¶
Bases:
Mapping
[H
,V
]Abstract base class for dictionaries that can grow but not shrink.
An accretive dictionary allows new entries to be added but prevents modification or removal of existing entries. This provides a middle ground between immutable and fully mutable mappings.
Implementations must provide: - __getitem__, __iter__, __len__ - _pre_setitem_ for entry validation/preparation - _store_item_ for storage implementation
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- setdefault(key: H, default: V) V ¶
Returns value for key, setting it to default if missing.
- update(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Adds new entries as a batch. Returns self.
- values() an object providing a view on D's values ¶
- class accretive.dictionaries.Dictionary(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')])¶
Bases:
_DictionaryOperations
[H
,V
]Accretive dictionary.
Prevents alteration or removal of dictionary entries after they have been added. Only addition of new dictionary entries is permitted.
- copy() Self ¶
Provides fresh copy of dictionary.
- get(key: H, default: V | AbsentSingleton = absence.absent) Annotated[V, Doc('Value of entry, if it exists. Else, supplied default value or ``None``.')] ¶
Retrieves entry associated with key, if it exists.
- setdefault(key: H, default: V) V ¶
Returns value for key, setting it to default if missing.
- update(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Adds new entries as a batch. Returns self.
- values() ValuesView[V] ¶
Provides iterable view over dictionary values.
- with_data(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Creates new dictionary with same behavior but different data.
- class accretive.dictionaries.ProducerDictionary(producer: Annotated[Callable[[], V], Doc('Callable which produces values for absent dictionary entries.')], /, *iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')])¶
Bases:
Dictionary
[H
,V
]Accretive dictionary with default value for missing entries.
Prevents alteration or removal of dictionary entries after they have been added. Only addition of new dictionary entries is permitted.
When an attempt to access a missing entry is made, then the entry is added with a default value. Modeled after
collections.defaultdict
.- copy() Self ¶
Provides fresh copy of dictionary.
- get(key: H, default: V | AbsentSingleton = absence.absent) Annotated[V, Doc('Value of entry, if it exists. Else, supplied default value or ``None``.')] ¶
Retrieves entry associated with key, if it exists.
- setdefault(key: H, default: V) V ¶
Returns value for key, setting it to default if missing.
- update(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Adds new entries as a batch. Returns self.
- values() ValuesView[V] ¶
Provides iterable view over dictionary values.
- with_data(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Creates new dictionary with same behavior but different data.
- class accretive.dictionaries.ProducerValidatorDictionary(producer: Annotated[Callable[[], V], Doc('Callable which produces values for absent dictionary entries.')], validator: Annotated[Callable[[H, V], bool], Doc('Callable which validates entries before addition to dictionary.')], /, *iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')])¶
Bases:
Dictionary
[H
,V
]Accretive dictionary with defaults and validation.
Prevents alteration or removal of dictionary entries after they have been added. Only addition of new dictionary entries is permitted.
When an attempt to access a missing entry is made, then the entry is added with a default value. Modeled after
collections.defaultdict
.When an attempt to add a new entry is made, then the entry is validated against supplied criteria. If validation fails, then the entry is rejected.
- copy() Self ¶
Provides fresh copy of dictionary.
- get(key: H, default: V | AbsentSingleton = absence.absent) Annotated[V, Doc('Value of entry, if it exists. Else, supplied default value or ``None``.')] ¶
Retrieves entry associated with key, if it exists.
- setdefault(key: H, default: V) V ¶
Returns value for key, setting it to default if missing.
- update(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Adds new entries as a batch. Returns self.
- values() ValuesView[V] ¶
Provides iterable view over dictionary values.
- with_data(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Creates new dictionary with same behavior but different data.
- class accretive.dictionaries.ValidatorDictionary(validator: Annotated[Callable[[H, V], bool], Doc('Callable which validates entries before addition to dictionary.')], /, *iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')])¶
Bases:
Dictionary
[H
,V
]Accretive dictionary with validation of new entries.
Prevents alteration or removal of dictionary entries after they have been added. Only addition of new dictionary entries is permitted.
When an attempt to add a new entry is made, then the entry is validated against supplied criteria. If validation fails, then the entry is rejected.
- copy() Self ¶
Provides fresh copy of dictionary.
- get(key: H, default: V | AbsentSingleton = absence.absent) Annotated[V, Doc('Value of entry, if it exists. Else, supplied default value or ``None``.')] ¶
Retrieves entry associated with key, if it exists.
- setdefault(key: H, default: V) V ¶
Returns value for key, setting it to default if missing.
- update(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Adds new entries as a batch. Returns self.
- values() ValuesView[V] ¶
Provides iterable view over dictionary values.
- with_data(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **entries: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')]) Self ¶
Creates new dictionary with same behavior but different data.
Module accretive.namespaces
¶
Accretive namespaces.
Provides a namespace type that can grow but never shrink. Once an attribute is set, it cannot be modified or removed. This provides a simple way to create objects with named attributes that become immutable after assignment.
The namespace implementation is modeled after types.SimpleNamespace
but adds accretive behavior. Like types.SimpleNamespace
, it
provides a simple __repr__
which lists all attributes.
>>> from accretive import Namespace
>>> ns = Namespace( apples = 12, bananas = 6 )
>>> ns.cherries = 42 # Add new attribute
>>> ns.apples = 14 # Attempt modification
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'apples'.
>>> del ns.bananas # Attempt deletion
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'bananas'.
- class accretive.namespaces.Namespace(*iterables: Annotated[Mapping[H, V] | Iterable[tuple[H, V]], Doc('Zero or more iterables from which to initialize dictionary data. Each iterable must be dictionary or sequence of key-value pairs. Duplicate keys will result in an error.')], **attributes: Annotated[V, Doc('Zero or more keyword arguments from which to initialize dictionary data.')])¶
Bases:
Object
Accretive namespaces.
A namespace is an object, whose attributes can be determined from iterables and keyword arguments, at initialization time. The string representation of the namespace object reflects its current instance attributes. Modeled after
types.SimpleNamespace
.Prevents reassignment or deletion of instance attributes after they have been assigned. Only assignment of new instance attributes is permitted.
Module accretive.modules
¶
Accretive modules.
Provides a module type that enforces attribute immutability after assignment. This helps ensure that module-level constants remain constant and that module interfaces remain stable during runtime.
The module implementation is derived from types.ModuleType
and
adds accretive behavior. This makes it particularly useful for:
Ensuring constants remain constant
Preventing accidental modification of module interfaces
Also provides a convenience function:
reclassify_modules
: Converts existing modules to accretive modules.
- class accretive.modules.Module(name, doc=None)¶
Bases:
ModuleType
Accretive modules.
Derived from
types.ModuleType
, this class is suitable for use as a Python module class.Prevents reassignment or deletion of module attributes after they have been assigned. Only assignment of new module attributes is permitted.
This behavior helps ensure that module-level constants remain constant and that module interfaces remain stable during runtime.
- accretive.modules.reclassify_modules(attributes: Annotated[Mapping[str, Any] | ModuleType | str, Doc('Module, module name, or dictionary of object attributes.')], recursive: Annotated[bool, Doc('Recursively reclassify package modules?')] = False) None ¶
Reclassifies modules to be accretive.
Can operate on individual modules or entire package hierarchies.
Notes¶
Only converts modules within the same package to prevent unintended modifications to external modules.
When used with a dictionary, converts any module objects found as values if they belong to the same package.
Has no effect on already-accretive modules.
Module accretive.classes
¶
Accretive classes.
Provides metaclasses for creating classes with accretive attributes. Once a class attribute is set, it cannot be reassigned or deleted.
The implementation includes:
Class
: Standard metaclass for accretive classes; derived fromtype
.ABCFactory
: Metaclass for abstract base classes; derived fromabc.ABCMeta
.ProtocolClass
: Metaclass for protocol classes; derived fromtyping.Protocol
.
These metaclasses are particularly useful for:
Creating classes with constant class attributes
Defining stable abstract base classes
Building protocol classes with fixed interfaces
>>> from accretive import Class
>>> class Example( metaclass = Class ):
... x = 1
>>> Example.y = 2 # Add new class attribute
>>> Example.x = 3 # Attempt reassignment
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete attribute 'x'.
For cases where some attributes need to remain mutable, use the mutables
parameter:
>>> class Config( metaclass = Class, mutables = ( 'version', ) ):
... name = 'MyApp'
... version = '1.0.0'
>>> Config.version = '1.0.1' # Can modify designated mutable attributes
>>> Config.version
'1.0.1'
>>> Config.name = 'NewApp' # Other attributes remain immutable
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete attribute 'name'.
- class accretive.classes.ABCFactory(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, decorators: Iterable[Callable[[type], type]] = (), docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = (), **args: Any)¶
Bases:
ABCMeta
Accretive abstract base class factory.
Derived from
type
, this is a metaclass. A metaclass is a class factory class. I.e., it is a class that produces other classes as its instances.Prevents reassignment or deletion of class attributes after they have been assigned. Only assignment of new class attributes is permitted.
- mro()¶
Return a type’s method resolution order.
- register(subclass)¶
Register a virtual subclass of an ABC.
Returns the subclass, to allow usage as a class decorator.
- class accretive.classes.Class(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, decorators: Iterable[Callable[[type], type]] = (), docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = (), **args: Any)¶
Bases:
type
Accretive class factory.
Derived from
type
, this is a metaclass. A metaclass is a class factory class. I.e., it is a class that produces other classes as its instances.Prevents reassignment or deletion of class attributes after they have been assigned. Only assignment of new class attributes is permitted.
- mro()¶
Return a type’s method resolution order.
- class accretive.classes.CompleteDataclass(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, decorators: Iterable[Callable[[type], type]] = (), docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = (), **args: Any)¶
Bases:
Class
Accretive dataclass factory.
Dataclasses from this factory produce immutable instances.
Derived from
type
, this is a metaclass. A metaclass is a class factory class. I.e., it is a class that produces other classes as its instances.Prevents reassignment or deletion of class attributes after they have been assigned. Only assignment of new class attributes is permitted.
- mro()¶
Return a type’s method resolution order.
- class accretive.classes.CompleteProtocolDataclass(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, decorators: Iterable[Callable[[type], type]] = (), docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = (), **args: Any)¶
Bases:
ProtocolClass
Accretive protocol dataclass factory.
Dataclasses from this factory produce immutable instances.
Derived from
type
, this is a metaclass. A metaclass is a class factory class. I.e., it is a class that produces other classes as its instances.Prevents reassignment or deletion of class attributes after they have been assigned. Only assignment of new class attributes is permitted.
- mro()¶
Return a type’s method resolution order.
- register(subclass)¶
Register a virtual subclass of an ABC.
Returns the subclass, to allow usage as a class decorator.
- class accretive.classes.Dataclass(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, decorators: Iterable[Callable[[type], type]] = (), docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = (), **args: Any)¶
Bases:
Class
Accretive dataclass factory.
Derived from
type
, this is a metaclass. A metaclass is a class factory class. I.e., it is a class that produces other classes as its instances.Prevents reassignment or deletion of class attributes after they have been assigned. Only assignment of new class attributes is permitted.
- mro()¶
Return a type’s method resolution order.
- class accretive.classes.ProtocolClass(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, decorators: Iterable[Callable[[type], type]] = (), docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = (), **args: Any)¶
Bases:
_ProtocolMeta
Accretive protocol class factory.
Derived from
type
, this is a metaclass. A metaclass is a class factory class. I.e., it is a class that produces other classes as its instances.Prevents reassignment or deletion of class attributes after they have been assigned. Only assignment of new class attributes is permitted.
- mro()¶
Return a type’s method resolution order.
- register(subclass)¶
Register a virtual subclass of an ABC.
Returns the subclass, to allow usage as a class decorator.
- class accretive.classes.ProtocolDataclass(name: str, bases: tuple[type, ...], namespace: dict[str, Any], *, decorators: Iterable[Callable[[type], type]] = (), docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = (), **args: Any)¶
Bases:
ProtocolClass
Accretive protocol dataclass factory.
Derived from
type
, this is a metaclass. A metaclass is a class factory class. I.e., it is a class that produces other classes as its instances.Prevents reassignment or deletion of class attributes after they have been assigned. Only assignment of new class attributes is permitted.
- mro()¶
Return a type’s method resolution order.
- register(subclass)¶
Register a virtual subclass of an ABC.
Returns the subclass, to allow usage as a class decorator.
Module accretive.objects
¶
Accretive objects.
Provides the base class for objects with accretive attributes. Once an attribute is set on an instance, it cannot be reassigned or deleted.
The implementation uses a special dictionary type for attribute storage that enforces the accretive behavior. This makes it suitable as a base class for:
Configuration objects
Plugin interfaces
Immutable data containers
Objects requiring attribute stability
>>> from accretive import Object
>>> obj = Object( )
>>> obj.x = 1 # Add new instance attribute
>>> obj.y = 2 # Add another instance attribute
>>> obj.x = 3 # Attempt modification
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'x'.
The accretive decorator can be used to make any class accretive:
>>> from accretive import accretive
>>> @accretive
... class Config:
... def __init__( self, debug = False ):
... self.debug = debug
...
>>> config = Config( debug = True )
>>> config.debug # Access existing attribute
True
>>> config.verbose = True # Add new attribute
>>> config.debug = False # Attempt to modify existing attribute
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'debug'.
- class accretive.objects.Object(*posargs: Any, **nomargs: Any)¶
Bases:
object
Accretive objects.
Prevents reassignment or deletion of instance attributes after they have been assigned. Only assignment of new instance attributes is permitted.
- accretive.objects.accretive(class_: type[C] | AbsentSingleton = absence.absent, *, docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = ()) type[C] | Callable[[type[C]], type[C]] ¶
Decorator which makes class accretive after initialization.
Cannot be applied to classes which define their own __setattr__ or __delattr__ methods.
This decorator can be used in different ways:
Simple decorator:
>>> @accretive ... class Config: ... pass
With parameters:
>>> @accretive( mutables = ( 'version', ) ) ... class Config: ... pass
Module accretive.exceptions
¶
Family of exceptions for package API.
Provides a hierarchy of exceptions that are raised when accretive behavior is violated. The hierarchy is designed to allow both specific and general exception handling.
- exception accretive.exceptions.AttributeImmutabilityError(name: str)¶
Bases:
Omnierror
,AttributeError
,TypeError
Attempt to reassign or delete immutable attribute.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- args¶
- name¶
attribute name
- obj¶
object
- exception accretive.exceptions.DecoratorCompatibilityError(class_name: str, method_name: str)¶
-
Attempt to apply decorator to incompatible class.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- args¶
- exception accretive.exceptions.EntryImmutabilityError(indicator: Hashable)¶
-
Attempt to update or remove immutable dictionary entry.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- args¶
- exception accretive.exceptions.EntryValidityError(indicator: Hashable, value: Any)¶
Bases:
Omnierror
,ValueError
Attempt to add invalid entry to dictionary.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- args¶
- exception accretive.exceptions.Omnierror¶
Bases:
Omniexception
,Exception
Base for error exceptions raised by package API.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- args¶
- exception accretive.exceptions.Omniexception¶
Bases:
ImmutableObject
,BaseException
Base for all exceptions raised by package API.
- with_traceback()¶
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- args¶
Module accretive.qaliases
¶
Qualified aliases to accretive data structures.
Provides aliases prefixed with “Accretive” for all core classes. These are useful for avoiding namespace collisions when importing from the package, particularly with common names like “Dictionary” or “Namespace”.
For example, instead of:
>>> from accretive import Dictionary
>>> # Possible conflict with other Dictionary classes
you could use:
>>> from accretive.qaliases import AccretiveDictionary
>>> # Clearly indicates the source and behavior
- accretive.qaliases.AbstractAccretiveDictionary
alias of
AbstractDictionary
- accretive.qaliases.AccretiveABCFactory
alias of
ABCFactory
- accretive.qaliases.AccretiveClass
alias of
Class
- accretive.qaliases.AccretiveDictionary
alias of
Dictionary
- accretive.qaliases.AccretiveModule
alias of
Module
- accretive.qaliases.AccretiveNamespace
alias of
Namespace
- accretive.qaliases.AccretiveObject
alias of
Object
- accretive.qaliases.AccretiveProducerDictionary
alias of
ProducerDictionary
- accretive.qaliases.AccretiveProducerValidatorDictionary
alias of
ProducerValidatorDictionary
- accretive.qaliases.AccretiveProtocolClass
alias of
ProtocolClass
- accretive.qaliases.AccretiveValidatorDictionary
alias of
ValidatorDictionary
- accretive.qaliases.accretive(class_: type[C] | AbsentSingleton = absence.absent, *, docstring: str | None | AbsentSingleton = absence.absent, mutables: Collection[str] = ()) type[C] | Callable[[type[C]], type[C]]
Decorator which makes class accretive after initialization.
Cannot be applied to classes which define their own __setattr__ or __delattr__ methods.
This decorator can be used in different ways:
Simple decorator:
>>> @accretive ... class Config: ... pass
With parameters:
>>> @accretive( mutables = ( 'version', ) ) ... class Config: ... pass
- accretive.qaliases.reclassify_modules_as_accretive(attributes: Annotated[Mapping[str, Any] | ModuleType | str, Doc('Module, module name, or dictionary of object attributes.')], recursive: Annotated[bool, Doc('Recursively reclassify package modules?')] = False) None
Reclassifies modules to be accretive.
Can operate on individual modules or entire package hierarchies.
Notes¶
Only converts modules within the same package to prevent unintended modifications to external modules.
When used with a dictionary, converts any module objects found as values if they belong to the same package.
Has no effect on already-accretive modules.