Пример #1
0
        public override IPythonType MakeGeneric(IPythonType baseType, IReadOnlyList <IPythonType> args)
        {
            if (args == null || args.Count == 0 || baseType == null)
            {
                return(baseType);
            }

            if (!AstTypingModule.IsTypingType(baseType) && !(baseType is NameType))
            {
                return(baseType);
            }

            switch (baseType.Name)
            {
            case "Tuple":
            case "Sequence":
                return(MakeSequenceType(BuiltinTypeId.Tuple, BuiltinTypeId.TupleIterator, args));

            case "List":
                return(MakeSequenceType(BuiltinTypeId.List, BuiltinTypeId.ListIterator, args));

            case "Set":
                return(MakeSequenceType(BuiltinTypeId.Set, BuiltinTypeId.SetIterator, args));

            case "Iterable":
                return(MakeIterableType(args));

            case "Iterator":
                return(MakeIteratorType(args));

            case "Dict":
            case "Mapping":
                return(MakeLookupType(BuiltinTypeId.Dict, args));

            case "Optional":
                return(Finalize(args.FirstOrDefault()) ?? _scope._unknownType);

            case "Union":
                return(MakeUnion(args));

            case "ByteString":
                return(_scope.Interpreter.GetBuiltinType(BuiltinTypeId.Bytes));

            case "Type":
                return(_scope.Interpreter.GetBuiltinType(BuiltinTypeId.Type));

            case "Any":
                return(baseType);

            // TODO: Other types
            default:
                Trace.TraceWarning("Unhandled generic: typing.{0}", baseType.Name);
                break;
            }

            return(baseType);
        }
        private IMember GetValueFromIndex(IndexExpression expr, LookupOptions options)
        {
            if (expr == null || expr.Target == null)
            {
                return(null);
            }

            if (expr.Index is SliceExpression || expr.Index is TupleExpression)
            {
                // When slicing, assume result is the same type
                return(GetValueFromExpression(expr.Target));
            }

            var type = GetTypeFromValue(GetValueFromExpression(expr.Target));

            if (type != null && type != _unknownType)
            {
                if (AstTypingModule.IsTypingType(type))
                {
                    return(type);
                }

                switch (type.TypeId)
                {
                case BuiltinTypeId.Bytes:
                    if (Ast.LanguageVersion.Is3x())
                    {
                        return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Int), GetLoc(expr)));
                    }
                    else
                    {
                        return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Bytes), GetLoc(expr)));
                    }

                case BuiltinTypeId.Unicode:
                    return(new AstPythonConstant(Interpreter.GetBuiltinType(BuiltinTypeId.Unicode), GetLoc(expr)));
                }

                if (type.MemberType == PythonMemberType.Class)
                {
                    // When indexing into a type, assume result is the type
                    // TODO: Proper generic handling
                    return(type);
                }

                _log?.Log(TraceLevel.Verbose, "UnknownIndex", type.TypeId, expr.ToCodeString(Ast, CodeFormattingOptions.Traditional).Trim());
            }
            else
            {
                _log?.Log(TraceLevel.Verbose, "UnknownIndex", expr.ToCodeString(Ast, CodeFormattingOptions.Traditional).Trim());
            }
            return(new AstPythonConstant(_unknownType, GetLoc(expr)));
        }