Recommended type annotations in ops

Could someone explain me this:

I don’t understand this recommendation, which also cites PEP-526, which it basically kinda breaks at the same time in the example? The valid annotation syntax is:
var: type = value
their recommendation is:
var : bpy.props.WhateverPropert(arg1=x, arg2=y)

This isnt particularly what spec talks about and this also will plague __annotations__ with nonsense like tuple(func, {k,v}) and it will break linters and type checkers.

Quote from Jake Bailey, Pyright/Pylance author

Unless I’m mistaken, function calls like this shouldn’t be legal in type annotations. Type annotations are meant to be static only, and future releases of Python won’t evaluate them at all anymore. I don’t believe we really try and handle this.

I’m all for type annotations, just not like, err, that. Can someone explain grand logic and idea behind this?

1 Like

Some problems due to =: https://developer.blender.org/D9747

Sure, but the problem is that the annotations Blender requires are incorrect. I have no issues with annotations, I’m all for it, in fact.

I’m not sure if it was a good move to use annotations for this (can see arguments for both). As long as it “works” it’s probably unlikely to change.

Related: https://developer.blender.org/D8782.

1 Like

The problem is that it WILL have to change when next X version of python will stop evaluating them. First, it will be just bleeding-edge users reporting issues with these recommendations, then, when Blender switches to new python backend - it will affect everyone.

Please provide proper sources for this claim. Not saying that this isn’t true, but we can’t have a discussion without them.

What do you mean? You’ve linked the issue yourself, it is one of the first comings to type annotations :thinking:

https://docs.python.org/3.10/whatsnew/3.10.html

Right. The issue is actually a bit worse than I thought when I wrote https://developer.blender.org/D8782.

Here is a quick way to to see the issue in practice (without any addons):

  1. Add from __future__ import annotations as first import statement in node.py.
  2. Start Blender.
  3. See the new error message in the terminal ValueError: NODE_OT_add_search.bl_property 'node_item' not found.

Blender could probably still use annotations, it would just have to force Python to evaluate them I believe. I don’t know what’s the best solution to this yet (breaking the api in such a fundamental way has a large cost).

If you cant have proper types for annotations in any foreseeable future until you rewrite whole thing to comply with annotations usefulness - don’t use them, simple as that. Better do it old way, then have half-broken in-between, for now.

We track this problem in https://developer.blender.org/T83626 now.

1 Like