fake-bpy-module
fake-bpy-module copied to clipboard
Update: Create type aliases for rna enums
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.
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.
Should I create a new node to use the PEP 695 type syntax instead?
Should I create a new node to use the PEP 695
typesyntax instead?
Could you show me the mock generated code? To solve #161, it seems necessary to do this.
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?
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.