pygfx / pyshader

Write modern GPU shaders in Python!
BSD 2-Clause "Simplified" License
73 stars 1 forks source link

Allow altternative way to provide type info to prevent F821 linting errors #41

Closed almarklein closed 4 years ago

almarklein commented 4 years ago

Closes #40

The problem

We use annotations to specify shader type information using tuples, e.g.:

@python2shader
def vertex_shader(
    index: ("input", "VertexId", i32),
    out_pos: ("output", "Position", vec4),
    out_color: ("output", 0, vec3),
):
    ...

Unfortunately, pyflakes (which we use via flake8), reports stuff like:

F821 undefined name 'VertexId'
F821 undefined name 'output'
...

Solution in this PR

There may be (or come, e.g. PEP 593) ways to mark our type annotations to prevent pyflakes from seeing our strings as forward declarations, but these will likely make the spelling more complex.

A very simple solution (both in implementation and use) is to also allow default values to specify the shader type info. This information is as easy to obtain as the annotations. In usage, it is as simple as changing the above code to (replacing : with =):

@python2shader
def vertex_shader(
    index=("input", "VertexId", i32),
    out_pos=("output", "Position", vec4),
    out_color=("output", 0, vec3),
):
    ...