2
$\begingroup$

Here's my understanding of bpy.props.BoolProperty which offers additional functionality and integration with Blender's UI and data system.

Using bpy.props.BoolProperty allows you to specify various parameters such as the name, description, default value, and other settings that are important for properties used in Blender's interface. bpy.props module defines properties to extend Blender’s internal data. The result of these functions is used to assign properties to classes registered with Blender and can’t be used directly.

Like in this example where I define a simple addon with a panel and one checkbox. I intend to add more properties in this group:

import bpy

class MyProperties(bpy.types.PropertyGroup):
    my_prop_1: bpy.props.BoolProperty(
        name="My Checkbox",
        description="Some checkbox description",
        default=False,
    )

class OBJECT_PT_my_panel(bpy.types.Panel):
    bl_label = f"My Simple Panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = 'Some Simple Panel'

    def draw(self, context):
        layout = self.layout
        properties = context.scene.my_panel_properties

        layout.prop(properties, "my_prop_1")


def register():
    bpy.utils.register_class(MyProperties)
    bpy.types.Scene.my_panel_properties = bpy.props.PointerProperty(type=MyProperties)
    bpy.utils.register_class(OBJECT_PT_my_panel)

def unregister():
    bpy.utils.unregister_class(MyProperties)
    del bpy.types.Scene.my_panel_properties
    bpy.utils.unregister_class(OBJECT_PT_my_panel)
    
register()

I'm on VSCode using Pylance and everything was working fine with no syntax problems and then all of a sudden overnight I noticed that, even though the code still works perfectly when run with Blender python, Pylance is now marking my property as incorrect syntax with the following error:

Call expression not allowed in type expression Pylance(reportInvalidTypeForm)

enter image description here

So I thought at first this was a bug and reported it in pylance-release/issues/5455 but got a reply:

Pylance is behaving correctly here. The return value of a function cannot be used as a type annotation. Pyright, the type checker that Pylance is built on top of, has recently been working to adhere more closely to the typing spec. If this is a recent change in behavior, my guess is it is related to that work.

There's more detail on valid type expressions for type annotations here: https://github.com/python/typing/blob/main/docs/spec/annotations.rst#valid-type-expression-forms.

And this reply from another developer:

The values of type annotations are ignored at runtime. They are used for static type checking only, and there are rules about what expression forms are allowed within type annotations. You're using an expression form here that is expressly disallowed by the Python static type specification.

So I double checked the examples in the bpy.props documentation and they are now also all incorrect syntax. I assume this is now the wrong way to write my addon. How then do you rewrite the script to correctly add UI elements such as checkboxes (bpy.props.BoolProperty), value sliders (bpy.props.FloatProperty), drop down lists (bpy.props.EnumProperty), and so on?

$\endgroup$
8
  • 1
    $\begingroup$ Pyright is a static type checker for Python that primarily focuses on type checking, type inference, and static analysis. It doesn't enforce PEP8 style guidelines or perform linting in the same way that tools like flake8 or pylint do. $\endgroup$ Feb 10 at 2:28
  • 1
    $\begingroup$ Thanks for that info. Looking at the Github page, I didn't know just what it did. I don't have much experience with Python. $\endgroup$
    – curious_1
    Feb 10 at 2:57
  • 2
    $\begingroup$ I've seen this pop up recently too. Just a quirk of the API I think, I suggest to suppress the warning in the vs code settings. To answer the question, I don't think you can adhere to the spec and define Blender properties. The python conventions are very loosely followed in Blender's own python implementations in the interface and addons so I wouldn't worry too much about it. :p $\endgroup$
    – Gorgious
    Feb 10 at 14:20
  • 1
    $\begingroup$ FWIW historically if you look at pre 2.80 python code you'll see property definitions with = instead of :, I don't know exactly why they chose to switch but it must have been a very good reason because it broke many, many scripts and still to this day frequently causes questions on this site :) $\endgroup$
    – Gorgious
    Feb 10 at 14:23
  • 1
    $\begingroup$ Nope you're pretty safe writing your addon like that. At least until 5.0 comes out :p $\endgroup$
    – Gorgious
    Feb 11 at 14:18

1 Answer 1

1
$\begingroup$

As pointed out by @Gorgious:

Just a quirk of the API I think, I suggest to suppress the warning in the vs code settings. To answer the question, I don't think you can adhere to the spec and define Blender properties. The python conventions are very loosely followed in Blender's own python implementations in the interface and addons so I wouldn't worry too much about it. you're pretty safe writing your addon like that. At least until 5.0 comes out.

And another note from a Pylance developer:

The Blender API has been using this syntax for more than 4 years now. It might be "incorrect" in the general sense for typing in Python, but in the context of the Blender API, annotations are only used for this singular purpose. Add-on codes are not otherwise annotated. The main maintainers may or may not be aware of this inconsistency between the Blender API and the Python specs, but changing the API would break backwads compatibility with all existing add-ons, so my guess is that it'll stay for now. All we can do is suppressing this warning in VSCode.

You can easily suppress the warning using # type: ignore right after the declaration:

my_prop_1: bpy.props.BoolProperty(
    name="My Checkbox",
    description="Some checkbox description",
    default=False,
) # type: ignore
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .