Starting with Python 3.10, type annotation evaluation is postponed until requested explicitly using e.g. typing.get_type_hints(). This is a problem, because we make great use of annotations in Python scripts and Blender expects them to be evaluated.
The issue can be reproduced in the Python version we use currently with a __future__ import.
- Add from __future__ import annotations as first import statement in release/scripts/startup/bl_operators/wm.py.
- Start Blender.
- The terminal output contains many errors, because the properties of registered classes haven't been created.
Further Info:
- Python 3.10 release notes: https://docs.python.org/3.10/whatsnew/3.10.html
- PEP 619 - Python 3.10 release schedule: https://www.python.org/dev/peps/pep-0619/
- PEP 563 - Postponed evaluation of annotations: https://www.python.org/dev/peps/pep-0563/
- Related Pylance report: https://github.com/microsoft/pylance-release/issues/708
One solution is obviously not to use annotations for that use case anymore. However, given that we just forced everyone to update their scripts to use annotations, it's probably unlikely that we want to reverse that change. It's still an option though.
A solution that does not require an api breakage could work as follows:
- Finish D8782, so that typing.get_type_hints() actually works with our types.
- Use typing.get_type_hints() in bpy.utils.register_class().
This strategy seems to work in my improvised test in P1841. This is a diff that combines D8782 and some changes in wm.py. See that it still works although from __future__ import annotations is added in wm.py.
Overall, it is not clear to me, whether this is a correct use case for annotations, since it also doesn't work with static type checkers and autocomplete very well. What was the original motivation for using annotations in this context?