Example #1
0
        public IPythonType GetTypeFromString(string typeString)
        {
            // Type alone is not a valid syntax, so we need to simulate the annotation.
            typeString = $"x: {typeString}";
            using (var sr = new StringReader(typeString)) {
                var sink   = new CollectingErrorSink();
                var parser = Parser.CreateParser(sr, Module.Interpreter.LanguageVersion, new ParserOptions {
                    ErrorSink = sink
                });
                var ast           = parser.ParseFile();
                var exprStatement = (ast?.Body as SuiteStatement)?.Statements?.FirstOrDefault() as ExpressionStatement;
                if (!(Statement.GetExpression(exprStatement) is ExpressionWithAnnotation annExpr) || sink.Errors.Count > 0)
                {
                    return(null);
                }

                var ann   = new TypeAnnotation(Ast.LanguageVersion, annExpr.Annotation);
                var value = ann.GetValue(new TypeAnnotationConverter(this));
                var t     = value.GetPythonType();
                if (!t.IsUnknown())
                {
                    return(t);
                }
            }
            return(null);
        }
Example #2
0
        public void Walk()
        {
            var self = GetSelf();

            _selfType = (self as AstPythonConstant)?.Type as AstPythonType;

            if (_target.ReturnAnnotation != null)
            {
                var retAnn = new TypeAnnotation(_scope.Ast.LanguageVersion, _target.ReturnAnnotation);
                var m      = retAnn.GetValue(new AstTypeAnnotationConverter(_scope));
                if (m is IPythonMultipleMembers mm)
                {
                    _returnTypes.AddRange(mm.Members.OfType <IPythonType>());
                }
                else if (m is IPythonType type)
                {
                    _returnTypes.Add(type);
                }
            }

            _scope.PushScope();
            if (self != null)
            {
                var p0 = _target.ParametersInternal?.FirstOrDefault();
                if (p0 != null && !string.IsNullOrEmpty(p0.Name))
                {
                    _scope.SetInScope(p0.Name, self);
                }
            }
            _target.Walk(this);
            _scope.PopScope();
        }
Example #3
0
        public IPythonType GetTypeFromAnnotation(Expression expr, out bool isGeneric, LookupOptions options = LookupOptions.Global | LookupOptions.Builtins)
        {
            isGeneric = false;
            switch (expr)
            {
            case null:
                return(null);

            case CallExpression callExpr:
                // x: NamedTuple(...)
                return(GetValueFromCallable(callExpr)?.GetPythonType() ?? UnknownType);

            case IndexExpression indexExpr:
                // Try generics
                var target = GetValueFromExpression(indexExpr.Target);
                var result = GetValueFromGeneric(target, indexExpr);
                if (result != null)
                {
                    isGeneric = true;
                    return(result.GetPythonType());
                }
                break;
            }

            // Look at specialization and typing first
            var ann = new TypeAnnotation(Ast.LanguageVersion, expr);

            return(ann.GetValue(new TypeAnnotationConverter(this, options)));
        }
Example #4
0
        public void Walk()
        {
            IMember self = null;
            bool    classmethod, staticmethod;

            GetMethodType(_target, out classmethod, out staticmethod);
            if (!staticmethod)
            {
                self = _scope.LookupNameInScopes("__class__", NameLookupContext.LookupOptions.Local);
                if (!classmethod)
                {
                    var cls = self as IPythonType;
                    if (cls == null)
                    {
                        self = null;
                    }
                    else
                    {
                        self = new AstPythonConstant(cls, ((cls as ILocatedMember)?.Locations).MaybeEnumerate().ToArray());
                    }
                }
            }

            if (_target.ReturnAnnotation != null)
            {
                var retAnn = new TypeAnnotation(_scope.Ast.LanguageVersion, _target.ReturnAnnotation);
                var m      = retAnn.GetValue(new AstTypeAnnotationConverter(_scope));
                if (m is IPythonMultipleMembers mm)
                {
                    _returnTypes.AddRange(mm.Members.OfType <IPythonType>());
                }
                else if (m is IPythonType type)
                {
                    _returnTypes.Add(type);
                }
            }

            _scope.PushScope();
            if (self != null)
            {
                var p0 = _target.ParametersInternal?.FirstOrDefault();
                if (p0 != null && !string.IsNullOrEmpty(p0.Name))
                {
                    _scope.SetInScope(p0.Name, self);
                }
            }
            _target.Walk(this);
            _scope.PopScope();
        }
Example #5
0
        public async Task <IPythonType> GetTypeFromAnnotationAsync(Expression expr, CancellationToken cancellationToken = default, LookupOptions options = LookupOptions.Global | LookupOptions.Builtins)
        {
            if (expr == null)
            {
                return(null);
            }

            if (expr is CallExpression callExpr)
            {
                // x: NamedTuple(...)
                return((await GetValueFromCallableAsync(callExpr, cancellationToken))?.GetPythonType() ?? UnknownType);
            }
            // Look at specialization and typing first
            var ann = new TypeAnnotation(Ast.LanguageVersion, expr);

            return(ann.GetValue(new TypeAnnotationConverter(this, options)));
        }
        public IEnumerable <IPythonType> GetTypesFromAnnotation(Expression expr)
        {
            if (expr == null)
            {
                return(Enumerable.Empty <IPythonType>());
            }

            var ann = new TypeAnnotation(Ast.LanguageVersion, expr);
            var m   = ann.GetValue(new AstTypeAnnotationConverter(this));

            if (m is IPythonMultipleMembers mm)
            {
                return(mm.Members.OfType <IPythonType>());
            }
            else if (m is IPythonType type)
            {
                return(Enumerable.Repeat(type, 1));
            }

            return(Enumerable.Empty <IPythonType>());
        }