MCPcopy Index your code
hub / github.com/lutzroeder/netron / subscriptToType

Method subscriptToType

source/python.js:11971–12029  ·  view source on GitHub ↗
(typeName, subscript)

Source from the content-addressed store, hash-verified

11969 return this.parseTypeFromExpr(expr.body[0].value);
11970 }
11971 subscriptToType(typeName, subscript) {
11972 if (typeName === 'Tuple' || typeName === 'tuple') {
11973 const subscript_expr_types = [];
11974 const elts = subscript.slice instanceof ast.Tuple ? subscript.slice.elts : [subscript.slice];
11975 for (const expr of elts) {
11976 subscript_expr_types.push(this.parseTypeFromExprImpl(expr));
11977 }
11978 return torch.TupleType.create(subscript_expr_types);
11979 } else if (typeName === 'List' || typeName === 'list') {
11980 if (subscript.slice instanceof ast.Slice || subscript.slice instanceof ast.Tuple) {
11981 throw new python.Error('List type must have exactly one element type.');
11982 }
11983 const elem_type = this.parseTypeFromExprImpl(subscript.slice);
11984 return torch.ListType.create(elem_type);
11985 } else if (typeName === 'Optional') {
11986 if (subscript.slice instanceof ast.Slice || subscript.slice instanceof ast.Tuple) {
11987 throw new python.Error('Optional type must have exactly one element type.');
11988 }
11989 const elem_type = this.parseTypeFromExprImpl(subscript.slice);
11990 return torch.OptionalType.create(elem_type);
11991 } else if (typeName === 'Union') {
11992 const subscript_expr_types = [];
11993 const elts = subscript.slice instanceof ast.Tuple ? subscript.slice.elts : [subscript.slice];
11994 for (const expr of elts) {
11995 subscript_expr_types.push(this.parseTypeFromExprImpl(expr));
11996 }
11997 return torch.UnionType.create(subscript_expr_types);
11998 } else if (typeName === 'Future' || typeName === 'torch.jit.Future') {
11999 const elts = subscript.slice instanceof ast.Tuple ? subscript.slice.elts : [subscript.slice];
12000 if (elts.length !== 1) {
12001 throw new python.Error('Future type must have exactly one element type.');
12002 }
12003 const elem_type = this.parseTypeFromExprImpl(elts[0]);
12004 return torch.FutureType.create(elem_type);
12005 } else if (typeName === 'Await' || typeName === 'torch.jit._Await') {
12006 const elts = subscript.slice instanceof ast.Tuple ? subscript.slice.elts : [subscript.slice];
12007 if (elts.length !== 1) {
12008 throw new python.Error('Await type must have exactly one element type.');
12009 }
12010 const elem_type = this.parseTypeFromExprImpl(elts[0]);
12011 return torch.AwaitType.create(elem_type);
12012 } else if (typeName === 'RRef') {
12013 const elts = subscript.slice instanceof ast.Tuple ? subscript.slice.elts : [subscript.slice];
12014 if (elts.length !== 1) {
12015 throw new python.Error('RRef type must have exactly one element type.');
12016 }
12017 const elem_type = this.parseTypeFromExprImpl(elts[0]);
12018 return torch.RRefType.create(elem_type);
12019 } else if (typeName === 'Dict' || typeName === 'dict') {
12020 const elts = subscript.slice instanceof ast.Tuple ? subscript.slice.elts : [subscript.slice];
12021 if (elts.length !== 2) {
12022 throw new python.Error('Dict type must have exactly two element types.');
12023 }
12024 const key_type = this.parseTypeFromExprImpl(elts[0]);
12025 const value_type = this.parseTypeFromExprImpl(elts[1]);
12026 return torch.DictType.create(key_type, value_type);
12027 }
12028 throw new python.Error(`Unknown type constructor '${typeName}'.`);

Callers 1

parseTypeFromExprImplMethod · 0.80

Calls 3

parseTypeFromExprImplMethod · 0.80
pushMethod · 0.45
createMethod · 0.45

Tested by

no test coverage detected