nutti / fake-bpy-module

Fake Blender Python API module collection for the code completion.
MIT License
1.42k stars 98 forks source link

Update: Create type aliases for rna enums #300

Closed JonathanPlasse closed 2 months ago

JonathanPlasse commented 3 months ago

Purpose of the pull request

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 commented 3 months ago

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

nutti commented 3 months ago

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.

Andrej730 commented 3 months ago

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?

JonathanPlasse commented 3 months ago

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