Using the name class as type variable


#1

Im implementing an API based upon one of our customers specifications and i do require to set one of the fields to be named as “class”. Since the class variable is protected this is an issue. Ive tried to use the title=“class” in the validator but the validations still complains about that the renamed “_class” is missing and required.

Any ideas how to achieve this?

Wanted:
class A(types.Type):
    class = validators.Number()

What i've tried
class A(types.Type):
    _class = validators.Number(title="class")

#2

Here’s a workaround:

from apistar import types, validators

A = types.TypeMetaclass('A', (types.Type,), {'class': validators.Number()})

We use the metaclass provided in APIStar to construct the type class we need with the reserved keyword class.

@tom is there a cleaner way?


#3

Note that this is also important for kabab-case variables such as g-repatcha-response