bagof.magic._fields
Classes
Field
Bases: SlotsBase
A single field in a Magic class.
Every annotation in a Magic class body becomes a Field. You rarely
create one directly. The annotation family (Factory, KwOnly,
ConvertTo, ...) and the field() function are the usual ways in.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The field's name in the class body. |
required |
type
|
type or type hint
|
The field's type. Used for conversion, validation and factory defaults when those are turned on. |
required |
default
|
any
|
The default value. |
required |
factory
|
bool or Callable[[], any]
|
How a fresh default is built per instance, rather than one
value shared across instances: |
`Options().factory`
|
init
|
bool
|
Whether this field appears in |
required |
repr
|
bool
|
Include this field in the generated |
True (False for a pseudo-field)
|
hash
|
bool
|
Include this field in the generated |
None (follows `eq`)
|
eq
|
bool
|
Include this field in the generated |
True
|
order
|
bool
|
Include this field in the generated ordering. A field out
of |
follows `eq`
|
metadata
|
dict
|
Arbitrary user-defined metadata. |
required |
kw
|
bool
|
Allow this field to be passed by keyword. To make it
keyword-only, also set |
`not Options().positional_only`
|
positional
|
bool
|
Allow this field to be passed by position. To make it
positional-only, also set |
`not Options().kw_only`
|
frozen
|
bool
|
Forbid assignment after construction. |
`Options().frozen`
|
converter
|
bool or Callable[[any], any]
|
How the incoming value is converted: |
`Options().convert`
|
validator
|
bool or Callable[[any], any]
|
How the incoming value is validated, in the same three forms
as |
`Options().validate`
|
var
|
bool
|
Mark this as a pseudo-field. An |
False
|
doc
|
str
|
Documentation for this field. Also settable through the
|
required |
key
|
bool | str
|
Include this field in the dict-like interface. A string value is used as the key name. |
`Options().mapping`
|
alias
|
str
|
The name used in generated methods (constructor parameter, repr output, dict key). Useful when the field name is not a good public name, or when matching an external API. |
`name.lstrip("_")`
|
Other Parameters:
| Name | Type | Description |
|---|---|---|
compare |
bool
|
Shorthand for setting both |
Attributes
init
property
writable
init: bool
Whether the generated __init__ takes this field.
True when the field can be passed by keyword, by position, or
both. False when it can be passed neither way. Computed from
kw and positional.
Setting field.init = True or field.init = False sets both
kw and positional to that value. Field(init=False) (or
NoInit) forbids both ways. Field(init=True) (or Init)
changes nothing, since a field is a parameter by default.
convert
property
convert: bool
Whether the incoming value is converted.
Reads converter: True for a converter worked out from the
type or a callable given directly, False when conversion is
off. Set conversion through converter, or the convert=
keyword when building the field.
validate
property
validate: bool
Whether the incoming value is validated.
Reads validator, the same way convert reads converter.
build
property
build: bool
Whether a fresh default is built for this field per instance.
Reads factory, the same way convert reads converter.
public_key
property
public_key: str | None
The key to use for this field in the generated dict-like interface.
Methods:
Default
Factory
Bases: AnnotatedField
Build a field's default by calling something, once per instance.
Use this instead of a plain default for anything mutable: every instance gets its own object. With no argument, the factory is worked out from the field's type.
How it lowers
In a class
ConvertTo
Bases: AnnotatedField
Convert whatever is passed in to the field's type.
With no argument the converter is worked out from the type; pass a callable to use your own.
How it lowers
In a class
Validate
Bases: AnnotatedField
Reject a value that does not match the field's type.
With no argument the check is worked out from the type; pass a callable
to use your own. Unlike ConvertTo, the value is left exactly as it
was given.
How it lowers
In a class
Init
Bases: BoolAnnotatedField
Include a field in the generated __init__, or leave it out.
NoInit lets a field be passed neither by name nor by position: it
still exists and takes its default or factory value, it just cannot
be passed in.
Init is the other way round and says nothing new -- a field is a
parameter unless something says otherwise -- so it changes nothing
and is there to say so out loud. How the field may be passed stays
with the class, or with Kw and Positional if you want to say.
How it lowers
Kw
Bases: BoolAnnotatedField
Allow a field to be passed by keyword, or forbid it.
Pair it with Positional to say exactly how a field may be given.
KwOnly and PositionalOnly are the two useful combinations, ready
made; forbidding both is NoInit.
How it lowers
Positional
Bases: BoolAnnotatedField
Allow a field to be passed by position, or forbid it.
Pair it with Kw to say exactly how a field may be given.
PositionalOnly and KwOnly are the two useful combinations, ready
made.
How it lowers
Frozen
Bases: BoolAnnotatedField
Forbid assignment to a field after the object is built.
Useful for freezing part of an otherwise mutable class.
How it lowers
In a class
Var
Bases: BoolAnnotatedField
Declare something that is not stored on each instance.
InitVar is passed to __init__, used, and not kept -- it reaches
__pre_init__ and __post_init__ like any other argument;
ClassVar is a plain class attribute, shared by every instance and
absent from __init__.
How it lowers
In a class
Repr
Bases: BoolAnnotatedField
Show a field in the generated __repr__, or hide it.
Use HIDE_IF_NONE to show it only when it has a value.
How it lowers
In a class
Eq
Bases: BoolAnnotatedField
Compare a field in the generated __eq__, or ignore it.
An ignored field takes no part in equality, so two objects that differ only there compare equal.
How it lowers
In a class
Order
Compare
Use a field for both equality and ordering, or for neither.
A shorthand for setting Eq and Order together.
How it lowers
Hash
Bases: BoolAnnotatedField
Include a field in the generated __hash__, or leave it out.
A field left out of the comparison is left out of the hash too, so you rarely need this on its own.
How it lowers
Key
Bases: BoolAnnotatedField
Include a field in the dict-like interface, or leave it out.
Only relevant on a class built with mapping=True. Pass a string to use
a different key from the field name.
How it lowers
In a class
Doc
Doc(documentation: str)
Bases: AnnotatedField, Doc
Document a field.
The text appears in the class docstring and in the documentation of the
generated __init__.
How it lowers
Functions:
field
Describe one field, for use as its default value.
class Task(Magic):
name: str
tags: list = field(factory=list)
token: str = field(default="", repr=False)
Takes the same arguments as Field and produces the same object.
The difference is for type checkers: field(...) declares its
return type as the annotated type, so tags: list = field(...) reads
cleanly. Field(...) in that position also works.