private IPythonType CreateNamedTuple(IReadOnlyList <IMember> typeArgs, IPythonModule declaringModule, IndexSpan indexSpan)
        {
            if (typeArgs.Count != 2)
            {
                // TODO: report wrong number of arguments
                return(Interpreter.UnknownType);
            }

            ;
            if (!typeArgs[0].TryGetConstant <string>(out var tupleName) || string.IsNullOrEmpty(tupleName))
            {
                // TODO: report name is incorrect.
                return(Interpreter.UnknownType);
            }

            var argList = (typeArgs[1] as IPythonCollection)?.Contents;

            if (argList == null)
            {
                // TODO: report type spec is not a list.
                return(Interpreter.UnknownType);
            }

            var itemNames = new List <string>();
            var itemTypes = new List <IPythonType>();

            foreach (var a in argList)
            {
                if (a.TryGetConstant(out string itemName1))
                {
                    // Not annotated
                    itemNames.Add(itemName1);
                    itemTypes.Add(Interpreter.UnknownType);
                    continue;
                }

                // Now assume annotated pair that comes as a tuple.
                if (!(a is IPythonCollection c) || c.Type.TypeId != BuiltinTypeId.Tuple)
                {
                    // TODO: report that item is not a tuple.
                    continue;
                }
                if (c.Contents.Count != 2)
                {
                    // TODO: report extra items in the element spec.
                    continue;
                }
                if (!c.Contents[0].TryGetConstant <string>(out var itemName2))
                {
                    // TODO: report item name is not a string.
                    continue;
                }

                itemNames.Add(itemName2);
                itemTypes.Add(c.Contents[1].GetPythonType());
            }
            return(TypingTypeFactory.CreateNamedTupleType(tupleName, itemNames, itemTypes, declaringModule, indexSpan));
        }
 private IPythonType CreateType(IReadOnlyList <IPythonType> typeArgs)
 {
     if (typeArgs.Count == 1)
     {
         return(TypingTypeFactory.CreateType(this, typeArgs[0]));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateUnion(IReadOnlyList <IMember> typeArgs)
 {
     if (typeArgs.Count > 0)
     {
         return(TypingTypeFactory.CreateUnionType(Interpreter, typeArgs.Select(a => a.GetPythonType()).ToArray(), this));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateDictionary(string typeName, IReadOnlyList <IPythonType> typeArgs, bool isMutable)
 {
     if (typeArgs.Count == 2)
     {
         return(TypingTypeFactory.CreateDictionary(Interpreter, typeName, typeArgs[0], typeArgs[1], isMutable));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateListType(string typeName, BuiltinTypeId typeId, IReadOnlyList <IPythonType> typeArgs, bool isMutable)
 {
     if (typeArgs.Count == 1)
     {
         return(TypingTypeFactory.CreateListType(Interpreter, typeName, typeId, typeArgs[0], isMutable));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateItemsViewType(IReadOnlyList <IPythonType> typeArgs)
 {
     if (typeArgs.Count == 2)
     {
         // If argument is generic type parameter then this is still a generic specification
         return(typeArgs.Any(a => a is IGenericTypeParameter)
             ? ToGenericTemplate("ItemsView", typeArgs.OfType <IGenericTypeParameter>().ToArray(), BuiltinTypeId.ListIterator)
             : TypingTypeFactory.CreateItemsViewType(Interpreter, typeArgs[0], typeArgs[1]));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateValuesViewType(IReadOnlyList <IPythonType> typeArgs)
 {
     if (typeArgs.Count == 1)
     {
         // If argument is generic type parameter then this is still a generic specification
         return(typeArgs[0] is IGenericTypeParameter
             ? ToGenericTemplate("ValuesView", typeArgs, BuiltinTypeId.ListIterator)
             : TypingTypeFactory.CreateValuesViewType(Interpreter, typeArgs[0]));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateDictionary(string typeName, IReadOnlyList <IPythonType> typeArgs, bool isMutable)
 {
     if (typeArgs.Count == 2)
     {
         // If argument is generic type parameter then this is still a generic specification
         return(typeArgs.Any(a => a is IGenericTypeParameter)
             ? ToGenericTemplate(typeName, typeArgs, BuiltinTypeId.Dict)
             : TypingTypeFactory.CreateDictionary(Interpreter, typeName, typeArgs[0], typeArgs[1], isMutable));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateListType(string typeName, BuiltinTypeId typeId, IReadOnlyList <IPythonType> typeArgs, bool isMutable)
 {
     if (typeArgs.Count == 1)
     {
         // If argument is generic type parameter then this is still a generic specification
         // except instead of 'List' as in 'from typing import List' it is a template
         // like in 'class A(Generic[T], List[T])
         return(typeArgs[0] is IGenericTypeParameter
             ? ToGenericTemplate(typeName, typeArgs, BuiltinTypeId.List)
             : TypingTypeFactory.CreateListType(Interpreter, typeName, typeId, typeArgs[0], isMutable));
     }
     // TODO: report wrong number of arguments
     return(Interpreter.UnknownType);
 }
 private IPythonType CreateTupleType(IReadOnlyList <IPythonType> typeArgs)
 => typeArgs.Any(a => a is IGenericTypeParameter)
         ? ToGenericTemplate("Tuple", typeArgs, BuiltinTypeId.Tuple)
         : TypingTypeFactory.CreateTupleType(Interpreter, typeArgs);
Esempio n. 11
0
 private IPythonType CreateTupleType(IReadOnlyList <IPythonType> typeArgs)
 => typeArgs.Any(a => a is IGenericTypeDefinition)
         ? ToGenericTemplate("Tuple", typeArgs.OfType <IGenericTypeDefinition>().ToArray(), BuiltinTypeId.Tuple)
         : TypingTypeFactory.CreateTupleType(Interpreter, typeArgs);
 private IPythonType CreateTupleType(IReadOnlyList <IPythonType> typeArgs)
 => TypingTypeFactory.CreateTupleType(Interpreter, typeArgs);