-
-
Notifications
You must be signed in to change notification settings - Fork 50
Description
Describe the bug
Currently, all wgpu-py
enum member arguments are marked as type errors when given to functions that expect the overall enum type.
This applies for user-defined functions, but also library functions such as device.create_buffer
, which require an enum argument.
To Reproduce
- Ensure
wgpu-py
is installed for the project - Install
pylance
with VSCode - Click the curly bracket pair at the bottom right of the status bar next to 'Python' and set the type checking mode to 'basic', 'standard', or 'strict'
- Create a python file with the following contents:
from wgpu import PowerPreference
def example_set_power_mode(preference: PowerPreference):
pass
example_set_power_mode(PowerPreference.high_performance)
Observe the following type error:
Argument of type "str" cannot be assigned to parameter "preference" of type "PowerPreference" in function "example_set_power_mode"
"str" is not assignable to "PowerPreference" (PylancereportArgumentType)
Expected behavior
I expect that I should be able to pass a PowerPreference.high_performance
into a function which expects a PowerPreference
.
Notes
It seems like all enums are being coerced into their underlying representative type by _coreutils/EnumType
upon field access, which is what is causing this type error.
While there aren't any runtime problems with the current enum type, the linting errors can be a bit distracting. In a small ~4k LOC project, I have over 40 type errors just from the enum errors. Part of me likes that about Python, because I can have that many linting errors and still run my program with no problem, but today I woke up and chose violence against these squigglies.
Disabling argument typing errors altogether is also not an option for me because it would negate a lot of the static typing benefits.
I noticed (with some help from GPT) that using the Enum
type defined by the python library, there are no such type errors that occur upon accessing a specific enum member, even when defining the underlying representation of the enum member.
from enum import Enum
class PowerPreference(Enum):
low_power = "low-power"
high_performance = "high-performance"
def example_set_power_mode(preference: PowerPreference):
pass
example_set_power_mode(PowerPreference.high_performance)
I'm thinking of a few ways this error can be mitigated:
- We could change all functions which take in an enum to take in the underlying type representation instead.
- The authors behind the webgpu type spec chose this route. Though, they alias the literal type (i.e. str or number) with a new type for more clarity. For example,
type GPUBufferUsageFlags = number;
- The authors behind the webgpu type spec chose this route. Though, they alias the literal type (i.e. str or number) with a new type for more clarity. For example,
- We could find out a way to access an enum member of
_coreutils/EnumType
without coercing the underlying type to a literal. - We could use the standard library
Enum
instead. Is there an advantage to our_coreutils/EnumType
implementation over the standard library enum that I'm not aware of?
Or maybe there's a way to silence these errors specifically without removing type-checking for all arguments that I'm not aware of?
Thanks for making wgpu-py
! I've always wanted to use numpy with a more modern API than OpenGL and this project has made it possible to do so.
Your environment
██ system:
platform: macOS-15.5-arm64-arm-64bit-Mach-O
python_implementation: CPython
python: 3.13.3
██ versions:
wgpu: 0.22.2