8000 Fix: allow foreach_get/set to accept Buffer types by Road-hog123 · Pull Request #260 · nutti/fake-bpy-module · GitHub
[go: up one dir, main page]

Skip to content

Fix: allow foreach_get/set to accept Buffer types #260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 5, 2024

Conversation

Road-hog123
Copy link
Contributor

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

@nutti
Copy link
Owner
nutti commented Jun 20, 2024

@Road-hog123

Thank you for tackling this issue.
Should we change bpy_prop_array also?

@JonathanPlasse
Copy link
Contributor

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

@Road-hog123
Copy link
Contributor Author

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?

@JonathanPlasse
Copy link
Contributor
JonathanPlasse commented Jun 20, 2024

@Road-hog123
Copy link
Contributor Author

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.

@Andrej730
Copy link
Contributor

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.

@JonathanPlasse
Copy link
Contributor

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

@Road-hog123
Copy link
Contributor Author

#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

@Andrej730
Copy link
Contributor

...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 - numpy/numpy#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)

@JonathanPlasse
Copy link
Contributor

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

@Road-hog123 Road-hog123 force-pushed the foreach-buffer branch 5 times, most recently from 2678785 to 250f4c4 Compare June 23, 2024 03:30
[4.7.0](https://github.com/python/typing_extensions/releases/tag/4.7.0)
is the minimum version for Python 3.12 features; semantic versioning
means the backwards-incompatible changes will only be made in the next
major version (5.0.0).
@nutti nutti merged commit fb2bfe5 into nutti:master Jul 5, 2024
33 checks passed
@nutti
Copy link
Owner
nutti commented Jul 5, 2024

Thank you @Road-hog123

@Road-hog123 Road-hog123 deleted the foreach-buffer branch July 5, 2024 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

foreach_get and foreach_set doesn't support numpy buffers
4 participants
0