MCPcopy
hub / github.com/lutzroeder/netron / setAttr

Method setAttr

source/python.js:15721–15760  ·  view source on GitHub ↗
(loc, m, field, newValue)

Source from the content-addressed store, hash-verified

15719 throw new python.Error('Object has no attribute or method.');
15720 }
15721 setAttr(loc, m, field, newValue) {
15722 const type = this._value.type();
15723 if (type instanceof torch.ClassType === false) {
15724 throw new python.Error('Cannot set attribute on non-class type.');
15725 }
15726 const classType = type;
15727 let expectedType = classType.findAttribute(field);
15728 if (!expectedType) {
15729 const isInitializing = m.name() === '__init__' && m.graph().inputs().length > 0 && m.graph().inputs()[0].type() === classType;
15730 if (isInitializing) {
15731 if (this.isRecursive(classType, newValue.type())) {
15732 throw new python.Error('Classes that recursively contain instances of themselves are not supported.');
15733 }
15734 classType.addAttribute(field, newValue.type());
15735 expectedType = newValue.type();
15736 const insertPoint = m.graph().insertPoint();
15737 const topLevelBlock = m.graph().block();
15738 if (insertPoint.owningBlock() !== topLevelBlock) {
15739 throw new python.Error('First assignment cannot be in a control-flow block.');
15740 }
15741 } else {
15742 const prop = classType.getProperty(field);
15743 if (prop && prop.setter) {
15744 new torch._C.MethodValue(this._value, prop.setter.name()).call(loc, m, [newValue], [], /*n_binders=*/1);
15745 return;
15746 }
15747 if (prop && !prop.setter) {
15748 throw new python.Error('Tried to set read-only attribute.');
15749 }
15750 throw new python.Error('Tried to set nonexistent attribute.');
15751 }
15752 }
15753 torch._C.AT_ASSERT(expectedType);
15754 const newType = newValue.type();
15755 if (!newType.isSubtypeOf(expectedType)) {
15756 throw new python.Error('Wrong type for attribute assignment.');
15757 }
15758 const g = m.graph();
15759 g.insertNode(g.createSetAttr(this._value, field, newValue));
15760 }
15761 getitem(loc, m, idx, type_hint) {
15762 const val = this.getValue();
15763 const val_type = val.type();

Callers 1

emitSelectAssignMethod · 0.80

Calls 14

findAttributeMethod · 0.80
insertPointMethod · 0.80
owningBlockMethod · 0.80
getPropertyMethod · 0.80
isSubtypeOfMethod · 0.80
insertNodeMethod · 0.80
createSetAttrMethod · 0.80
typeMethod · 0.45
nameMethod · 0.45
inputsMethod · 0.45
graphMethod · 0.45
addAttributeMethod · 0.45

Tested by

no test coverage detected