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
def _serialize_field(field_: Field): """ :param field_: :return: """ if isinstance(field_, Base): return field_.id_ if isinstance(field_, datetime): return field_.strftime("%Y-%m-%d %H:%M:%S") if isinstance(field_, date): return field_.strftime("%Y-%m-%d") return field_
def _serialize_field(field_: Field): """ :param field_: :return: """ if issubclass(field_.__class__, Entity): return field_.id_ if isinstance(field_, datetime): return field_.strftime("%Y-%m-%d %H:%M:%S") if isinstance(field_, date): return field_.strftime("%Y-%m-%d") return field_
def get_default(field: Field, schema: Schema[T]) -> Any: if not schema.omit_default: return MISSING # type ignore because of https://github.com/python/mypy/issues/6910 if field.default_factory != MISSING: # type: ignore return field.default_factory() # type: ignore return field.default
def _get_field_meta(cls, field: Field, schema_type: SchemaType) -> Tuple[FieldMeta, bool]: required = True field_meta = FieldMeta(schema_type=schema_type) default_value = MISSING if field.default is not MISSING: # In case of default value given default_value = field.default elif field.default_factory is not MISSING and field.default_factory is not None: # type: ignore # In case of a default factory given, we call it default_value = field.default_factory() # type: ignore if default_value is not MISSING: field_meta.default = cls._encode_field(field.type, default_value, omit_none=False) required = False if field.metadata is not None: if "examples" in field.metadata: field_meta.examples = [ cls._encode_field(field.type, example, omit_none=False) for example in field.metadata["examples"] ] if "extensions" in field.metadata: field_meta.extensions = field.metadata["extensions"] if "description" in field.metadata: field_meta.description = field.metadata["description"] if "title" in field.metadata: field_meta.title = field.metadata["title"] if schema_type == SchemaType.OPENAPI_3: field_meta.read_only = field.metadata.get("read_only") if field_meta.read_only and default_value is MISSING: warnings.warn(f"Read-only fields should have a default value") field_meta.write_only = field.metadata.get("write_only") return field_meta, required
def _get_field_meta(cls, field: Field, schema_type: SchemaType) -> Tuple[FieldMeta, bool]: required = True field_meta = FieldMeta() default_value = None if field.default is not MISSING and field.default is not None: # In case of default value given default_value = field.default elif field.default_factory is not MISSING and field.default_factory is not None: # type: ignore # In case of a default factory given, we call it default_value = field.default_factory() # type: ignore if default_value is not None: field_meta.default = cls._encode_field(field.type, default_value, omit_none=False) required = False if field.metadata is not None: if "description" in field.metadata: field_meta.description = field.metadata["description"] if schema_type == SchemaType.OPENAPI_3: field_meta.read_only = field.metadata.get("read_only") if field_meta.read_only and default_value is None: raise ValueError( f"Read-only fields must have a default value") field_meta.write_only = field.metadata.get("write_only") return field_meta, required
def get_dataclass_default(field: Field, omit_default: Optional[bool]) -> Any: if not omit_default: return MISSING # type ignore because of https://github.com/python/mypy/issues/6910 if field.default_factory != MISSING: # type: ignore return field.default_factory() # type: ignore return field.default
def name(name: str, field: Field = None): if field is None: field = fld() meta = dict(getattr(field, "metadata", _EMPTY_METADATA)) meta[Mods.name] = {"from": name} field.metadata = types.MappingProxyType(meta) return field
def get_default_value_for_field(field: Field) -> Any: if field.default != MISSING: return field.default elif field.default_factory != MISSING: # type: ignore return field.default_factory() # type: ignore elif is_optional(field.type): return None raise DefaultValueNotFoundError()
def _get_default_value_for_field(field: Field) -> Any: if field.default != MISSING: return field.default elif field.default_factory != MISSING: return field.default_factory() elif _is_optional(field.type): return None else: raise MissingValueError(field)
def _get_dataclass_field_default(field: Field) -> Any: if field.default_factory is not MISSING: # pyre-fixme[29]: `Union[dataclasses._MISSING_TYPE, # dataclasses._DefaultFactory[typing.Any]]` is not a function. return field.default_factory() elif field.default is not MISSING: return field.default else: return None
def _load_enum(obj, f: Field, prefix: str) -> None: enum_annotations = f.type.__dict__.get('__annotations__', {}) name = f'{prefix.upper()}_{f.name.upper()}' for n, nested_type in enum_annotations.items(): try: setattr(obj, f.name, f.type(nested_type(os.environ[name]))) return except ValueError: continue
def deserialize_field( field: dataclasses.Field, serialized_value: Union[dict, PrimitiveType, CollectionOfPrimitiveType]): if issubclass(field.type, Serializable): return field.type.from_dict(dct=serialized_value, streets_map=streets_map) if issubclass(field.type, Junction): assert streets_map is not None return streets_map[int(serialized_value)] if issubclass(field.type, (list, tuple)): assert all( isinstance(val, (str, int, float)) for val in serialized_value) return field.type(serialized_value) assert any( issubclass(field.type, primitive_type) for primitive_type in (str, int, float)) return field.type(serialized_value)
def deserialize_field(field: dataclasses.Field, serialized_value: str): if issubclass(field.type, Serializable): return field.type.deserialize(serialized=serialized_value, streets_map=streets_map) if issubclass(field.type, Junction): assert streets_map is not None return streets_map[int(serialized_value)] assert any( issubclass(field.type, primitive_type) for primitive_type in (str, int, float)) return field.type(serialized_value)
def _convert_dataclass_field(value, field: dataclasses.Field): if value is not dataclasses.MISSING: return convert(value, field.type) if field.default is not dataclasses.MISSING: return field.default if field.default_factory is not dataclasses.MISSING: return field.default_factory() elif is_optional(field.type): return None else: raise MissingException
def _get_field_default(field: dataclasses.Field): """ Return a marshmallow default value given a dataclass default value >>> _get_field_default(dataclasses.field()) <marshmallow.missing> """ if field.default_factory is not dataclasses.MISSING: return field.default_factory() elif field.default is dataclasses.MISSING: return marshmallow.missing return field.default
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)
def _default_value(x: Field): """Return the default value of the input Field. Args: x (Field): input Field. Returns: object: default value of the input Field. """ if x.default not in (MISSING, _MISSING): return x.default if x.default_factory not in (MISSING, _MISSING): return x.default_factory() return x.default
def auto_conversion(obj, field: dataclasses.Field): """ Tries to convert the field to the right type Raises a TypeError if conversion fails. obj -- The obj the field belongs to field -- The field to check """ field_val = getattr(obj, field.name) if not field_val is None and not isinstance(field_val, field.type): try: setattr(obj, field.name, field.type(field_val)) except TypeError as error: error_handling_auto_conversion(obj, field, error)
def _add_type_list(group: argparse._ArgumentGroup, field: dataclasses.Field, name: str): group.add_argument( f"--{name}_{field.name}", type=typing.get_args(field.type)[0], default=None if field.default_factory is dataclasses.MISSING # type: ignore else field.default_factory(), # type: ignore required=field.default_factory is dataclasses.MISSING, # type: ignore choices=field.metadata.get( # type: ignore Options.POSSIBLE_VALUES, Options.POSSIBLE_VALUES.value), help=field.metadata.get(Options.HELP_TEXT, Options.HELP_TEXT.value), # type: ignore nargs="*", )
def _get_value_for_field(field: Field, data: Data, config: Config) -> Any: try: if field.name in config.prefixed: return _extract_nested_dict_for_prefix(config.prefixed[field.name], data) elif field.name in config.flattened: return _extract_flattened_fields(field, data, config.remap) else: key_name = config.remap.get(field.name, field.name) return data[key_name] except KeyError: if _is_optional(field.type): return None elif field.default != MISSING: return field.default elif field.default_factory != MISSING: return field.default_factory() else: raise MissingValueError(field)
def _get_default(catch_all_field: Field) -> Any: # access to the default factory currently causes # a false-positive mypy error (16. Dec 2019): # https://github.com/python/mypy/issues/6910 # noinspection PyProtectedMember has_default = not isinstance(catch_all_field.default, dataclasses._MISSING_TYPE) # noinspection PyProtectedMember has_default_factory = not isinstance(catch_all_field.default_factory, # type: ignore dataclasses._MISSING_TYPE) default_value = _CatchAllUndefinedParameters._SentinelNoDefault if has_default: default_value = catch_all_field.default elif has_default_factory: # This might be unwanted if the default factory constructs # something expensive, # because we have to construct it again just for this test default_value = catch_all_field.default_factory() # type: ignore return default_value
def _get_field_meta(cls, field: Field, schema_type: SchemaType) -> Tuple[FieldMeta, bool]: required = True field_meta = FieldMeta() default_value = None if field.default is not MISSING and field.default is not None: # In case of default value given default_value = field.default elif field.default_factory is not MISSING and field.default_factory is not None: # type: ignore # In case of a default factory given, we call it default_value = field.default_factory() # type: ignore if default_value is not None: field_meta.default = cls._encode_field(field.type, default_value, omit_none=False) required = False if field.metadata is not None: if "description" in field.metadata: field_meta.description = field.metadata["description"] return field_meta, required
def _parse_dataclass_field(parser: ArgumentParser, field: dataclasses.Field): field_name = f"--{field.name}" kwargs = field.metadata.copy() # field.metadata is not used at all by Data Classes, # it is provided as a third-party extension mechanism. if isinstance(field.type, str): raise RuntimeError( "Unresolved type detected, which should have been done with the help of " "`typing.get_type_hints` method by default") origin_type = getattr(field.type, "__origin__", field.type) if origin_type is Union: if len(field.type.__args__) != 2 or type( None) not in field.type.__args__: raise ValueError( "Only `Union[X, NoneType]` (i.e., `Optional[X]`) is allowed for `Union`" ) if bool not in field.type.__args__: # filter `NoneType` in Union (except for `Union[bool, NoneType]`) field.type = (field.type.__args__[0] if isinstance( None, field.type.__args__[1]) else field.type.__args__[1]) origin_type = getattr(field.type, "__origin__", field.type) # A variable to store kwargs for a boolean field, if needed # so that we can init a `no_*` complement argument (see below) bool_kwargs = {} if isinstance(field.type, type) and issubclass(field.type, Enum): kwargs["choices"] = [x.value for x in field.type] kwargs["type"] = type(kwargs["choices"][0]) if field.default is not dataclasses.MISSING: kwargs["default"] = field.default else: kwargs["required"] = True elif field.type is bool or field.type is Optional[bool]: # Copy the currect kwargs to use to instantiate a `no_*` complement argument below. # We do not initialize it here because the `no_*` alternative must be instantiated after the real argument bool_kwargs = copy(kwargs) # Hack because type=bool in argparse does not behave as we want. kwargs["type"] = string_to_bool if field.type is bool or (field.default is not None and field.default is not dataclasses.MISSING): # Default value is False if we have no default when of type bool. default = False if field.default is dataclasses.MISSING else field.default # This is the value that will get picked if we don't include --field_name in any way kwargs["default"] = default # This tells argparse we accept 0 or 1 value after --field_name kwargs["nargs"] = "?" # This is the value that will get picked if we do --field_name (without value) kwargs["const"] = True elif isclass(origin_type) and issubclass(origin_type, list): kwargs["type"] = field.type.__args__[0] kwargs["nargs"] = "+" if field.default_factory is not dataclasses.MISSING: kwargs["default"] = field.default_factory() elif field.default is dataclasses.MISSING: kwargs["required"] = True else: kwargs["type"] = field.type if field.default is not dataclasses.MISSING: kwargs["default"] = field.default elif field.default_factory is not dataclasses.MISSING: kwargs["default"] = field.default_factory() else: kwargs["required"] = True parser.add_argument(field_name, **kwargs) # Add a complement `no_*` argument for a boolean field AFTER the initial field has already been added. # Order is important for arguments with the same destination! # We use a copy of earlier kwargs because the original kwargs have changed a lot before reaching down # here and we do not need those changes/additional keys. if field.default is True and (field.type is bool or field.type is Optional[bool]): bool_kwargs["default"] = False parser.add_argument(f"--no_{field.name}", action="store_false", dest=field.name, **bool_kwargs)
def default_field_value(f: Field) -> Any: if callable(f.default_factory): # type: ignore return f.default_factory() # type: ignore else: return f.default
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)
def get_default(field: Field) -> Any: if field.default_factory is not MISSING: value = field.default_factory() else: value = field.default return value if value is not MISSING else None
def get_dataclass_default(field: Field) -> Any: # type ignore because of https://github.com/python/mypy/issues/6910 if field.default_factory != MISSING: # type: ignore return field.default_factory() # type: ignore return field.default
def has_default(self, field: dataclasses.Field) -> FieldDefault: if field.default != dataclasses.MISSING: return FieldDefault(True, field.default) if field.default_factory != dataclasses.MISSING: return FieldDefault(True, field.default_factory()) return FieldDefault()
class President(): first_name: Field(str) last_name: str birthdate: date