RNA enum properties support

This commit is contained in:
2026-09-21 11:18:10 +02:00
parent f575a84c13
commit f2efa629ba
8 changed files with 134 additions and 8 deletions
+36 -1
View File
@@ -1555,7 +1555,7 @@ def _property_data_from_member(
is_readonly = raw.fset is None
else:
is_readonly = not _is_getset_writable(cls, name)
prop_type = rtype or "object"
prop_type = rna_enum_literal_type(cls, name) or rtype or "object"
result: PropertyData = {
"name": name,
"type": prop_type,
@@ -1567,6 +1567,41 @@ def _property_data_from_member(
return result
def rna_enum_literal_type(cls: type[object], name: str) -> str | None:
"""Return a Literal annotation for an RNA enum property, when available.
RNA metadata is the authoritative source for enum identifiers. Blender's
Python docstrings do not consistently include the enum items for class
properties, so relying on the docstring can leave properties such as
``Panel.bl_space_type`` typed as plain ``str``.
"""
rna = getattr(cls, "bl_rna", None)
if rna is None:
return None
properties = getattr(rna, "properties", None)
if properties is None:
return None
try:
prop = properties[name]
except (KeyError, IndexError, TypeError):
return None
if getattr(prop, "type", None) != "ENUM":
return None
values = [
str(getattr(item, "identifier", "")) for item in getattr(prop, "enum_items", [])
]
values = [value for value in values if value]
if not values:
return None
quoted = ", ".join(f'"{value}"' for value in values)
return f"Literal[{quoted}]"
def _introspect_declared_class_members(
cls: type[object],
) -> tuple[list[PropertyData], list[FunctionData]]: