mafw.processor.parameters

Parameter descriptors for the Processor framework.

This module defines the parameter descriptor system that underpins all Processor subclass configuration. It provides:

The helper functions _copy_parent_parameter_definitions(), _ensure_parameter_definitions(), and _validate_filter_schema() are used by the metaclass to finalise parameter bookkeeping at class-creation time.

Added in version 2.3: Extracted from the monolithic processor.py module for improved maintainability and focused testing.

Module Attributes

ParameterType

Generic variable type for the ActiveParameter and PassiveParameter.

Functions

ensure_parameter_registration(func)

Decorator to ensure that processor parameters are registered before func executes.

Classes

ActiveParameter(name[, value, default, help_doc])

The public interface to the processor parameter.

PassiveParameter(name[, value, default, ...])

An helper class to store processor parameter value and metadata.

class mafw.processor.parameters.ActiveParameter(name: str, value: ParameterType | None = None, default: ParameterType | None = None, help_doc: str = '')[source]

Bases: Generic[ParameterType]

The public interface to the processor parameter.

The behaviour of a processor.Processor can be customised by using processor parameters. The value of these parameters can be either set via a configuration file or directly when creating the class.

If the user wants to benefit from this facility, they have to add in the instance of the Processor subclass an ActiveParameter instance in this way:

