MCPcopy Index your code
hub / github.com/python/mypy / replace_object_state

Function replace_object_state

mypy/util.py:390–419  ·  view source on GitHub ↗

Copy state of old node to the new node. This handles cases where there is __dict__ and/or attribute descriptors (either from slots or because the type is defined in a C extension module). Assume that both objects have the same __class__.

(
    new: object, old: object, copy_dict: bool = False, skip_slots: tuple[str, ...] = ()
)

Source from the content-addressed store, hash-verified

388
389
390def replace_object_state(
391 new: object, old: object, copy_dict: bool = False, skip_slots: tuple[str, ...] = ()
392) -> None:
393 """Copy state of old node to the new node.
394
395 This handles cases where there is __dict__ and/or attribute descriptors
396 (either from slots or because the type is defined in a C extension module).
397
398 Assume that both objects have the same __class__.
399 """
400 if hasattr(old, "__dict__"):
401 if copy_dict:
402 new.__dict__ = dict(old.__dict__)
403 else:
404 new.__dict__ = old.__dict__
405
406 for attr in get_class_descriptors(old.__class__):
407 if attr in skip_slots:
408 continue
409 try:
410 if hasattr(old, attr):
411 setattr(new, attr, getattr(old, attr))
412 elif hasattr(new, attr):
413 delattr(new, attr)
414 # There is no way to distinguish getsetdescriptors that allow
415 # writes from ones that don't (I think?), so we just ignore
416 # AttributeErrors if we need to.
417 # TODO: What about getsetdescriptors that act like properties???
418 except AttributeError:
419 pass
420
421
422def is_sub_path_normabs(path: str, dir: str) -> bool:

Callers 4

apply_changesMethod · 0.90
visit_func_defMethod · 0.90
fixupMethod · 0.90

Calls 6

hasattrFunction · 0.85
dictClass · 0.85
get_class_descriptorsFunction · 0.85
setattrFunction · 0.85
getattrFunction · 0.85
delattrFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…