Better typing for enums and flags - #736
Merged
Merged
Conversation
almarklein
marked this pull request as ready for review
September 9, 2025 10:26
Member
Author
|
Relies on #737 for CI to pass. |
Member
Author
|
Ready. I have some more typing improvements I want to do, but I'll make new prs for that. |
Korijn
approved these changes
Sep 15, 2025
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #720
Context
Enums in Python are not great, and we want/need simple strings to be passed for arguments that represent an enum. We want to support both of these:
The first is the "full-enum" approach, which can actually be made to work with the builtin
enums.Enum. The latter is a lighter, and arguably more readable approach, also used by WebGPU in JS.In terms of autocompletion, we have that working for the first approach. This PR adds it also for the latter (in IDE's that are smart enough to understand
typing.Literal).Requirements
>> wgpu.PowerPreferenceand hitting enter.>> wgpu.PowerPreference.hi.(power_preference=".Implementation
Implementing this is apparently not trivial: python/typing#781
In this PR I went with the approach where for each enum and flag, we have two flavours, one for typing and one to act as the public flag/enum object.
To stick with the power-preference example:
The
PowerPreferenceEnumis only used for typing and is not public. Notice that the docstring uses the publicPowerPreference, so the Sphinx docs will make that a link to the docs for that enum.