fake-bpy-module
fake-bpy-module copied to clipboard
Fix: allow foreach_get/set to accept Buffer types
As per #161:
In .pyi files, the most recent syntax of python can be used, as it is only used for static analysis.
collections.abc.Buffer was added in Python 3.12 for typing classes that implement the buffer protocol—it is not possible to specify the contained type or mutability of the buffer.
Fixes #257
@Road-hog123
Thank you for tackling this issue.
Should we change bpy_prop_array also?
https://github.com/nutti/fake-bpy-module/blob/991583418cf2026dc1603a247b0d84f98983e17e/src/mods/common/analyzer/new/bpy.types.mod.rst?plain=1#L21
We should also change mathutils classes too (Vector, Quaternion, …)
Should we change
bpy_prop_arrayalso?
Done! 👍
We should also change
mathutilsclasses too (Vector,Quaternion, …)
They don't have foreach_get/set methods—what are you proposing to change?
- The descriptors
__set__and others related to #158 for numpy. I may have misread the description.
The descriptors
__set__and others related to #158 for numpy.
Unless __set__ makes use of the Buffer Protocol we should not annotate it as accepting such types.
Using collections.abc.Buffer will make this code Python 3.12+ while even Blender itself is currently Python 3.11.
Alternatively we can use typing_extensions.Buffer though we will need to add typing_extensions to the package dependencies. It's pretty common module for typing maintained by Python itself, so it shouln't be an issue.
Using
collections.abc.Bufferwill make this code Python 3.12+ while even Blender itself is currently Python 3.11.Alternatively we can use
typing_extensions.Bufferthough we will need to add typing_extensions to the package dependencies. It's pretty common module for typing maintained by Python itself, so it shouln't be an issue.
This is what flake8-pyi recommands too.
https://github.com/PyCQA/flake8-pyi/blob/f99a1b1dfe713706b3d413a69dd231b9cd2de676/CHANGELOG.md?plain=1#L115-L118
#161 includes a proposal to use the type Parameter Syntax from PEP 695 (which is also 3.12+) after stating...
In .pyi files, the most recent syntax of python can be used, as it is only used for static analysis.
...which I quoted in the OP. Am I misunderstanding when I take this to mean I can use types like collections.abc.Buffer in mod files? @JonathanPlasse
...which I quoted in the OP. Am I misunderstanding when I take this to mean I can use types like
collections.abc.Bufferin mod files?
E.g. in my case I have only Python 3.11 and PyRight is okay with using Python 3.12 syntax (PEP 695) in .pyi files but it will fail if I try to import something that's only available in Python 3.12 as it's still using Python 3.11 stub file for collections.abc
Btw, just noticed that Buffer doesn't really work in Python 3.11 for numpy arrays. Opened an issue in numpy - https://github.com/numpy/numpy/issues/26783
from typing_extensions import Buffer
class ClassA:
def accept_buffer2(self, buffer: Buffer):
pass
a = ClassA()
from array import array
import numpy as np
array_buffer = array("l", [1, 2, 3, 4, 5])
np_buffer = np.array([1, 2, 3, 4, 5], dtype=np.int32)
print(memoryview(array_buffer))
print(memoryview(np_buffer))
a.accept_buffer2(array_buffer)
# Argument of type "NDArray[signedinteger[_32Bit]]" cannot be assigned to parameter "buffer" of type "Buffer" in function "accept_buffer2"
# "ndarray[Any, dtype[signedinteger[_32Bit]]]" is incompatible with protocol "Buffer"
# "__buffer__" is not present
a.accept_buffer2(np_buffer)
#161 includes a proposal to use the
typeParameter Syntax from PEP 695 (which is also 3.12+) after stating...In .pyi files, the most recent syntax of python can be used, as it is only used for static analysis.
...which I quoted in the OP. Am I misunderstanding when I take this to mean I can use types like
collections.abc.Bufferin mod files? @JonathanPlasse
You could but it would be less correct, also typing_extensions.Buffer is what typeshed use which is what we should follow.
Thank you @Road-hog123