public string GetReturnDocumentation(IPythonType self = null) { if (self == null) { return(_returnDocumentation); } var returnType = StaticReturnValue.GetPythonType(); switch (returnType) { case PythonClassType cls when cls.IsGeneric(): { // -> A[_T1, _T2, ...] // Match arguments var typeArgs = cls.GenericParameters.Keys .Select(n => cls.GenericParameters.TryGetValue(n, out var t) ? t : null) .ExcludeDefault() .ToArray(); var specificReturnValue = cls.CreateSpecificType(new ArgumentSet(typeArgs)); return(specificReturnValue.Name); } case IGenericTypeDefinition gtp1 when self is IPythonClassType cls: { // -> _T if (cls.GenericParameters.TryGetValue(gtp1.Name, out var specificType)) { return(specificType.Name); } // Try returning the constraint // TODO: improve this, the heuristic is pretty basic and tailored to simple func(_T) -> _T var name = StaticReturnValue.GetPythonType()?.Name; var typeDefVar = DeclaringModule.Analysis.GlobalScope.Variables[name]; if (typeDefVar?.Value is IGenericTypeDefinition gtp2) { var t = gtp2.Constraints.FirstOrDefault(); if (t != null) { return(t.Name); } } break; } } return(_returnDocumentation); }
public string GetReturnDocumentation(IPythonType self = null) { if (self != null) { var returnType = GetSpecificReturnType(self as IPythonClassType, null); if (!returnType.IsUnknown()) { return(returnType.GetPythonType().Name); } } // Use annotation value if it is there if (_fromAnnotation && !StaticReturnValue.IsUnknown()) { return(StaticReturnValue.GetPythonType().Name); } return(_returnDocumentation); }
internal void AddReturnValue(IMember value) { if (value.IsUnknown()) { return; // Don't add useless values. } if (StaticReturnValue.IsUnknown()) { SetReturnValue(value, false); return; } // If return value is set from annotation, it should not be changing. var currentType = StaticReturnValue.GetPythonType(); var valueType = value.GetPythonType(); if (!_fromAnnotation && !currentType.Equals(valueType)) { var type = PythonUnionType.Combine(currentType, valueType); // Track instance vs type info. StaticReturnValue = value is IPythonInstance ? new PythonInstance(type) : (IMember)type; } }