Type hints for pyglet.gl.gl* functions (pyglet#1237)#1325
Type hints for pyglet.gl.gl* functions (pyglet#1237)#1325benmoran56 merged 7 commits intopyglet:masterfrom
Conversation
|
The type hints for pointers (e.g., |
|
Thanks for this PR! This is helpful for users. However, one thing I would do is instead of making this a double function call (function in a function), which has a runtime overhead cost, there are a couple solutions that won't affect runtime:
def glClearColor(red: GLfloat | float, green: GLfloat | float, blue: GLfloat | float, alpha: GLfloat | float) -> None:
...I think the stub file is more clean in my opinion, and IDE's will lookup the stub file signature instead . |
|
I think adding the type annotations in a stub file is a good idea. |
caffeinepills
left a comment
There was a problem hiding this comment.
Overall looks good, just a couple of the star imports. Probably related to the old script.
|
This looks good, thanks @Ross-TheBoss! Question though: is it necessary to also include all of the GL constants in the stub files? |
Stub files override all inspections on my IDE, if you don't have them, it will act as if the variables don't exist. |
Including the GL constants in the stub files is necessary but I currently also define the constants' values which is not necessary. Originally, I liked that I could see the value assigned to a constant in Pycharm's tooltip, but the convention seems to be not to include this information in the stub file. |
|
OK that explains it. I assumed there was a reason to include them. One last thing I just noticed: shouldn't we alias GLbyte to c_byte instead of c_char? GLbyte = c_char
...
GLchar = c_charben@hammer ~ $ cat /usr/include/GL/gl.h | grep GLbyte
typedef signed char GLbyte; /* 1-byte signed */ |
I didn't notice that as I wasn't making changes to that part of the code. We should probably alias GLbyte to c_byte instead as that's more specific and less implementation dependent. Using c_char works but only coincidentally as whether a GLbyte is supposed to be specifically a signed 8-bit integer: OpenGL types |
|
OK. I have made that change now. |
|
I guess GLbyte is not commonly used in OpenGL to begin with, which is probably why nobody noticed 😅 |
As suggested in #1237, this modifies
gengl.pyto add a type annotated function for each gl command which replaces it as the new publicly accessible way of interacting with OpenGL.The annotations are determined from the
argtypes(argument types variable) andrestype(return type variable) given for each command in thewrite_commandsfunction.I also broadened the allowable parameters to include the python type that can be translated into the ctypes type. E.g., A python
intcan be used for a parameter acceptingGLenum,GLboolean,GLbitfield,GLubyte, etc. This is done according to the "Fundamental data types" table in the ctypes documentation.Sample type hint:
