Skip to content

AOP Module

The AOP module gives you the possibility to define aspects that will participate in method execution flows.

Example: all method executions of methods named "foo" will include a before aspect, that will be executed before the original method

@advice
class Advice:
   @before(methods().named("foo"))
   def before_call(self, invocation: Invocation):
      ...

Note, that this requires that both the advice and the targeted methods need to be managed by an environment.

AspectTarget()

Bases: ABC

AspectTarget defines the target for an aspect. It can be used to specify the class, method, and conditions under which the aspect should be applied. It supports matching by class type, method name, patterns, decorators, and more.

decorated_with(decorator)

matches methods or classes that are decorated with the specified decorator

Parameters:

Name Type Description Default
decorator Callable

the decorator callable

required

Returns:

Name Type Description
AspectTarget AspectTarget

self

matches(pattern)

Matches the target against a pattern.

Parameters:

Name Type Description Default
pattern str

the pattern

required

Returns:

Name Type Description
AspectTarget AspectTarget

self

named(name)

Matches the target against a name.

Parameters:

Name Type Description Default
name str

the name

required

Returns:

Name Type Description
AspectTarget AspectTarget

self

of_type(type)

matches methods belonging to a class or classes that are subclasses of the specified type

Parameters:

Name Type Description Default
type Type

the type to match against

required

Returns:

Name Type Description
AspectTarget AspectTarget

self

that_are_async()

matches methods that are async

Returns:

Name Type Description
AspectTarget AspectTarget

self

that_are_sync()

matches methods that are sync

Returns:

Name Type Description
AspectTarget AspectTarget

self

Invocation(func, aspects)

Invocation stores the relevant data of a single method invocation. It holds the arguments, keyword arguments, result, error, and the aspects that define the aspect behavior.

proceed(*args, **kwargs)

Proceed to the next aspect in the around chain up to the original method.

proceed_async(*args, **kwargs) async

Proceed to the next aspect in the around chain up to the original method.

advice(cls)

Classes decorated with @advice are treated as advice classes. They can contain methods decorated with @before, @after, @around, or @error to define aspects.

after(*targets)

Methods decorated with @after will be executed after the target method is invoked.

around(*targets)

Methods decorated with @around will be executed around the target method. Every around method must accept a single parameter of type Invocation and needs to call proceed on this parameter to proceed to the next around method.

before(*targets)

Methods decorated with @before will be executed before the target method is invoked.

classes()

Create a new AspectTarget instance to define class aspect targets.

Returns:

Name Type Description
AspectTarget ClassAspectTarget

the method target

error(*targets)

Methods decorated with @error will be executed if the target method raises an exception.

methods()

Create a new AspectTarget instance to define method aspect targets.

Returns:

Name Type Description
AspectTarget MethodAspectTarget

the method target