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

Fix: allow foreach_get/set to accept Buffer types

Open Road-hog123 opened this issue 1 year ago • 10 comments
trafficstars

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 avatar Jun 19 '24 19:06 Road-hog123

@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

nutti avatar Jun 20 '24 07:06 nutti

We should also change mathutils classes too (Vector, Quaternion, …)

JonathanPlasse avatar Jun 20 '24 07:06 JonathanPlasse

Should we change bpy_prop_array also?

Done! 👍

We should also change mathutils classes too (Vector, Quaternion, …)

They don't have foreach_get/set methods—what are you proposing to change?

Road-hog123 avatar Jun 20 '24 17:06 Road-hog123

  • The descriptors __set__ and others related to #158 for numpy. I may have misread the description.

JonathanPlasse avatar Jun 20 '24 18:06 JonathanPlasse

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.

Road-hog123 avatar Jun 20 '24 20:06 Road-hog123

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.

Andrej730 avatar Jun 21 '24 09:06 Andrej730

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.

This is what flake8-pyi recommands too.

https://github.com/PyCQA/flake8-pyi/blob/f99a1b1dfe713706b3d413a69dd231b9cd2de676/CHANGELOG.md?plain=1#L115-L118

JonathanPlasse avatar Jun 21 '24 09:06 JonathanPlasse

#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

Road-hog123 avatar Jun 22 '24 04:06 Road-hog123

...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?

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

image

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)

Andrej730 avatar Jun 22 '24 05:06 Andrej730

#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

You could but it would be less correct, also typing_extensions.Buffer is what typeshed use which is what we should follow.

JonathanPlasse avatar Jun 22 '24 07:06 JonathanPlasse

Thank you @Road-hog123

nutti avatar Jul 05 '24 11:07 nutti