bagof.magic.fields
Classes
Field
Bases: SlotsBase
A field in a Magic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The name of the field. |
required |
type
|
type or type hint
|
The type of the field. This is used for type checking, validation, and conversion. |
required |
default
|
any
|
The default value for the field. |
required |
factory
|
Callable[[], any]
|
A factory function that generates a default value for the field. |
`Options().factory`
|
init
|
bool
|
Whether to include this field in the generated |
`Options().init`
|
repr
|
bool
|
Whether to include this field in the generated |
`Options().repr`
|
hash
|
bool
|
Whether to include this field in the generated |
`Options().hash`
|
eq
|
bool
|
Whether to include this field in the generated |
`Options().eq`
|
order
|
bool
|
Whether to include this field in the generated |
`Options().order`
|
metadata
|
dict
|
User-defined metadata for this field. |
required |
kw
|
bool
|
Make this field a keyword argument in the generated |
`not Options().positional_only`
|
positional
|
bool
|
Make this field a positional argument in the generated |
`not Options().kw_only`
|
frozen
|
bool
|
Whether to make this field immutable after initialization. |
`Options().frozen`
|
converter
|
bool | Callable[[any], any]
|
A function that converts the input value for this field.
If |
`Options().convert`
|
validator
|
bool | Callable[[any], any]
|
A function that validates the input value for this field.
If should be pass-through when the value is valid, and raise
an exception when it is not.
If |
`Options().validate`
|
var
|
bool
|
Whether this field is a pseudo-field (InitVar or ClassVar).
Pseudo-fields are not set by the generated |
False
|
doc
|
str
|
A docstring for this field.
The |
required |
key
|
bool | str
|
Whether to include this field in the generated dict-like interface. If a string, it will be used as the key. |
`Options().mapping`
|
alias
|
str
|
An alternative name for this field in the generated methods. This is useful when the field name is not a valid Python identifier, or when you want to use a different name in the generated methods for readability or consistency with an external API. By default, names that start with an underscore will have the underscore stripped in the alias. |
`name.lstrip("_")`
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
compare |
bool
|
Alias for setting both |
Default
Factory
ConvertTo
Validate
Init
Kw
Bases: BoolAnnotatedField
Specify that a field is [not] a keyword-only parameter.
Kw() ~> Field(kw=True)
NotKw() ~> Field(kw=False)
KwOnly() ~> Field(kw=True, positional=False)
Kw[int] ~> Annotated[T, Field(kw=True)]
NotKw[int] ~> Annotated[T, Field(kw=False)]
KwOnly[int] ~> Annotated[T, Field(kw=True, positional=False)]
Positional
Bases: BoolAnnotatedField
Specify that a field is [not] a positional-only parameter.
Positional() ~> Field(positional=True)
NotPositional() ~> Field(positional=False)
PositionalOnly() ~> Field(positional=True, kw=False)
Positional[int] ~> Annotated[T, Field(positional=True)]
NotPositional[int] ~> Annotated[T, Field(positional=False)]
PositionalOnly[int] ~> Annotated[T, Field(positional=True, kw=False)]
Frozen
Var
Bases: BoolAnnotatedField
Specify that a field is a pseudo-field (InitVar or ClassVar).
Var() ~> Field(var=True)
InitVar() ~> Field(var=True, init=True)
ClassVar() ~> Field(var=True, init=False)
Var[int] ~> Annotated[T, Field(var=True)]
InitVar[int] ~> Annotated[T, Field(var=True, init=True)]
ClassVar[int] ~> Annotated[T, Field(var=True, init=False)]
Repr
Eq
Order
Hash
Key
Doc
Doc(documentation: str)