Proposal for type hint use in Blender's Python scripts #87333
Labels
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: blender/blender#87333
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This proposal suggests how we could include type hints in Blender's Python code (based on a discussion with @mont29 regarding how & where we might use type hints).
Motivation
Adding type information gives us some of the benefits of a statically typed language:
While these can be useful, there are some down sites to type hints too:
Current Status
Type hints aren't used in Blender's core Python scripts.
Type hints are used in some utility scripts (
./build_files/
,./release/steam
),Created #87409 to keep track of scripts that can be used with mypy.
Proposal
Move to type hints on a per-module basis.
Blender modules must go via patch review, so we can ensure this is being applied consistently.
Stand-alone scripts can be committed directly (such as stand-alone Python scripts in
source/tools
).Only add type hints when they can be validated (where
mypy
can run on the file without problems).mypy --strict
)Proposed Steps
Any further work requires a way to run
mypy
with scripts that usebpy
and other Blender modules.This needs some investigation.
Blender modules can then move to using type hints where appropriate.
We can then investigate if it's worth expanding type hint use to other areas areas, or leave as-is.
Notes
mypy
is one of the main tasks to support type hints.Changed status from 'Needs Triage' to: 'Confirmed'
Added subscribers: @mont29, @ideasman42
For the records, proposal LGTM... Personnaly I would be very conservative about what module we switch to type hinting though.
No issues with being conservative, especially for startup/core-scripts, although this isn't likely to be an issue until using type hints with Blender's C/API modules are supported.
Created #87409 to keep track of scripts that can be used with mypy.
Added subscriber: @dr.sybren
This is not strictly true -- there are no hard guarantees, at least not at runtime.
I've hardly run into such situations in practice. There have been cases where I thought things were clear enough without annotations, but often I was later glad that I added them anyway. For example, some operator execute function that ends with
return some_other_function()
where that other function gets refactored to return something other than{'FINISHED'}
. Yes, it is super clear to use that an operator execute function returnsSet[str]
, so it could be argued that adding such an annotation is noise. However, removing that annotation would have made it impossible for mypy to detect the just-decribed problem.I see this as a very minor barrier. More locally-understandable, better described code means that it's easier to contribute to that code. Having more ways to do automated checks on that code also makes it easier to catch problems early. Both contribute to a more pleasant experience submitting patches. Of course these developers need to know how to use type annotations, and that does present a bit of a barrier, but IMO compared to learning how to script for Blender in the first place this is also minor.
In the past years I've used this
mypy.ini
to check add-on code, and it has served me quite well. It allows us to use mypy without having to wait for complete stubs ofbpy
, while still staying quite strict in other areas.Of course these stubs will be super useful, and will make the automated type checking much stronger. I don't think we have to wait with type annotations until we have those stubs, though.
About being conservative, do we even have timing information about the actual cost of adding type annotations to startup scripts? And is this based on Python 3.9, which is faster to handle such things than 3.7? If not, I strongly object against the "be conservative", as it's not based on actual knowledge, but does stand in the way of more understandable code.
I tend to agree with you here, nevertheless, some scripts are very small, and realistically they aren't going to be a maintenance problem either way AFAICS.
The barrier of entry IMHO is more that new developers have to become familier with new tools that have stumbling blocks we can't avoid entirely.
mypy
and get errors, it takes them time to figure out this is the problem.All of these scenarios can be quite demotivating, even if newer developers manage to overcome them, it's just a cost to taking on type hinting, not a reason to reject it. Even so, if there is minimal gain in an area, we could leave it out for now. Areas that have much more to gain can be worked on first & we can re-evaluate once they're done.
I'd like to have the stubs before moving bpy and other modules to use typing, it shouldn't be so hard to generate them.
Probably the cost isn't prohibitive, although for switching startup modules - it's reasonable the changes are proven not to cause significant issues. I didn't think @mont29's comment about being conservative was with regard to performance though.
Added subscriber: @grzelins
Just wanted to mention that I made a deep dive into this issue in this thread: Proposal: Generate stub files for blender back in 2020. There are many attempts by community to generate stub files, there are links in this thread including:
but it can not be simply done with addon, an ingeration to source code is required and it is quite hard to get it through.
When writting this post I am experimenting with LSP and
mypy
plugin + VS code extension.Writing stub generator proved to be
:type param:
annotation to enable parsing (and parsing doc strings in often not supported in type checkers)bpy.types
file - this is slowI wrote one, so I know. LSP might be the way to go, we will see.
BTW. writing stub generator is very similar to writing API documentation generator (currently we use sphinx), so there is that.