Skip to content

Type hints for pyglet.gl.gl* functions (pyglet#1237)#1325

Merged
benmoran56 merged 7 commits intopyglet:masterfrom
Ross-TheBoss:master
Jun 3, 2025
Merged

Type hints for pyglet.gl.gl* functions (pyglet#1237)#1325
benmoran56 merged 7 commits intopyglet:masterfrom
Ross-TheBoss:master

Conversation

@Ross-TheBoss
Copy link
Copy Markdown
Contributor

As suggested in #1237, this modifies gengl.py to 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) and restype (return type variable) given for each command in the write_commands function.

I also broadened the allowable parameters to include the python type that can be translated into the ctypes type. E.g., A python int can be used for a parameter accepting GLenum, GLboolean, GLbitfield, GLubyte, etc. This is done according to the "Fundamental data types" table in the ctypes documentation.

Sample type hint:
image

@Ross-TheBoss
Copy link
Copy Markdown
Contributor Author

The type hints for pointers (e.g., POINTER(GLenum)) might not be correct.

@caffeinepills
Copy link
Copy Markdown
Collaborator

caffeinepills commented May 29, 2025

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:

  1. glClearColor: Callable[[None], GLfloat | float, GLfloat | float, GLfloat | float, GLfloat | float] = _link_function('glClearColor', None, [GLfloat, GLfloat, GLfloat, GLfloat], requires='OpenGL 1.0')
    While this works, it doesn't allow any argument names.

  2. Stub file, (in this case) pyglet/gl/gl.pyi
    You would define them like this:

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 .

@Ross-TheBoss
Copy link
Copy Markdown
Contributor Author

I think adding the type annotations in a stub file is a good idea.

Copy link
Copy Markdown
Collaborator

@caffeinepills caffeinepills left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good, just a couple of the star imports. Probably related to the old script.

@benmoran56
Copy link
Copy Markdown
Member

This looks good, thanks @Ross-TheBoss!

Question though: is it necessary to also include all of the GL constants in the stub files?

@caffeinepills
Copy link
Copy Markdown
Collaborator

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.

@Ross-TheBoss
Copy link
Copy Markdown
Contributor Author

This looks good, thanks @Ross-TheBoss!

Question though: is it necessary to also include all of the GL constants in the stub files?

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.

@benmoran56
Copy link
Copy Markdown
Member

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_char
ben@hammer ~ $ cat /usr/include/GL/gl.h | grep GLbyte
typedef signed char	GLbyte;		/* 1-byte signed */

@Ross-TheBoss
Copy link
Copy Markdown
Contributor Author

GLbyte

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 char is signed or unsigned depends on the platform. c_byte however is a signed char in C.

GLbyte is supposed to be specifically a signed 8-bit integer: OpenGL types

@Ross-TheBoss
Copy link
Copy Markdown
Contributor Author

OK. I have made that change now.

Copy link
Copy Markdown
Member

@benmoran56 benmoran56 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@benmoran56 benmoran56 merged commit 65c4f3c into pyglet:master Jun 3, 2025
16 checks passed
@benmoran56
Copy link
Copy Markdown
Member

I guess GLbyte is not commonly used in OpenGL to begin with, which is probably why nobody noticed 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants