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

`draw_handler_add` - false negatives on passing `None` values

Open Andrej730 opened this issue 6 months ago • 7 comments

Example snippet:

import bpy

def draw_callback(*args, **kwargs):
    print("Draw callback called with:")
    print("  args:", args)
    print("  kwargs:", kwargs)

# TypeError: Space.draw_handler_add() argument 3 must be tuple, not None
handler = bpy.types.SpaceView3D.draw_handler_add(
    draw_callback,
    None,
    'WINDOW',
    'POST_PIXEL'
)

It has no typing issues but results in an error on runtime: TypeError: Space.draw_handler_add() argument 3 must be tuple, not None. False negative is caused by | None present in argument type.

Image

Andrej730 avatar May 13 '25 08:05 Andrej730

@Andrej730 I could not understand the root cause of this issue. Is it possible from our side to fix this?

nutti avatar May 13 '25 16:05 nutti

I don't know, I haven't investigated where optionality is coming from exactly - can't pinpoint whether it's fake-bpy issue or something's wrong with blender docs also.

But from the docs it seems there's nothing that suggest that those arguments are optional:

https://docs.blender.org/api/current/bpy.types.SpaceView3D.html

Image

Andrej730 avatar May 13 '25 18:05 Andrej730

@Andrej730 Do you have expected typing? If so, we can handle this issue. If not, it is difficult to fix from our side.

nutti avatar Jun 05 '25 15:06 nutti

Sure, overall argument types are correct, but there shouldn't be | None for any of those arguments.

Here are runtime tests:

  1. Passing None as callback results in TypeError: first argument isn't callable
  2. Passing None as args - Space.draw_handler_add() argument 3 must be tuple, not None
  3. Passing None as region_type - TypeError: expected a string enum, not NoneType
  4. Passing None as draw_type - TypeError: expected a string enum, not NoneType.

Andrej730 avatar Jun 05 '25 15:06 Andrej730

Thank you. So, is removing | None the solution?

nutti avatar Jun 05 '25 15:06 nutti

Indeed. Just uncertain about the cause.

Andrej730 avatar Jun 05 '25 16:06 Andrej730

Raised PR to fix this. https://projects.blender.org/blender/blender/pulls/139946

nutti avatar Jun 06 '25 16:06 nutti

Blender patch is rejected. Will handle this by changing the code.

nutti avatar Nov 01 '25 14:11 nutti