Using the name class as type variable


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?

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

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


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?


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