class MyProcessor(Processor):

    # this is the input folder
    input_folder = ActiveParameter('input_folder', Path(r'C:\'), help_doc='This is where to look for input files')

    def __init__(self, *args, **kwargs):
        super().__init(*args, **kwargs)

        # change the input folder to something else
        self.input_folder = Path(r'D:\data')

        # get the value of the parameter
        print(self.input_folder)

The ActiveParameter is a descriptor, it means that when you create one of them, a lot of work is done behind the scene.

In simple words, a processor parameter is made by two objects: a public interface where the user can easily access the value of the parameter and a private interface where all other information (default, documentation…) is also stored.

The user does not have to take care of all of this. When a new ActiveParameter instance is added to the class as in the code snippet above, the private interface is automatically created and will stay in the class instance until the end of the class lifetime.

To access the private interface, the user can use the processor.Processor.get_parameter() method using the parameter name as a key.

The user can assign to an ActiveParameter almost any name. There are just a few invalid parameter names that are used for other purposes. The list of reserved names is available here. Should the user inadvertently use a reserved named, a ProcessorParameterError is raised.

See also

The private counterpart in the processor.PassiveParameter.

An explanation on how processor parameters work and should be used is given in Understanding processor parameters

The list of reserved names.

Constructor parameters:

Parameters:
  • name (str) – The name of the parameter.

  • value (ParameterType, Optional) – The initial value of the parameter. Defaults to None.

  • default (ParameterType, Optional) – The default value of the parameter, to be used when value is not set., Defaults to None.

  • help_doc (str, Optional) – An explanatory text describing the parameter.

static _matches_container(annotation: type | None, default_value: Any, container: type) bool[source]

Generic check for whether annotation/default matches a given container type.

_is_dict_annotation(annotation: type | None, default_value: Any) bool[source]

Check whether the annotation or default value indicates a dict type.

_is_list_annotation(annotation: type | None, default_value: Any) bool[source]

Check whether the annotation or default value indicates a list type.

_resolve_parameter_annotation() type | None[source]

Resolve the type annotation for this parameter from the owner class hints.

_schema_default_value() Any[source]

Return a safe copy of the default/initial value for schema reporting.

_validate_name(proposed_name: str) str[source]

Validate that the proposed parameter name is not in the list of forbidden names.

This private method checks if the provided name is allowed for use as a processor parameter. Names that are listed in reserved_names cannot be used as parameter names.

Parameters:

proposed_name (str) – The name to be validated for use as a processor parameter.

Returns:

The validated name if it passes the forbidden names check.

Return type:

str

Raises:

ProcessorParameterError – If the proposed name is in the list of forbidden names.

to_schema() ParameterSchema[source]

Returns the static schema describing this parameter.

The schema is derived solely from the descriptor metadata and does not instantiate the owning processor.

reserved_names: list[str] = ['__logic__', '__filter__', '__new_only__', '__inheritance__', '__enable__']

A list of names that cannot be used as processor parameter names.

  • __logic__

  • __filter__

  • __new_only__

  • __inheritance__

  • __enable__

class mafw.processor.parameters.ParameterType

Generic variable type for the ActiveParameter and PassiveParameter.

alias of TypeVar(‘ParameterType’)

class mafw.processor.parameters.PassiveParameter(name: str, value: ParameterType | None = None, default: ParameterType | None = None, help_doc: str = '')[source]

Bases: Generic[ParameterType]

An helper class to store processor parameter value and metadata.

This class is the private interface used by the ActiveParameter descriptor to store its value and metadata.

When a new ActiveParameter is added to a class, an instance of a PassiveParameter is added to the processor parameter register.

See also

An explanation on how processor parameters work and should be used is given in Understanding processor parameters

Changed in version v2.0.0: User should only use ActiveParameter and never manually instantiate PassiveParameter.

Constructor parameters:

Parameters:
  • name (str) – The name of the parameter. It must be a valid python identifier.

  • value (ParameterType, Optional) – The set value of the parameter. If None, then the default value will be used. Defaults to None.

  • default (ParameterType, Optional) – The default value for the parameter. It is used if the value is not provided. Defaults to None.

  • help_doc (str, Optional) – A brief explanation of the parameter.

Raises:

ProcessorParameterError – if both value and default are not provided or if name is not a valid identifier.

property is_optional: bool

Property to check if the parameter is optional.

Returns:

True if the parameter is optional

Return type:

bool

property is_set: bool

Property to check if the value has been set.

It is useful for optional parameter to see if the current value is the default one, or if the user set it.

property value: ParameterType

Gets the parameter value.

Returns:

The parameter value.

Return type:

ParameterType

Raises:

ProcessorParameterError – if both value and default were not defined.

class mafw.processor.parameters._F

Type variable for generic callable with any return value (private to avoid Sphinx collision).

alias of TypeVar(‘_F’, bound=Callable[[…], Any])

mafw.processor.parameters._copy_parent_parameter_definitions(owner: type[mafw.processor.Processor]) _ParameterRegistry[source]

Build an ordered dictionary of ActiveParameters inherited from base classes.

Walks the MRO (skipping the class itself) and collects all parameter descriptors declared on ancestor classes. This ensures that subclasses inherit parameters defined higher in the hierarchy.

Parameters:

owner – The Processor subclass whose ancestors are inspected.

Returns:

Aggregated parameter definitions from all base classes.

Return type:

_ParameterRegistry

mafw.processor.parameters._ensure_parameter_definitions(owner: type[mafw.processor.Processor]) _ParameterRegistry[source]

Return the class-level parameter registry, creating it if necessary.

If the class does not yet have its own _parameter_definitions dict (i.e. it has not been touched by the metaclass), one is created by copying definitions from the parent classes.

Parameters:

owner (type[mafw.processor.Processor]) – The Processor subclass to inspect/initialise.

Returns:

The parameter registry for owner.

Return type:

_ParameterRegistry

mafw.processor.parameters._validate_filter_schema(owner: type[mafw.processor.Processor]) None[source]

Validate the optional FilterSchema declared on a Processor.

Checks that:

  • _filter_schema is an instance of FilterSchema (if present).

  • root_model inherits from MAFwBaseModel.

  • All entries in allowed_models are unique MAFwBaseModel subclasses.

Parameters:

owner (type[mafw.processor.Processor]) – The Processor subclass being validated.

Raises:

ProcessorParameterError – If any of the above constraints are violated.

mafw.processor.parameters.ensure_parameter_registration(func: _F) _F[source]

Decorator to ensure that processor parameters are registered before func executes.

This is applied to methods that access self._processor_parameters and need the registration step to have completed first.

Parameters:

func (_F) – The method to wrap.

Returns:

The wrapped method.

Return type:

_F

Raises:

ProcessorParameterError – If applied to something that is not a Processor instance method.

mafw.processor.parameters._ParameterRegistry

Ordered mapping of external parameter names to their ActiveParameter descriptors.

alias of OrderedDict[str, ActiveParameter[Any]]