Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python3....
File: dataclasses.py
import re
[0] Fix | Delete
import sys
[1] Fix | Delete
import copy
[2] Fix | Delete
import types
[3] Fix | Delete
import inspect
[4] Fix | Delete
import keyword
[5] Fix | Delete
import builtins
[6] Fix | Delete
import functools
[7] Fix | Delete
import _thread
[8] Fix | Delete
[9] Fix | Delete
[10] Fix | Delete
__all__ = ['dataclass',
[11] Fix | Delete
'field',
[12] Fix | Delete
'Field',
[13] Fix | Delete
'FrozenInstanceError',
[14] Fix | Delete
'InitVar',
[15] Fix | Delete
'MISSING',
[16] Fix | Delete
[17] Fix | Delete
# Helper functions.
[18] Fix | Delete
'fields',
[19] Fix | Delete
'asdict',
[20] Fix | Delete
'astuple',
[21] Fix | Delete
'make_dataclass',
[22] Fix | Delete
'replace',
[23] Fix | Delete
'is_dataclass',
[24] Fix | Delete
]
[25] Fix | Delete
[26] Fix | Delete
# Conditions for adding methods. The boxes indicate what action the
[27] Fix | Delete
# dataclass decorator takes. For all of these tables, when I talk
[28] Fix | Delete
# about init=, repr=, eq=, order=, unsafe_hash=, or frozen=, I'm
[29] Fix | Delete
# referring to the arguments to the @dataclass decorator. When
[30] Fix | Delete
# checking if a dunder method already exists, I mean check for an
[31] Fix | Delete
# entry in the class's __dict__. I never check to see if an attribute
[32] Fix | Delete
# is defined in a base class.
[33] Fix | Delete
[34] Fix | Delete
# Key:
[35] Fix | Delete
# +=========+=========================================+
[36] Fix | Delete
# + Value | Meaning |
[37] Fix | Delete
# +=========+=========================================+
[38] Fix | Delete
# | <blank> | No action: no method is added. |
[39] Fix | Delete
# +---------+-----------------------------------------+
[40] Fix | Delete
# | add | Generated method is added. |
[41] Fix | Delete
# +---------+-----------------------------------------+
[42] Fix | Delete
# | raise | TypeError is raised. |
[43] Fix | Delete
# +---------+-----------------------------------------+
[44] Fix | Delete
# | None | Attribute is set to None. |
[45] Fix | Delete
# +=========+=========================================+
[46] Fix | Delete
[47] Fix | Delete
# __init__
[48] Fix | Delete
#
[49] Fix | Delete
# +--- init= parameter
[50] Fix | Delete
# |
[51] Fix | Delete
# v | | |
[52] Fix | Delete
# | no | yes | <--- class has __init__ in __dict__?
[53] Fix | Delete
# +=======+=======+=======+
[54] Fix | Delete
# | False | | |
[55] Fix | Delete
# +-------+-------+-------+
[56] Fix | Delete
# | True | add | | <- the default
[57] Fix | Delete
# +=======+=======+=======+
[58] Fix | Delete
[59] Fix | Delete
# __repr__
[60] Fix | Delete
#
[61] Fix | Delete
# +--- repr= parameter
[62] Fix | Delete
# |
[63] Fix | Delete
# v | | |
[64] Fix | Delete
# | no | yes | <--- class has __repr__ in __dict__?
[65] Fix | Delete
# +=======+=======+=======+
[66] Fix | Delete
# | False | | |
[67] Fix | Delete
# +-------+-------+-------+
[68] Fix | Delete
# | True | add | | <- the default
[69] Fix | Delete
# +=======+=======+=======+
[70] Fix | Delete
[71] Fix | Delete
[72] Fix | Delete
# __setattr__
[73] Fix | Delete
# __delattr__
[74] Fix | Delete
#
[75] Fix | Delete
# +--- frozen= parameter
[76] Fix | Delete
# |
[77] Fix | Delete
# v | | |
[78] Fix | Delete
# | no | yes | <--- class has __setattr__ or __delattr__ in __dict__?
[79] Fix | Delete
# +=======+=======+=======+
[80] Fix | Delete
# | False | | | <- the default
[81] Fix | Delete
# +-------+-------+-------+
[82] Fix | Delete
# | True | add | raise |
[83] Fix | Delete
# +=======+=======+=======+
[84] Fix | Delete
# Raise because not adding these methods would break the "frozen-ness"
[85] Fix | Delete
# of the class.
[86] Fix | Delete
[87] Fix | Delete
# __eq__
[88] Fix | Delete
#
[89] Fix | Delete
# +--- eq= parameter
[90] Fix | Delete
# |
[91] Fix | Delete
# v | | |
[92] Fix | Delete
# | no | yes | <--- class has __eq__ in __dict__?
[93] Fix | Delete
# +=======+=======+=======+
[94] Fix | Delete
# | False | | |
[95] Fix | Delete
# +-------+-------+-------+
[96] Fix | Delete
# | True | add | | <- the default
[97] Fix | Delete
# +=======+=======+=======+
[98] Fix | Delete
[99] Fix | Delete
# __lt__
[100] Fix | Delete
# __le__
[101] Fix | Delete
# __gt__
[102] Fix | Delete
# __ge__
[103] Fix | Delete
#
[104] Fix | Delete
# +--- order= parameter
[105] Fix | Delete
# |
[106] Fix | Delete
# v | | |
[107] Fix | Delete
# | no | yes | <--- class has any comparison method in __dict__?
[108] Fix | Delete
# +=======+=======+=======+
[109] Fix | Delete
# | False | | | <- the default
[110] Fix | Delete
# +-------+-------+-------+
[111] Fix | Delete
# | True | add | raise |
[112] Fix | Delete
# +=======+=======+=======+
[113] Fix | Delete
# Raise because to allow this case would interfere with using
[114] Fix | Delete
# functools.total_ordering.
[115] Fix | Delete
[116] Fix | Delete
# __hash__
[117] Fix | Delete
[118] Fix | Delete
# +------------------- unsafe_hash= parameter
[119] Fix | Delete
# | +----------- eq= parameter
[120] Fix | Delete
# | | +--- frozen= parameter
[121] Fix | Delete
# | | |
[122] Fix | Delete
# v v v | | |
[123] Fix | Delete
# | no | yes | <--- class has explicitly defined __hash__
[124] Fix | Delete
# +=======+=======+=======+========+========+
[125] Fix | Delete
# | False | False | False | | | No __eq__, use the base class __hash__
[126] Fix | Delete
# +-------+-------+-------+--------+--------+
[127] Fix | Delete
# | False | False | True | | | No __eq__, use the base class __hash__
[128] Fix | Delete
# +-------+-------+-------+--------+--------+
[129] Fix | Delete
# | False | True | False | None | | <-- the default, not hashable
[130] Fix | Delete
# +-------+-------+-------+--------+--------+
[131] Fix | Delete
# | False | True | True | add | | Frozen, so hashable, allows override
[132] Fix | Delete
# +-------+-------+-------+--------+--------+
[133] Fix | Delete
# | True | False | False | add | raise | Has no __eq__, but hashable
[134] Fix | Delete
# +-------+-------+-------+--------+--------+
[135] Fix | Delete
# | True | False | True | add | raise | Has no __eq__, but hashable
[136] Fix | Delete
# +-------+-------+-------+--------+--------+
[137] Fix | Delete
# | True | True | False | add | raise | Not frozen, but hashable
[138] Fix | Delete
# +-------+-------+-------+--------+--------+
[139] Fix | Delete
# | True | True | True | add | raise | Frozen, so hashable
[140] Fix | Delete
# +=======+=======+=======+========+========+
[141] Fix | Delete
# For boxes that are blank, __hash__ is untouched and therefore
[142] Fix | Delete
# inherited from the base class. If the base is object, then
[143] Fix | Delete
# id-based hashing is used.
[144] Fix | Delete
#
[145] Fix | Delete
# Note that a class may already have __hash__=None if it specified an
[146] Fix | Delete
# __eq__ method in the class body (not one that was created by
[147] Fix | Delete
# @dataclass).
[148] Fix | Delete
#
[149] Fix | Delete
# See _hash_action (below) for a coded version of this table.
[150] Fix | Delete
[151] Fix | Delete
[152] Fix | Delete
# Raised when an attempt is made to modify a frozen class.
[153] Fix | Delete
class FrozenInstanceError(AttributeError): pass
[154] Fix | Delete
[155] Fix | Delete
# A sentinel object for default values to signal that a default
[156] Fix | Delete
# factory will be used. This is given a nice repr() which will appear
[157] Fix | Delete
# in the function signature of dataclasses' constructors.
[158] Fix | Delete
class _HAS_DEFAULT_FACTORY_CLASS:
[159] Fix | Delete
def __repr__(self):
[160] Fix | Delete
return '<factory>'
[161] Fix | Delete
_HAS_DEFAULT_FACTORY = _HAS_DEFAULT_FACTORY_CLASS()
[162] Fix | Delete
[163] Fix | Delete
# A sentinel object to detect if a parameter is supplied or not. Use
[164] Fix | Delete
# a class to give it a better repr.
[165] Fix | Delete
class _MISSING_TYPE:
[166] Fix | Delete
pass
[167] Fix | Delete
MISSING = _MISSING_TYPE()
[168] Fix | Delete
[169] Fix | Delete
# Since most per-field metadata will be unused, create an empty
[170] Fix | Delete
# read-only proxy that can be shared among all fields.
[171] Fix | Delete
_EMPTY_METADATA = types.MappingProxyType({})
[172] Fix | Delete
[173] Fix | Delete
# Markers for the various kinds of fields and pseudo-fields.
[174] Fix | Delete
class _FIELD_BASE:
[175] Fix | Delete
def __init__(self, name):
[176] Fix | Delete
self.name = name
[177] Fix | Delete
def __repr__(self):
[178] Fix | Delete
return self.name
[179] Fix | Delete
_FIELD = _FIELD_BASE('_FIELD')
[180] Fix | Delete
_FIELD_CLASSVAR = _FIELD_BASE('_FIELD_CLASSVAR')
[181] Fix | Delete
_FIELD_INITVAR = _FIELD_BASE('_FIELD_INITVAR')
[182] Fix | Delete
[183] Fix | Delete
# The name of an attribute on the class where we store the Field
[184] Fix | Delete
# objects. Also used to check if a class is a Data Class.
[185] Fix | Delete
_FIELDS = '__dataclass_fields__'
[186] Fix | Delete
[187] Fix | Delete
# The name of an attribute on the class that stores the parameters to
[188] Fix | Delete
# @dataclass.
[189] Fix | Delete
_PARAMS = '__dataclass_params__'
[190] Fix | Delete
[191] Fix | Delete
# The name of the function, that if it exists, is called at the end of
[192] Fix | Delete
# __init__.
[193] Fix | Delete
_POST_INIT_NAME = '__post_init__'
[194] Fix | Delete
[195] Fix | Delete
# String regex that string annotations for ClassVar or InitVar must match.
[196] Fix | Delete
# Allows "identifier.identifier[" or "identifier[".
[197] Fix | Delete
# https://bugs.python.org/issue33453 for details.
[198] Fix | Delete
_MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')
[199] Fix | Delete
[200] Fix | Delete
class _InitVarMeta(type):
[201] Fix | Delete
def __getitem__(self, params):
[202] Fix | Delete
return InitVar(params)
[203] Fix | Delete
[204] Fix | Delete
class InitVar(metaclass=_InitVarMeta):
[205] Fix | Delete
__slots__ = ('type', )
[206] Fix | Delete
[207] Fix | Delete
def __init__(self, type):
[208] Fix | Delete
self.type = type
[209] Fix | Delete
[210] Fix | Delete
def __repr__(self):
[211] Fix | Delete
if isinstance(self.type, type):
[212] Fix | Delete
type_name = self.type.__name__
[213] Fix | Delete
else:
[214] Fix | Delete
# typing objects, e.g. List[int]
[215] Fix | Delete
type_name = repr(self.type)
[216] Fix | Delete
return f'dataclasses.InitVar[{type_name}]'
[217] Fix | Delete
[218] Fix | Delete
[219] Fix | Delete
# Instances of Field are only ever created from within this module,
[220] Fix | Delete
# and only from the field() function, although Field instances are
[221] Fix | Delete
# exposed externally as (conceptually) read-only objects.
[222] Fix | Delete
#
[223] Fix | Delete
# name and type are filled in after the fact, not in __init__.
[224] Fix | Delete
# They're not known at the time this class is instantiated, but it's
[225] Fix | Delete
# convenient if they're available later.
[226] Fix | Delete
#
[227] Fix | Delete
# When cls._FIELDS is filled in with a list of Field objects, the name
[228] Fix | Delete
# and type fields will have been populated.
[229] Fix | Delete
class Field:
[230] Fix | Delete
__slots__ = ('name',
[231] Fix | Delete
'type',
[232] Fix | Delete
'default',
[233] Fix | Delete
'default_factory',
[234] Fix | Delete
'repr',
[235] Fix | Delete
'hash',
[236] Fix | Delete
'init',
[237] Fix | Delete
'compare',
[238] Fix | Delete
'metadata',
[239] Fix | Delete
'_field_type', # Private: not to be used by user code.
[240] Fix | Delete
)
[241] Fix | Delete
[242] Fix | Delete
def __init__(self, default, default_factory, init, repr, hash, compare,
[243] Fix | Delete
metadata):
[244] Fix | Delete
self.name = None
[245] Fix | Delete
self.type = None
[246] Fix | Delete
self.default = default
[247] Fix | Delete
self.default_factory = default_factory
[248] Fix | Delete
self.init = init
[249] Fix | Delete
self.repr = repr
[250] Fix | Delete
self.hash = hash
[251] Fix | Delete
self.compare = compare
[252] Fix | Delete
self.metadata = (_EMPTY_METADATA
[253] Fix | Delete
if metadata is None else
[254] Fix | Delete
types.MappingProxyType(metadata))
[255] Fix | Delete
self._field_type = None
[256] Fix | Delete
[257] Fix | Delete
def __repr__(self):
[258] Fix | Delete
return ('Field('
[259] Fix | Delete
f'name={self.name!r},'
[260] Fix | Delete
f'type={self.type!r},'
[261] Fix | Delete
f'default={self.default!r},'
[262] Fix | Delete
f'default_factory={self.default_factory!r},'
[263] Fix | Delete
f'init={self.init!r},'
[264] Fix | Delete
f'repr={self.repr!r},'
[265] Fix | Delete
f'hash={self.hash!r},'
[266] Fix | Delete
f'compare={self.compare!r},'
[267] Fix | Delete
f'metadata={self.metadata!r},'
[268] Fix | Delete
f'_field_type={self._field_type}'
[269] Fix | Delete
')')
[270] Fix | Delete
[271] Fix | Delete
# This is used to support the PEP 487 __set_name__ protocol in the
[272] Fix | Delete
# case where we're using a field that contains a descriptor as a
[273] Fix | Delete
# default value. For details on __set_name__, see
[274] Fix | Delete
# https://www.python.org/dev/peps/pep-0487/#implementation-details.
[275] Fix | Delete
#
[276] Fix | Delete
# Note that in _process_class, this Field object is overwritten
[277] Fix | Delete
# with the default value, so the end result is a descriptor that
[278] Fix | Delete
# had __set_name__ called on it at the right time.
[279] Fix | Delete
def __set_name__(self, owner, name):
[280] Fix | Delete
func = getattr(type(self.default), '__set_name__', None)
[281] Fix | Delete
if func:
[282] Fix | Delete
# There is a __set_name__ method on the descriptor, call
[283] Fix | Delete
# it.
[284] Fix | Delete
func(self.default, owner, name)
[285] Fix | Delete
[286] Fix | Delete
[287] Fix | Delete
class _DataclassParams:
[288] Fix | Delete
__slots__ = ('init',
[289] Fix | Delete
'repr',
[290] Fix | Delete
'eq',
[291] Fix | Delete
'order',
[292] Fix | Delete
'unsafe_hash',
[293] Fix | Delete
'frozen',
[294] Fix | Delete
)
[295] Fix | Delete
[296] Fix | Delete
def __init__(self, init, repr, eq, order, unsafe_hash, frozen):
[297] Fix | Delete
self.init = init
[298] Fix | Delete
self.repr = repr
[299] Fix | Delete
self.eq = eq
[300] Fix | Delete
self.order = order
[301] Fix | Delete
self.unsafe_hash = unsafe_hash
[302] Fix | Delete
self.frozen = frozen
[303] Fix | Delete
[304] Fix | Delete
def __repr__(self):
[305] Fix | Delete
return ('_DataclassParams('
[306] Fix | Delete
f'init={self.init!r},'
[307] Fix | Delete
f'repr={self.repr!r},'
[308] Fix | Delete
f'eq={self.eq!r},'
[309] Fix | Delete
f'order={self.order!r},'
[310] Fix | Delete
f'unsafe_hash={self.unsafe_hash!r},'
[311] Fix | Delete
f'frozen={self.frozen!r}'
[312] Fix | Delete
')')
[313] Fix | Delete
[314] Fix | Delete
[315] Fix | Delete
# This function is used instead of exposing Field creation directly,
[316] Fix | Delete
# so that a type checker can be told (via overloads) that this is a
[317] Fix | Delete
# function whose type depends on its parameters.
[318] Fix | Delete
def field(*, default=MISSING, default_factory=MISSING, init=True, repr=True,
[319] Fix | Delete
hash=None, compare=True, metadata=None):
[320] Fix | Delete
"""Return an object to identify dataclass fields.
[321] Fix | Delete
[322] Fix | Delete
default is the default value of the field. default_factory is a
[323] Fix | Delete
0-argument function called to initialize a field's value. If init
[324] Fix | Delete
is True, the field will be a parameter to the class's __init__()
[325] Fix | Delete
function. If repr is True, the field will be included in the
[326] Fix | Delete
object's repr(). If hash is True, the field will be included in
[327] Fix | Delete
the object's hash(). If compare is True, the field will be used
[328] Fix | Delete
in comparison functions. metadata, if specified, must be a
[329] Fix | Delete
mapping which is stored but not otherwise examined by dataclass.
[330] Fix | Delete
[331] Fix | Delete
It is an error to specify both default and default_factory.
[332] Fix | Delete
"""
[333] Fix | Delete
[334] Fix | Delete
if default is not MISSING and default_factory is not MISSING:
[335] Fix | Delete
raise ValueError('cannot specify both default and default_factory')
[336] Fix | Delete
return Field(default, default_factory, init, repr, hash, compare,
[337] Fix | Delete
metadata)
[338] Fix | Delete
[339] Fix | Delete
[340] Fix | Delete
def _tuple_str(obj_name, fields):
[341] Fix | Delete
# Return a string representing each field of obj_name as a tuple
[342] Fix | Delete
# member. So, if fields is ['x', 'y'] and obj_name is "self",
[343] Fix | Delete
# return "(self.x,self.y)".
[344] Fix | Delete
[345] Fix | Delete
# Special case for the 0-tuple.
[346] Fix | Delete
if not fields:
[347] Fix | Delete
return '()'
[348] Fix | Delete
# Note the trailing comma, needed if this turns out to be a 1-tuple.
[349] Fix | Delete
return f'({",".join([f"{obj_name}.{f.name}" for f in fields])},)'
[350] Fix | Delete
[351] Fix | Delete
[352] Fix | Delete
# This function's logic is copied from "recursive_repr" function in
[353] Fix | Delete
# reprlib module to avoid dependency.
[354] Fix | Delete
def _recursive_repr(user_function):
[355] Fix | Delete
# Decorator to make a repr function return "..." for a recursive
[356] Fix | Delete
# call.
[357] Fix | Delete
repr_running = set()
[358] Fix | Delete
[359] Fix | Delete
@functools.wraps(user_function)
[360] Fix | Delete
def wrapper(self):
[361] Fix | Delete
key = id(self), _thread.get_ident()
[362] Fix | Delete
if key in repr_running:
[363] Fix | Delete
return '...'
[364] Fix | Delete
repr_running.add(key)
[365] Fix | Delete
try:
[366] Fix | Delete
result = user_function(self)
[367] Fix | Delete
finally:
[368] Fix | Delete
repr_running.discard(key)
[369] Fix | Delete
return result
[370] Fix | Delete
return wrapper
[371] Fix | Delete
[372] Fix | Delete
[373] Fix | Delete
def _create_fn(name, args, body, *, globals=None, locals=None,
[374] Fix | Delete
return_type=MISSING):
[375] Fix | Delete
# Note that we mutate locals when exec() is called. Caller
[376] Fix | Delete
# beware! The only callers are internal to this module, so no
[377] Fix | Delete
# worries about external callers.
[378] Fix | Delete
if locals is None:
[379] Fix | Delete
locals = {}
[380] Fix | Delete
if 'BUILTINS' not in locals:
[381] Fix | Delete
locals['BUILTINS'] = builtins
[382] Fix | Delete
return_annotation = ''
[383] Fix | Delete
if return_type is not MISSING:
[384] Fix | Delete
locals['_return_type'] = return_type
[385] Fix | Delete
return_annotation = '->_return_type'
[386] Fix | Delete
args = ','.join(args)
[387] Fix | Delete
body = '\n'.join(f' {b}' for b in body)
[388] Fix | Delete
[389] Fix | Delete
# Compute the text of the entire function.
[390] Fix | Delete
txt = f' def {name}({args}){return_annotation}:\n{body}'
[391] Fix | Delete
[392] Fix | Delete
local_vars = ', '.join(locals.keys())
[393] Fix | Delete
txt = f"def __create_fn__({local_vars}):\n{txt}\n return {name}"
[394] Fix | Delete
[395] Fix | Delete
ns = {}
[396] Fix | Delete
exec(txt, globals, ns)
[397] Fix | Delete
return ns['__create_fn__'](**locals)
[398] Fix | Delete
[399] Fix | Delete
[400] Fix | Delete
def _field_assign(frozen, name, value, self_name):
[401] Fix | Delete
# If we're a frozen class, then assign to our fields in __init__
[402] Fix | Delete
# via object.__setattr__. Otherwise, just use a simple
[403] Fix | Delete
# assignment.
[404] Fix | Delete
#
[405] Fix | Delete
# self_name is what "self" is called in this function: don't
[406] Fix | Delete
# hard-code "self", since that might be a field name.
[407] Fix | Delete
if frozen:
[408] Fix | Delete
return f'BUILTINS.object.__setattr__({self_name},{name!r},{value})'
[409] Fix | Delete
return f'{self_name}.{name}={value}'
[410] Fix | Delete
[411] Fix | Delete
[412] Fix | Delete
def _field_init(f, frozen, globals, self_name):
[413] Fix | Delete
# Return the text of the line in the body of __init__ that will
[414] Fix | Delete
# initialize this field.
[415] Fix | Delete
[416] Fix | Delete
default_name = f'_dflt_{f.name}'
[417] Fix | Delete
if f.default_factory is not MISSING:
[418] Fix | Delete
if f.init:
[419] Fix | Delete
# This field has a default factory. If a parameter is
[420] Fix | Delete
# given, use it. If not, call the factory.
[421] Fix | Delete
globals[default_name] = f.default_factory
[422] Fix | Delete
value = (f'{default_name}() '
[423] Fix | Delete
f'if {f.name} is _HAS_DEFAULT_FACTORY '
[424] Fix | Delete
f'else {f.name}')
[425] Fix | Delete
else:
[426] Fix | Delete
# This is a field that's not in the __init__ params, but
[427] Fix | Delete
# has a default factory function. It needs to be
[428] Fix | Delete
# initialized here by calling the factory function,
[429] Fix | Delete
# because there's no other way to initialize it.
[430] Fix | Delete
[431] Fix | Delete
# For a field initialized with a default=defaultvalue, the
[432] Fix | Delete
# class dict just has the default value
[433] Fix | Delete
# (cls.fieldname=defaultvalue). But that won't work for a
[434] Fix | Delete
# default factory, the factory must be called in __init__
[435] Fix | Delete
# and we must assign that to self.fieldname. We can't
[436] Fix | Delete
# fall back to the class dict's value, both because it's
[437] Fix | Delete
# not set, and because it might be different per-class
[438] Fix | Delete
# (which, after all, is why we have a factory function!).
[439] Fix | Delete
[440] Fix | Delete
globals[default_name] = f.default_factory
[441] Fix | Delete
value = f'{default_name}()'
[442] Fix | Delete
else:
[443] Fix | Delete
# No default factory.
[444] Fix | Delete
if f.init:
[445] Fix | Delete
if f.default is MISSING:
[446] Fix | Delete
# There's no default, just do an assignment.
[447] Fix | Delete
value = f.name
[448] Fix | Delete
elif f.default is not MISSING:
[449] Fix | Delete
globals[default_name] = f.default
[450] Fix | Delete
value = f.name
[451] Fix | Delete
else:
[452] Fix | Delete
# This field does not need initialization. Signify that
[453] Fix | Delete
# to the caller by returning None.
[454] Fix | Delete
return None
[455] Fix | Delete
[456] Fix | Delete
# Only test this now, so that we can create variables for the
[457] Fix | Delete
# default. However, return None to signify that we're not going
[458] Fix | Delete
# to actually do the assignment statement for InitVars.
[459] Fix | Delete
if f._field_type is _FIELD_INITVAR:
[460] Fix | Delete
return None
[461] Fix | Delete
[462] Fix | Delete
# Now, actually generate the field assignment.
[463] Fix | Delete
return _field_assign(frozen, f.name, value, self_name)
[464] Fix | Delete
[465] Fix | Delete
[466] Fix | Delete
def _init_param(f):
[467] Fix | Delete
# Return the __init__ parameter string for this field. For
[468] Fix | Delete
# example, the equivalent of 'x:int=3' (except instead of 'int',
[469] Fix | Delete
# reference a variable set to int, and instead of '3', reference a
[470] Fix | Delete
# variable set to 3).
[471] Fix | Delete
if f.default is MISSING and f.default_factory is MISSING:
[472] Fix | Delete
# There's no default, and no default_factory, just output the
[473] Fix | Delete
# variable name and type.
[474] Fix | Delete
default = ''
[475] Fix | Delete
elif f.default is not MISSING:
[476] Fix | Delete
# There's a default, this will be the name that's used to look
[477] Fix | Delete
# it up.
[478] Fix | Delete
default = f'=_dflt_{f.name}'
[479] Fix | Delete
elif f.default_factory is not MISSING:
[480] Fix | Delete
# There's a factory function. Set a marker.
[481] Fix | Delete
default = '=_HAS_DEFAULT_FACTORY'
[482] Fix | Delete
return f'{f.name}:_type_{f.name}{default}'
[483] Fix | Delete
[484] Fix | Delete
[485] Fix | Delete
def _init_fn(fields, frozen, has_post_init, self_name, globals):
[486] Fix | Delete
# fields contains both real fields and InitVar pseudo-fields.
[487] Fix | Delete
[488] Fix | Delete
# Make sure we don't have fields without defaults following fields
[489] Fix | Delete
# with defaults. This actually would be caught when exec-ing the
[490] Fix | Delete
# function source code, but catching it here gives a better error
[491] Fix | Delete
# message, and future-proofs us in case we build up the function
[492] Fix | Delete
# using ast.
[493] Fix | Delete
seen_default = False
[494] Fix | Delete
for f in fields:
[495] Fix | Delete
# Only consider fields in the __init__ call.
[496] Fix | Delete
if f.init:
[497] Fix | Delete
if not (f.default is MISSING and f.default_factory is MISSING):
[498] Fix | Delete
seen_default = True
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function