Beispiel #1
0
        public IMember ConstructMember(string qualifiedName)
        {
            // Determine module name, member chain and if this is an instance.
            if (!TypeNames.DeconstructQualifiedName(qualifiedName, out var parts))
            {
                return(null);
            }

            // See if member is a module first.
            var module = GetModule(parts);

            if (module == null)
            {
                return(null);
            }

            var member = parts.ModuleName == Module.Name
                    ? GetMemberFromThisModule(parts.MemberNames)
                    : GetMemberFromModule(module, parts.MemberNames);

            if (parts.ObjectType != ObjectType.Instance)
            {
                return(member);
            }

            var t = member.GetPythonType() ?? module.Interpreter.UnknownType;

            return(new PythonInstance(t));
        }
Beispiel #2
0
        private IReadOnlyList <IPythonType> GetTypeArguments(string memberName, out string typeName)
        {
            typeName = null;
            // TODO: better handle generics.
            // https://github.com/microsoft/python-language-server/issues/1215
            // Determine generic type arguments, if any, so we can construct
            // complex types from parts, such as Union[typing.Any, a.b.c].
            var typeArgs    = new List <IPythonType>();
            var openBracket = memberName.IndexOf('[');

            if (openBracket > 0)
            {
                var closeBracket = memberName.LastIndexOf(']');
                if (closeBracket > 0)
                {
                    var argumentString = memberName.Substring(openBracket + 1, closeBracket - openBracket - 1);
                    // Extract type names from argument string. Note that types themselves
                    // can have arguments: Union[int, Union[int, Union[str, bool]], ...].
                    var qualifiedNames = TypeNames.GetTypeNames(argumentString, ',');
                    foreach (var qn in qualifiedNames)
                    {
                        var t = ConstructType(qn);
                        if (t == null)
                        {
                            TypeNames.DeconstructQualifiedName(qn, out var parts);
                            typeName = string.Join(".", parts.MemberNames);
                            t        = new GenericTypeParameter(typeName, Module, Array.Empty <IPythonType>(), null, null, null, default);
                        }
                        typeArgs.Add(t);
                    }
                    typeName = memberName.Substring(0, openBracket);
                }
            }
            return(typeArgs);
        }