fake-bpy-module icon indicating copy to clipboard operation
fake-bpy-module copied to clipboard

Update: Create type aliases for rna enums

Open JonathanPlasse opened this issue 1 year ago • 4 comments
trafficstars

Purpose of the pull request

  • Follow up to #295

This PR adds type aliases for the literals that contain the enum items. Using the type aliases avoid having to manually add the different literal values.

Before

class Foo(bpy.types.Operator):
    def execute(self, context: bpy.type.Context) -> set[typing.Literal[
    "RUNNING_MODAL", "CANCELLED", "FINISHED", "PASS_THROUGH", "INTERFACE"
]]:
        ...

After

class Foo(bpy.types.Operator):
    def execute(self, context: bpy.type.Context) -> set["bpy.types.OperatorReturnItems"]:
        ...

Note that the type aliases are only available in the pyi files and not during runtime, so we must use forward reference when using them (i.e. use quotes around the type).

It also allows for auto-completion. Screenshot from 2024-08-11 18-03-39

Description about the pull request

DefaultValueNode has been added to DataNode and AttributeNode to store the value of the type alias. The tests fixtures have been updated accordingly. get_rna_enum_items() adds the type aliases to the document and refine the type to the type alias.

JonathanPlasse avatar Aug 11 '24 15:08 JonathanPlasse

Should I create a new node to use the PEP 695 type syntax instead?

JonathanPlasse avatar Aug 11 '24 18:08 JonathanPlasse

Should I create a new node to use the PEP 695 type syntax instead?

Could you show me the mock generated code? To solve #161, it seems necessary to do this.

nutti avatar Aug 12 '24 05:08 nutti

It would be nice if we could put OperatorReturnItems in some other module and not to bpy.types to emphasize that Blender doesn't have it originally. Maybe bpy.typing?

Andrej730 avatar Aug 12 '24 07:08 Andrej730

The CI was failing because the type syntax is Python 3.12+ only.

I move the enum literal types to bpy.typing like suggested. Enum type must be imported with from bpy.typing import <EnumItemType> as typing is not present in bpy.__init__.py. Here is a snapshot of the generated code of bpy.typing. Screenshot from 2024-08-15 00-33-24

JonathanPlasse avatar Aug 14 '24 22:08 JonathanPlasse