示例#1
0
        def _get_fields_uncached():
            dataclass_bases = [
                klass for klass in cls.__bases__ if is_dataclass(klass) and issubclass(klass, JsonSchemaMixin)
            ]
            base_fields_types = set()
            for base in dataclass_bases:
                base_fields_types |= {(f.name, f.type) for f in fields(base)}

            mapped_fields = []
            type_hints = get_type_hints(cls)
            for f in fields(cls):
                # Skip internal fields
                if f.name.startswith("__") or (not base_fields and (f.name, f.type) in base_fields_types):
                    continue
                # Note fields() doesn't resolve forward refs
                f.type = type_hints[f.name]
                mapped_fields.append(JsonSchemaField(f, cls.field_mapping().get(f.name, f.name)))

            if cls.__serialise_properties:
                include_properties = None
                if isinstance(cls.__serialise_properties, tuple):
                    include_properties = set(cls.__serialise_properties)

                members = inspect.getmembers(cls, inspect.isdatadescriptor)
                for name, member in members:
                    if name != "__weakref__" and (include_properties is None or name in include_properties):
                        f = Field(MISSING, None, None, None, None, None, None)
                        f.name = name
                        f.type = member.fget.__annotations__['return']
                        mapped_fields.append(JsonSchemaField(f, name, is_property=True))

            return mapped_fields
示例#2
0
def field(*, field=None, callback=None, default=MISSING, default_factory=MISSING, init=True, repr=True,
          hash=None, compare=True, metadata=None):
    if default is not MISSING and default_factory is not MISSING:
        raise ValueError('cannot specify both default and default_factory')
    if field is not None or callback is not None:
        if callback is not None:
            if field is not None:
                raise ValueError('cannot specify both field and callback')
            if not callable(callback):
                raise ValueError('callback must be callable')
        return RowField(field, callback, default, default_factory, init, repr, hash, compare, metadata)
    else:
        return Field(default, default_factory, init, repr, hash, compare, metadata)
示例#3
0
from dataclasses import dataclass,Field

@dataclass
class Employee:
    name: str
    age: int
    gender: str


if __name__ == '__main__':
    e1 = Employee('Sasi',30,'M')
    e2 = Employee('Ramu',35,'M')

    print(e1.__dataclass_fields__)
    my_field = Field('city',str)
    print(my_field)
示例#4
0
class President():

    first_name: Field(str)
    last_name: str
    birthdate: date