本文整理匯總了Python中dataclasses.MISSING屬性的典型用法代碼示例。如果您正苦於以下問題:Python dataclasses.MISSING屬性的具體用法?Python dataclasses.MISSING怎麽用?Python dataclasses.MISSING使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類dataclasses
的用法示例。
在下文中一共展示了dataclasses.MISSING屬性的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _contains_non_default_init_vars
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def _contains_non_default_init_vars(cls, previous_classes=None):
"""Check whether this dataclass contains non-default init-only vars.
Performs a recursive check through all fields that are declared as
dataclasses to ensure that no nested dataclasses contain init-only
variables. The ``previous_classes`` argument is a set of previously
checked classes to prevent infinite recursion on recursive structures.
:param previous_classes: The set of previously checked classes.
"""
try:
previous_classes.add(cls)
except AttributeError: # NoneType
previous_classes = {cls}
# The identify check (.. is MISSING) is fine, MISSING is a singleton
has_init_vars = any(field.type == InitVar and field.default is MISSING
for field in cls.__dataclass_fields__.values())
children_have_init_vars = any(
child.type._contains_non_default_init_vars(previous_classes)
for child in fields(cls)
if (is_dataclass(child.type)
and child.type not in previous_classes))
return has_init_vars or children_have_init_vars
示例2: _get_fields
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def _get_fields(cls, base_fields=True) -> List[JsonSchemaField]:
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
if not base_fields:
return _get_fields_uncached()
if not cls.__mapped_fields:
cls.__mapped_fields = _get_fields_uncached()
return cls.__mapped_fields # type: ignore
示例3: from_dict
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def from_dict(cls: Type[T], data: JsonDict, validate=True, validate_enums: bool = True) -> T:
"""Returns a dataclass instance with all nested classes converted from the dict given"""
if cls is JsonSchemaMixin:
raise NotImplementedError
if cls.__discriminator_name is not None and cls.__discriminator_name in data:
if data[cls.__discriminator_name] != cls.__name__:
for subclass in cls.__subclasses__():
if subclass.__name__ == data[cls.__discriminator_name]:
return subclass.from_dict(data, validate)
init_values: Dict[str, Any] = {}
non_init_values: Dict[str, Any] = {}
if validate:
cls._validate(data, validate_enums)
for f in cls._get_fields():
values = init_values if f.field.init else non_init_values
if f.mapped_name in data or (
f.field.default == MISSING and f.field.default_factory == MISSING): # type: ignore
try:
values[f.field.name] = cls._decode_field(f.field.name, f.field.type, data.get(f.mapped_name))
except ValueError:
if is_enum(f.field.type):
values[f.field.name] = data.get(f.mapped_name)
else:
raise
# Need to ignore the type error here, since mypy doesn't know that subclasses are dataclasses
instance = cls(**init_values) # type: ignore
for field_name, value in non_init_values.items():
setattr(instance, field_name, value)
return instance
示例4: _get_field_meta
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
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
示例5: tensorflow_get_default
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def tensorflow_get_default(type_str):
if not "default" in type_str:
return dataclasses.MISSING
type_str = type_str[type_str.index("default") :]
type_str = type_str.replace("default", "")
type_str = type_str.replace(")", "")
type_str = type_str.replace("=", "")
type_str = type_str.replace('"', "")
type_str = type_str.replace("'", "")
type_str = type_str.strip()
if type_str == "None":
return None
return type_str
示例6: make_config
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def make_config(cls_name: str, fields, *args, namespace=None, **kwargs):
"""
Function to create a dataclass
"""
if namespace is None:
namespace = {}
namespace.setdefault("_fromdict", classmethod(_fromdict))
namespace.setdefault(
"_replace",
lambda self, *args, **kwargs: dataclasses.replace(
self, *args, **kwargs
),
)
namespace.setdefault("_asdict", config_asdict)
kwargs["eq"] = True
kwargs["init"] = True
# Ensure non-default arguments always come before default arguments
fields_non_default = []
fields_default = []
for name, cls, field in fields:
if (
field.default is not dataclasses.MISSING
or field.default_factory is not dataclasses.MISSING
):
fields_default.append((name, cls, field))
else:
fields_non_default.append((name, cls, field))
fields = fields_non_default + fields_default
# Create dataclass
return dataclasses.make_dataclass(
cls_name, fields, *args, namespace=namespace, **kwargs
)
示例7: numpy_get_default
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def numpy_get_default(type_str):
if not "default" in type_str:
return dataclasses.MISSING
type_str = type_str[type_str.index("default") :]
type_str = type_str.replace("default", "")
type_str = type_str.replace(")", "")
type_str = type_str.replace("=", "")
type_str = type_str.replace('"', "")
type_str = type_str.replace("'", "")
type_str = type_str.strip()
if type_str == "None":
return None
return type_str
示例8: _raise_missing_error
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def _raise_missing_error(obj: Any, name: str) -> None:
raise ValueError(
f"Missing default value for {get_type_of(obj).__name__}.{name}, to indicate "
"default must be populated later use OmegaConf.MISSING"
)
示例9: get_dataclass_data
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def get_dataclass_data(obj: Any) -> Dict[str, Any]:
from omegaconf.omegaconf import _maybe_wrap
d = {}
for field in dataclasses.fields(obj):
name = field.name
is_optional, type_ = _resolve_optional(field.type)
type_ = _resolve_forward(type_, obj.__module__)
is_nested = is_structured_config(type_)
if hasattr(obj, name):
value = getattr(obj, name)
if value == dataclasses.MISSING:
_raise_missing_error(obj, name)
assert False
else:
if field.default_factory == dataclasses.MISSING: # type: ignore
if is_nested:
value = type_
else:
_raise_missing_error(obj, name)
assert False
else:
value = field.default_factory() # type: ignore
if _is_union(type_):
e = ConfigValueError(
f"Union types are not supported:\n{name}: {type_str(type_)}"
)
format_and_raise(node=None, key=None, value=value, cause=e, msg=str(e))
d[name] = _maybe_wrap(
ref_type=type_, is_optional=is_optional, key=name, value=value, parent=None,
)
return d
示例10: _resolve_default
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def _resolve_default(val):
if not isinstance(val, Field):
return val
if val.default != MISSING:
return val.default
if val.default_factory != MISSING:
return val.default_factory()
raise ValueError("No default value")
示例11: _get_field_default
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def _get_field_default(field: dataclasses.Field):
"""
Return a marshmallow default value given a dataclass default value
>>> _get_field_default(dataclasses.field())
<marshmallow.missing>
"""
# Remove `type: ignore` when https://github.com/python/mypy/issues/6910 is fixed
default_factory = field.default_factory # type: ignore
if default_factory is not dataclasses.MISSING:
return default_factory
elif field.default is dataclasses.MISSING:
return marshmallow.missing
return field.default
示例12: schema
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def schema(cls, mixin, infer_missing):
schema = {}
overrides = _user_overrides_or_exts(cls)
# TODO check the undefined parameters and add the proper schema action
# https://marshmallow.readthedocs.io/en/stable/quickstart.html
for field in dc_fields(cls):
metadata = (field.metadata or {}).get('dataclasses_json', {})
metadata = overrides[field.name]
if metadata.mm_field is not None:
schema[field.name] = metadata.mm_field
else:
type_ = field.type
options = {}
missing_key = 'missing' if infer_missing else 'default'
if field.default is not MISSING:
options[missing_key] = field.default
elif field.default_factory is not MISSING:
options[missing_key] = field.default_factory
if options.get(missing_key, ...) is None:
options['allow_none'] = True
if _is_optional(type_):
options.setdefault(missing_key, None)
options['allow_none'] = True
if len(type_.__args__) == 2:
# Union[str, int, None] is optional too, but it has more than 1 typed field.
type_ = type_.__args__[0]
if metadata.letter_case is not None:
options['data_key'] = metadata.letter_case(field.name)
t = build_type(type_, options, mixin, field, cls)
# if type(t) is not fields.Field: # If we use `isinstance` we would return nothing.
if field.type != typing.Optional[CatchAllVar]:
schema[field.name] = t
return schema
示例13: defaults
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def defaults(self):
d = {}
for ancestor in self.cls.__mro__[-1:0:-1]:
if is_dataclass(ancestor):
for field in getattr(ancestor, _FIELDS).values():
d[field.name] = field.default
for name in self.__get_fields(recursive=False):
d[name] = self.namespace.get(name, MISSING)
return d
示例14: from_object
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def from_object(cls: Type[T], obj: Any, exclude: FieldExcludeList = tuple()) -> T:
"""Returns a dataclass instance from another object (typically an ORM model).
The `exclude` parameter is a tuple of field names or (field.name, nested_exclude)
to exclude from the conversion. For example `exclude=('artist_name', ('albums', ('tracks',))` will exclude
the `artist_name` and `tracks` from related albums
"""
exclude_dict = dict([(f[0], f[1]) if isinstance(f, tuple) else (f, None) for f in exclude])
init_values: Dict[str, Any] = {}
non_init_values: Dict[str, Any] = {}
for f in cls._get_fields():
sub_exclude: FieldExcludeList = tuple()
if f.field.name in exclude_dict:
if exclude_dict[f.field.name] is None:
if f.field.default == MISSING and f.field.default == MISSING:
raise ValueError("Excluded fields must have a default value")
continue
else:
sub_exclude = exclude_dict[f.field.name] # type: ignore
values = init_values if f.field.init else non_init_values
ft = f.field.type
if is_optional(ft):
ft = unwrap_optional(ft)
field_type_name = cls._get_field_type_name(ft)
from_value = getattr(obj, f.field.name)
if from_value is None:
values[f.field.name] = from_value
elif cls._is_json_schema_subclass(ft):
values[f.field.name] = ft.from_object(from_value, exclude=sub_exclude)
elif is_enum(ft):
values[f.field.name] = ft(from_value)
elif field_type_name == "List" and cls._is_json_schema_subclass(ft.__args__[0]):
values[f.field.name] = [
ft.__args__[0].from_object(v, exclude=sub_exclude) for v in from_value
]
else:
values[f.field.name] = from_value
instance = cls(**init_values) # type: ignore
for field_name, value in non_init_values.items():
setattr(instance, field_name, value)
return instance
示例15: wrap
# 需要導入模塊: import dataclasses [as 別名]
# 或者: from dataclasses import MISSING [as 別名]
def wrap(cls, func):
"""
If a subclass of BaseConfigurable is passed keyword arguments, convert
them into the instance of the CONFIG class.
"""
@functools.wraps(func)
def wrapper(self, config: Optional[BaseConfig] = None, **kwargs):
if config is not None and len(kwargs):
raise ConfigAndKWArgsMutuallyExclusive
elif config is None and hasattr(self, "CONFIG"):
if kwargs:
try:
config = self.CONFIG(**kwargs)
except TypeError as error:
error.args = (
error.args[0].replace(
"__init__", f"{self.CONFIG.__qualname__}"
),
)
raise
else:
use_CONFIG = True
for field in dataclasses.fields(self.CONFIG):
if (
field.default is dataclasses.MISSING
and field.default_factory is dataclasses.MISSING
):
use_CONFIG = False
break
if use_CONFIG:
config = self.CONFIG()
else:
raise TypeError(
"__init__() missing 1 required positional argument: 'config'"
)
elif config is None:
raise TypeError(
"__init__() missing 1 required positional argument: 'config'"
)
return func(self, config)
return wrapper