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