Skip to content

Better typing for enums and flags - #736

Merged
almarklein merged 7 commits into
mainfrom
enumtype
Sep 15, 2025
Merged

Better typing for enums and flags#736
almarklein merged 7 commits into
mainfrom
enumtype

Conversation

@almarklein

Copy link
Copy Markdown
Member

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:

wgpu.gpu.request_adapter_async(power_preference=wgpu.PowerPreference.high_performance)
wgpu.gpu.request_adapter_async(power_preference="high-performance")

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

  • In an interactive session, see the options by typing >> wgpu.PowerPreference and hitting enter.
  • In an interactive session, get autocompletion by typing >> wgpu.PowerPreference.hi.
  • Static type checking.
  • Static autocompletion by typing (power_preference=".
  • Generation of clear API docs.

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:

# In enums.py

PowerPreferenceEnum = Literal["low-power", "high-performance"] | str

class PowerPreference(Enum):
    low_power = "low-power"
    high_performance = "high-performance"

# In _classes.py

async def request_adapter_async(
        self,
        *,
        feature_level: str = "core",
        power_preference: enums.PowerPreferenceEnum = None,
        force_fallback_adapter: bool = False,
        canvas=None,
    ) -> GPUAdapter:
        """ ...
        Arguments:
            ...
            power_preference (PowerPreference): "high-performance" or "low-power".
            ...
        """
        ...

The PowerPreferenceEnum is only used for typing and is not public. Notice that the docstring uses the public PowerPreference, so the Sphinx docs will make that a link to the docs for that enum.

@almarklein
almarklein marked this pull request as ready for review September 9, 2025 10:26
@almarklein
almarklein requested a review from Korijn as a code owner September 9, 2025 10:26
@almarklein

Copy link
Copy Markdown
Member Author

Relies on #737 for CI to pass.

@almarklein

almarklein commented Sep 15, 2025

Copy link
Copy Markdown
Member Author

Ready. I have some more typing improvements I want to do, but I'll make new prs for that.

@almarklein
almarklein merged commit fc921ee into main Sep 15, 2025
19 checks passed
@almarklein
almarklein deleted the enumtype branch September 15, 2025 10:14
@almarklein almarklein mentioned this pull request Sep 15, 2025
2 tasks
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.

Enum Type Errors

2 participants