How to properly specify the return type of the Blender operator execute function

When using typing in code that calls the Blender Python API, we usually don’t encounter any problems. However, specifying the return type for the execute() function of the Blender operator is not entirely obvious.

We know that the execute() function, the main executable function of any Blender operator, always returns a set consisting of a single string element.

It can be:

In general, we can specify the return type of the execute() function as set[‘str’]

This will work. However, we can improve our code typing a bit by using a reference to rna_enums.OperatorReturnItems if we use fake-bpy-module for autocompletion.

The description of the type of the operator returned by the execute() function is described in fake-bpy-module in the bpy\_typing\rna_enums module

Further information provided by Andrej (Andrej730)

Basically, bpy._typing is even more fake than fake-bpy-modules themselves and doesn’t exist at all in real Blender.

So we need to ensure it’s never used at runtime.

Import it only inside TYPE_CHECKING block.

Refer to it only using quotes, so it won’t be evaluated.

The reason why those shenanigans needed – it’s a compromise between blender realities and Python typing system.

fake-bpy-module is providing exact possible enum values for the stuff like possible return values, but it requires those static-time-only things

Example code with formatting of the return value of the execute() operator function:

Update (04.06.2025) by Andrej (Andrej730)

In the latest version of fake-bpy-module, the bpy._typing module has been replaced with еру bpy.stub_internal.

For autocomplete to work correctly, we now need to specify bpy.stub_internal instead of bpy._typing. In the example above, it would look like this:

Instead of:

We need to write:

All other code remains unchanged.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments