Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

382

383

384

385

386

387

388

389

390

391

392

393

394

395

396

397

398

399

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

415

416

417

418

419

420

421

422

423

424

425

426

427

428

429

430

431

432

433

434

435

436

437

438

439

440

441

442

443

444

445

446

447

448

449

450

451

452

453

454

455

456

457

458

459

460

461

462

463

464

465

466

467

468

469

470

471

472

473

474

475

476

477

478

479

480

481

482

483

484

485

486

487

488

489

490

491

492

493

494

495

496

497

498

499

500

501

502

503

504

505

506

507

508

509

510

511

512

513

514

515

516

517

518

519

520

521

522

523

524

525

526

527

528

529

530

531

532

533

534

535

536

537

538

539

540

541

542

543

544

545

546

547

548

549

550

551

552

553

554

555

556

557

558

559

560

561

562

563

564

565

566

567

568

569

570

571

572

573

574

575

576

577

578

579

580

581

582

583

584

585

586

587

588

589

590

591

592

593

594

595

596

597

598

599

600

601

602

603

604

605

606

607

608

609

610

611

612

613

614

615

616

617

618

619

620

621

622

623

624

625

626

627

628

629

630

631

632

633

634

635

636

637

638

639

640

641

642

643

644

645

646

647

648

649

650

651

652

653

654

655

656

657

658

659

660

661

662

663

664

665

666

667

668

669

670

671

672

673

674

675

676

677

678

679

680

681

682

683

684

685

686

687

688

689

690

691

692

693

694

695

696

697

698

699

700

701

702

703

704

705

706

707

708

709

710

711

712

713

714

715

716

717

718

719

720

721

722

723

724

725

726

727

728

729

730

731

732

733

734

735

736

737

738

739

740

741

742

743

744

745

746

747

748

749

750

751

752

753

754

755

756

757

758

759

760

761

762

763

764

765

766

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

from __future__ import annotations 

 

__all__ = ( 

"allSlots", 

"getClassOf", 

"getFullTypeName", 

"getInstanceOf", 

"getObjectSize", 

"immutable", 

"IndexedTupleDict", 

"iterable", 

"NamedKeyDict", 

"NamedValueSet", 

"PrivateConstructorMeta", 

"Singleton", 

"slotValuesAreEqual", 

"slotValuesToHash", 

"stripIfNotNone", 

"transactional", 

) 

 

import builtins 

import sys 

import functools 

from typing import (TypeVar, MutableMapping, Iterator, KeysView, ValuesView, ItemsView, Dict, Union, 

MutableSet, Iterable, Mapping, Tuple) 

from types import MappingProxyType 

 

from lsst.utils import doImport 

 

 

def iterable(a): 

"""Make input iterable. 

 

There are three cases, when the input is: 

 

- iterable, but not a `str` -> iterate over elements 

(e.g. ``[i for i in a]``) 

- a `str` -> return single element iterable (e.g. ``[a]``) 

- not iterable -> return single elment iterable (e.g. ``[a]``). 

 

Parameters 

---------- 

a : iterable or `str` or not iterable 

Argument to be converted to an iterable. 

 

Returns 

------- 

i : `generator` 

Iterable version of the input value. 

""" 

if isinstance(a, str): 

yield a 

return 

try: 

yield from a 

except Exception: 

yield a 

 

 

def allSlots(self): 

""" 

Return combined ``__slots__`` for all classes in objects mro. 

 

Parameters 

---------- 

self : `object` 

Instance to be inspected. 

 

Returns 

------- 

slots : `itertools.chain` 

All the slots as an iterable. 

""" 

from itertools import chain 

return chain.from_iterable(getattr(cls, "__slots__", []) for cls in self.__class__.__mro__) 

 

 

def slotValuesAreEqual(self, other): 

""" 

Test for equality by the contents of all slots, including those of its 

parents. 

 

Parameters 

---------- 

self : `object` 

Reference instance. 

other : `object` 

Comparison instance. 

 

Returns 

------- 

equal : `bool` 

Returns True if all the slots are equal in both arguments. 

""" 

return all((getattr(self, slot) == getattr(other, slot) for slot in allSlots(self))) 

 

 

def slotValuesToHash(self): 

""" 

Generate a hash from slot values. 

 

Parameters 

---------- 

self : `object` 

Instance to be hashed. 

 

Returns 

------- 

h : `int` 

Hashed value generated from the slot values. 

""" 

return hash(tuple(getattr(self, slot) for slot in allSlots(self))) 

 

 

def getFullTypeName(cls): 

"""Return full type name of the supplied entity. 

 

Parameters 

---------- 

cls : `type` or `object` 

Entity from which to obtain the full name. Can be an instance 

or a `type`. 

 

Returns 

------- 

name : `str` 

Full name of type. 

 

Notes 

----- 

Builtins are returned without the ``builtins`` specifier included. This 

allows `str` to be returned as "str" rather than "builtins.str". 

""" 

# If we have an instance we need to convert to a type 

156 ↛ 157line 156 didn't jump to line 157, because the condition on line 156 was never true if not hasattr(cls, "__qualname__"): 

cls = type(cls) 

158 ↛ 160line 158 didn't jump to line 160, because the condition on line 158 was never true if hasattr(builtins, cls.__qualname__): 

# Special case builtins such as str and dict 

return cls.__qualname__ 

return cls.__module__ + "." + cls.__qualname__ 

 

 

def getClassOf(typeOrName): 

"""Given the type name or a type, return the python type. 

 

If a type name is given, an attempt will be made to import the type. 

 

Parameters 

---------- 

typeOrName : `str` or Python class 

A string describing the Python class to load or a Python type. 

 

Returns 

------- 

type_ : `type` 

Directly returns the Python type if a type was provided, else 

tries to import the given string and returns the resulting type. 

 

Notes 

----- 

This is a thin wrapper around `~lsst.utils.doImport`. 

""" 

if isinstance(typeOrName, str): 

cls = doImport(typeOrName) 

else: 

cls = typeOrName 

return cls 

 

 

def getInstanceOf(typeOrName, *args, **kwargs): 

"""Given the type name or a type, instantiate an object of that type. 

 

If a type name is given, an attempt will be made to import the type. 

 

Parameters 

---------- 

typeOrName : `str` or Python class 

A string describing the Python class to load or a Python type. 

args : `tuple` 

Positional arguments to use pass to the object constructor. 

kwargs : `dict` 

Keyword arguments to pass to object constructor. 

 

Returns 

------- 

instance : `object` 

Instance of the requested type, instantiated with the provided 

parameters. 

""" 

cls = getClassOf(typeOrName) 

return cls(*args, **kwargs) 

 

 

class Singleton(type): 

"""Metaclass to convert a class to a Singleton. 

 

If this metaclass is used the constructor for the singleton class must 

take no arguments. This is because a singleton class will only accept 

the arguments the first time an instance is instantiated. 

Therefore since you do not know if the constructor has been called yet it 

is safer to always call it with no arguments and then call a method to 

adjust state of the singleton. 

""" 

 

_instances = {} 

 

def __call__(cls): 

if cls not in cls._instances: 

cls._instances[cls] = super(Singleton, cls).__call__() 

return cls._instances[cls] 

 

 

def transactional(func): 

"""Decorator that wraps a method and makes it transactional. 

 

This depends on the class also defining a `transaction` method 

that takes no arguments and acts as a context manager. 

""" 

@functools.wraps(func) 

def inner(self, *args, **kwargs): 

with self.transaction(): 

return func(self, *args, **kwargs) 

return inner 

 

 

def getObjectSize(obj, seen=None): 

"""Recursively finds size of objects. 

 

Only works well for pure python objects. For example it does not work for 

``Exposure`` objects where all the content is behind getter methods. 

 

Parameters 

---------- 

obj : `object` 

Instance for which size is to be calculated. 

seen : `set`, optional 

Used internally to keep track of objects already sized during 

recursion. 

 

Returns 

------- 

size : `int` 

Size in bytes. 

 

See Also 

-------- 

sys.getsizeof 

 

Notes 

----- 

See https://goshippo.com/blog/measure-real-size-any-python-object/ 

""" 

size = sys.getsizeof(obj) 

if seen is None: 

seen = set() 

obj_id = id(obj) 

if obj_id in seen: 

return 0 

# Important mark as seen *before* entering recursion to gracefully handle 

# self-referential objects 

seen.add(obj_id) 

if isinstance(obj, dict): 

size += sum([getObjectSize(v, seen) for v in obj.values()]) 

size += sum([getObjectSize(k, seen) for k in obj.keys()]) 

elif hasattr(obj, "__dict__"): 

size += getObjectSize(obj.__dict__, seen) 

elif hasattr(obj, "__iter__") and not isinstance(obj, (str, bytes, bytearray)): 

size += sum([getObjectSize(i, seen) for i in obj]) 

 

return size 

 

 

def stripIfNotNone(s): 

"""Strip leading and trailing whitespace if the given object is not None. 

 

Parameters 

---------- 

s : `str`, optional 

Input string. 

 

Returns 

------- 

r : `str` or `None` 

A string with leading and trailing whitespace stripped if `s` is not 

`None`, or `None` if `s` is `None`. 

""" 

if s is not None: 

s = s.strip() 

return s 

 

 

class PrivateConstructorMeta(type): 

"""A metaclass that disables regular construction syntax. 

 

A class that uses PrivateConstructorMeta may have an ``__init__`` and/or 

``__new__`` method, but these can't be invoked by "calling" the class 

(that will always raise `TypeError`). Instead, such classes can be called 

by calling the metaclass-provided `_construct` class method with the same 

arguments. 

 

As is usual in Python, there are no actual prohibitions on what code can 

call `_construct`; the purpose of this metaclass is just to prevent 

instances from being created normally when that can't do what users would 

expect. 

 

..note:: 

 

Classes that inherit from PrivateConstructorMeta also inherit 

the hidden-constructor behavior. If you just want to disable 

construction of the base class, `abc.ABCMeta` may be a better 

option. 

 

Examples 

-------- 

Given this class definition:: 

class Hidden(metaclass=PrivateConstructorMeta): 

 

def __init__(self, a, b): 

self.a = a 

self.b = b 

 

This doesn't work: 

 

>>> instance = Hidden(a=1, b="two") 

TypeError: Hidden objects cannot be constructed directly. 

 

But this does: 

 

>>> instance = Hidden._construct(a=1, b="two") 

 

""" 

 

def __call__(cls, *args, **kwds): 

"""Disabled class construction interface; always raises `TypeError.` 

""" 

raise TypeError(f"{cls.__name__} objects cannot be constructed directly.") 

 

def _construct(cls, *args, **kwds): 

"""Private class construction interface. 

 

All arguments are forwarded to ``__init__`` and/or ``__new__`` 

in the usual way. 

""" 

return type.__call__(cls, *args, **kwds) 

 

 

K = TypeVar("K") 

V = TypeVar("V") 

 

 

class NamedKeyDict(MutableMapping[K, V]): 

"""A dictionary wrapper that require keys to have a ``.name`` attribute, 

and permits lookups using either key objects or their names. 

 

Names can be used in place of keys when updating existing items, but not 

when adding new items. 

 

It is assumed (but asserted) that all name equality is equivalent to key 

equality, either because the key objects define equality this way, or 

because different objects with the same name are never included in the same 

dictionary. 

 

Parameters 

---------- 

args 

All positional constructor arguments are forwarded directly to `dict`. 

Keyword arguments are not accepted, because plain strings are not valid 

keys for `NamedKeyDict`. 

 

Raises 

------ 

AttributeError 

Raised when an attempt is made to add an object with no ``.name`` 

attribute to the dictionary. 

AssertionError 

Raised when multiple keys have the same name. 

""" 

 

__slots__ = ("_dict", "_names",) 

 

def __init__(self, *args): 

self._dict = dict(*args) 

self._names = {key.name: key for key in self._dict} 

assert len(self._names) == len(self._dict), "Duplicate names in keys." 

 

@property 

def names(self) -> KeysView[str]: 

"""The set of names associated with the keys, in the same order 

(`~collections.abc.KeysView`). 

""" 

return self._names.keys() 

 

def byName(self) -> Dict[str, V]: 

"""Return a `dict` with names as keys and the same values as ``self``. 

""" 

return dict(zip(self._names.keys(), self._dict.values())) 

 

def __len__(self) -> int: 

return len(self._dict) 

 

def __iter__(self) -> Iterator[K]: 

return iter(self._dict) 

 

def __str__(self) -> str: 

return "{{{}}}".format(", ".join(f"{str(k)}: {str(v)}" for k, v in self.items())) 

 

def __repr__(self) -> str: 

return "NamedKeyDict({{{}}})".format(", ".join(f"{repr(k)}: {repr(v)}" for k, v in self.items())) 

 

def __getitem__(self, key: Union[str, K]) -> V: 

if hasattr(key, "name"): 

return self._dict[key] 

else: 

return self._dict[self._names[key]] 

 

def __setitem__(self, key: Union[str, K], value: V): 

if hasattr(key, "name"): 

assert self._names.get(key.name, key) == key, "Name is already associated with a different key." 

self._dict[key] = value 

self._names[key.name] = key 

else: 

self._dict[self._names[key]] = value 

 

def __delitem__(self, key: Union[str, K]): 

if hasattr(key, "name"): 

del self._dict[key] 

del self._names[key.name] 

else: 

del self._dict[self._names[key]] 

del self._names[key] 

 

def keys(self) -> KeysView[K]: 

return self._dict.keys() 

 

def values(self) -> ValuesView[V]: 

return self._dict.values() 

 

def items(self) -> ItemsView[K, V]: 

return self._dict.items() 

 

def copy(self) -> NamedKeyDict[K, V]: 

result = NamedKeyDict.__new__(NamedKeyDict) 

result._dict = dict(self._dict) 

result._names = dict(self._names) 

return result 

 

def freeze(self): 

"""Disable all mutators, effectively transforming ``self`` into 

an immutable mapping. 

""" 

if not isinstance(self._dict, MappingProxyType): 

self._dict = MappingProxyType(self._dict) 

 

 

T = TypeVar("T") 

 

 

class NamedValueSet(MutableSet[T]): 

"""A custom mutable set class that requires elements to have a ``.name`` 

attribute, which can then be used as keys in `dict`-like lookup. 

 

Names and elements can both be used with the ``in`` and ``del`` 

operators, `remove`, and `discard`. Names (but not elements) 

can be used with ``[]``-based element retrieval (not assignment) 

and the `get` method. `pop` can be used in either its `MutableSet` 

form (no arguments; an arbitrary element is returned) or its 

`MutableMapping` form (one or two arguments for the name and 

optional default value, respectively). 

 

Parameters 

---------- 

elements : `iterable` 

Iterable over elements to include in the set. 

 

Raises 

------ 

AttributeError 

Raised if one or more elements do not have a ``.name`` attribute. 

 

Notes 

----- 

Iteration order is guaranteed to be the same as insertion order (with 

the same general behavior as `dict` ordering). 

Like `dicts`, sets with the same elements will compare as equal even if 

their iterator order is not the same. 

""" 

 

__slots__ = ("_dict",) 

 

def __init__(self, elements: Iterable[T] = ()): 

self._dict = {element.name: element for element in elements} 

 

@property 

def names(self) -> KeysView[str]: 

"""The set of element names, in the same order 

(`~collections.abc.KeysView`). 

""" 

return self._dict.keys() 

 

def asDict(self) -> Mapping[str, T]: 

"""Return a mapping view with names as keys. 

 

Returns 

------- 

dict : `Mapping` 

A dictionary-like view with ``values() == self``. 

""" 

return self._dict 

 

def __contains__(self, key: Union[str, T]) -> bool: 

return getattr(key, "name", key) in self._dict 

 

def __len__(self) -> int: 

return len(self._dict) 

 

def __iter__(self) -> Iterator[T]: 

return iter(self._dict.values()) 

 

def __str__(self) -> str: 

return "{{{}}}".format(", ".join(str(element) for element in self)) 

 

def __repr__(self) -> str: 

return "NamedValueSet({{{}}})".format(", ".join(repr(element) for element in self)) 

 

def __eq__(self, other): 

try: 

return self._dict.keys() == other._dict.keys() 

except AttributeError: 

return NotImplemented 

 

def __hash__(self): 

return hash(frozenset(self._dict.keys())) 

 

# As per Set's docs, overriding just __le__ and __ge__ for performance will 

# cover the other comparisons, too. 

 

def __le__(self, other: NamedValueSet[T]) -> bool: 

try: 

return self._dict.keys() <= other._dict.keys() 

except AttributeError: 

return NotImplemented 

 

def __ge__(self, other: NamedValueSet[T]) -> bool: 

try: 

return self._dict.keys() >= other._dict.keys() 

except AttributeError: 

return NotImplemented 

 

def issubset(self, other): 

return self <= other 

 

def issuperset(self, other): 

return self >= other 

 

def __getitem__(self, name: str) -> T: 

return self._dict[name] 

 

def get(self, name: str, default=None): 

"""Return the element with the given name, or ``default`` if 

no such element is present. 

""" 

return self._dict.get(name, default) 

 

def __delitem__(self, name: str): 

del self._dict[name] 

 

def add(self, element: T): 

"""Add an element to the set. 

 

Raises 

------ 

AttributeError 

Raised if the element does not have a ``.name`` attribute. 

""" 

self._dict[element.name] = element 

 

def remove(self, element: Union[str, T]): 

"""Remove an element from the set. 

 

Parameters 

---------- 

element : `object` or `str` 

Element to remove or the string name thereof. Assumed to be an 

element if it has a ``.name`` attribute. 

 

Raises 

------ 

KeyError 

Raised if an element with the given name does not exist. 

""" 

del self._dict[getattr(element, "name", element)] 

 

def discard(self, element: Union[str, T]): 

"""Remove an element from the set if it exists. 

 

Does nothing if no matching element is present. 

 

Parameters 

---------- 

element : `object` or `str` 

Element to remove or the string name thereof. Assumed to be an 

element if it has a ``.name`` attribute. 

""" 

try: 

self.remove(element) 

except KeyError: 

pass 

 

def pop(self, *args): 

"""Remove and return an element from the set. 

 

Parameters 

---------- 

name : `str`, optional 

Name of the element to remove and return. Must be passed 

positionally. If not provided, an arbitrary element is 

removed and returned. 

default : `object`, optional 

Value to return if ``name`` is provided but no such element 

exists. 

 

Raises 

------ 

KeyError 

Raised if ``name`` is provided but ``default`` is not, and no 

matching element exists. 

""" 

if not args: 

return super().pop() 

else: 

return self._dict.pop(*args) 

 

def copy(self) -> NamedValueSet[T]: 

result = NamedValueSet.__new__(NamedValueSet) 

result._dict = dict(self._dict) 

return result 

 

def freeze(self): 

"""Disable all mutators, effectively transforming ``self`` into 

an immutable set. 

""" 

if not isinstance(self._dict, MappingProxyType): 

self._dict = MappingProxyType(self._dict) 

 

 

class IndexedTupleDict(Mapping[K, V]): 

"""An immutable mapping that combines a tuple of values with a (possibly 

shared) mapping from key to tuple index. 

 

Parameters 

---------- 

indices: `~collections.abc.Mapping` 

Mapping from key to integer index in the values tuple. This mapping 

is used as-is, not copied or converted to a true `dict`, which means 

that the caller must guarantee that it will not be modified by other 

(shared) owners in the future. If it is a `NamedKeyDict`, both names 

and key instances will be usable as keys in the `IndexedTupleDict`. 

The caller is also responsible for guaranteeing that the indices in 

the mapping are all valid for the given tuple. 

values: `tuple` 

Tuple of values for the dictionary. The caller is responsible for 

guaranteeing that this has the same number of elements as ``indices``. 

""" 

 

__slots__ = ("_indices", "_values") 

 

def __new__(cls, indices: Mapping[K, int], values: Tuple[V, ...]): 

self = super().__new__(cls) 

assert len(indices) == len(values) 

self._indices = indices 

self._values = values 

return self 

 

def __getitem__(self, key: K) -> V: 

return self._values[self._indices[key]] 

 

def __iter__(self) -> Iterator[K]: 

return iter(self._indices) 

 

def __len__(self) -> int: 

return len(self._indices) 

 

def __str__(self) -> str: 

return "{{{}}}".format(", ".join(f"{str(k)}: {str(v)}" for k, v in self.items())) 

 

def __repr__(self) -> str: 

return "IndexedTupleDict({{{}}})".format(", ".join(f"{repr(k)}: {repr(v)}" for k, v in self.items())) 

 

def __contains__(self, key: K) -> bool: 

return key in self._indices 

 

def keys(self) -> KeysView[K]: 

return self._indices.keys() 

 

def values(self) -> Tuple[V, ...]: 

return self._values 

 

def __getnewargs__(self) -> tuple: 

return (self._indices, self._values) 

 

def __getstate__(self) -> dict: # noqa: N807 

# Disable default state-setting when unpickled. 

return {} 

 

def __setstate__(self, state): # noqa: N807 

# Disable default state-setting when copied. 

# Sadly what works for pickle doesn't work for copy. 

assert not state 

 

# Let Mapping base class provide items(); we can't do it any more 

# efficiently ourselves. 

 

 

def immutable(cls): 

"""A class decorator that simulates a simple form of immutability for 

the decorated class. 

 

A class decorated as `immutable` may only set each of its attributes once 

(by convention, in ``__new__``); any attempts to set an already-set 

attribute will raise `AttributeError`. 

 

Because this behavior interferes with the default implementation for 

the ``pickle`` and ``copy`` modules, `immutable` provides implementations 

of ``__getstate__`` and ``__setstate__`` that override this behavior. 

Immutable classes can them implement pickle/copy via ``__getnewargs__`` 

only (other approaches such as ``__reduce__`` and ``__deepcopy__`` may 

also be used). 

""" 

def __setattr__(self, name, value): # noqa: N807 

if hasattr(self, name): 

raise AttributeError(f"{cls.__name__} instances are immutable.") 

object.__setattr__(self, name, value) 

cls.__setattr__ = __setattr__ 

 

def __getstate__(self) -> dict: # noqa: N807 

# Disable default state-setting when unpickled. 

return {} 

cls.__getstate__ = __getstate__ 

 

def __setstate__(self, state): # noqa: N807 

# Disable default state-setting when copied. 

# Sadly what works for pickle doesn't work for copy. 

assert not state 

cls.__setstate__ = __setstate__ 

return cls