-
Notifications
You must be signed in to change notification settings - Fork 3
Add typing for most variables/returns #6
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
base: master
Are you sure you want to change the base?
Conversation
|
To give more context about The intention is:
Maybe these types have a common typing ancestor ? Maybe they all implement the buffer protocol (I did not check if it is the case in practice, but that would make some sense) ? Another type I know of which could make sense is |
I see 3.12 introduced In the context of an out-of-stdlib, this feels too recent to require it (both as type hint and more generally to rely on the On the context of an stdlib candidate, I guess it would be desirable though. So I feel there is a need to have two branches in this repository:
Both should try to remain as close as possible (...which in practice should be very easy, this repository is so quiet). Does this make sense ? |
|
I wanted to add a short description on the first line to this function to use PEP 257 recommendations, would you have an idea? The rest already had it or were short enough to be able to be used. def IOC(dir: int, type: int, nr: int, size) -> int:
"""
dir
One of IOC_NONE, IOC_WRITE, IOC_READ, or IOC_READ|IOC_WRITE.
Direction is from the application's point of view, not kernel's.
size (14-bits unsigned integer)
Size of the buffer passed to ioctl's "arg" argument.
""" |
While <3.12 will be here with us for 2 more years, I don't believe it's a problem, people needing older Python versions can simply use the older version of the library, it's not like the typing additions are going to matter that much to people (otherwise someone would have contributed them by now). IMO, no need to split the library in two - but again, your call |
|
Sorry for the delay.
Good idea, thanks.
Maybe something like
Good point. Let's not split the repository. |
Here is the general direction in which I intend to go: diff --git a/ioctl_opt/__init__.py b/ioctl_opt/__init__.py
index aa2006f..d234991 100644
--- a/ioctl_opt/__init__.py
+++ b/ioctl_opt/__init__.py
@@ -26,6 +26,7 @@ Common parameter meanings:
Driver-imposed ioctl function number.
"""
import array
+from collections.abc import Buffer
import ctypes
import struct
from typing import Final
@@ -69,14 +70,11 @@ def IOC(dir: int, type: int, nr: int, size) -> int:
assert size <= _IOC_SIZEMASK, size
return (dir << _IOC_DIRSHIFT) | (type << _IOC_TYPESHIFT) | (nr << _IOC_NRSHIFT) | (size << _IOC_SIZESHIFT)
-def IOC_TYPECHECK(t) -> int:
+def IOC_TYPECHECK(t: Buffer | ctypes._CData | type[ctypes._CData]) -> int:
"""Returns the size of given type, and check its suitability for use in an ioctl command number."""
- if isinstance(t, (memoryview, bytearray)):
- size = len(t)
- elif isinstance(t, struct.Struct):
- size = t.size
- elif isinstance(t, array.array):
- size = t.itemsize * len(t)
+ if isinstance(t, Buffer):
+ with memoryview(t) as t_view:
+ size = t_view.nbytes
else:
size = ctypes.sizeof(t)
assert size <= _IOC_SIZEMASK, size
diff --git a/pyproject.toml b/pyproject.toml
index 09e75bb..a1fbcba 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -6,7 +6,7 @@
authors = [{name = "Vincent Pelletier", email = "[email protected]"}]
license = { text = "GPL-2.0-or-later" }
keywords = ["ioctl"]
- requires-python = ">=3.10"
+ requires-python = ">=3.12"
classifiers = [
"Intended Audience :: Information Technology",
"License :: OSI Approved :: GNU General Public License (GPL)",Of course, it is bad to have to reach for Can this be expressed in a nicer way ? Side note: while implementing the above I realize there is a bug in the current implementation when an instance of memoryview or bytearray (or of their subclasses) is provided and the item size is not one byte. The above patch fixes this. |
|
I've rebased against master and applied the patch + suggestions. I am unfortunately not sure how I'd improve the typing further. |
I was not a 100% on typing
tandsize, so I left them alone, rest is typed now.