private static bool TypeVarArgumentsValid(IArgumentSet argSet) { var args = argSet.Arguments; var constraints = argSet.ListArgument?.Values ?? Array.Empty <IMember>(); var eval = argSet.Eval; var expr = argSet.Expression; var callLocation = expr?.GetLocation(eval); if (argSet.Errors.Count > 0) { argSet.ReportErrors(); return(false); } // Report diagnostic if user passed in a value for name and it is not a string var name = (args[0].Value as IPythonConstant)?.GetString(); if (string.IsNullOrEmpty(name)) { eval.ReportDiagnostics( eval.Module.Uri, new DiagnosticsEntry(Resources.TypeVarFirstArgumentNotString, callLocation?.Span ?? default, Diagnostics.ErrorCodes.TypingTypeVarArguments, Severity.Warning, DiagnosticSource.Analysis) ); return(false); } // Python gives runtime error when TypeVar has one constraint // e.g. T = TypeVar('T', int) if (constraints.Count == 1) { eval.ReportDiagnostics( eval.Module.Uri, new DiagnosticsEntry(Resources.TypeVarSingleConstraint, callLocation?.Span ?? default, Diagnostics.ErrorCodes.TypingTypeVarArguments, Severity.Warning, DiagnosticSource.Analysis) ); return(false); } return(true); }
private IPythonType CreateTypeAlias(IArgumentSet argSet) { Check.Argument(nameof(argSet), () => argSet.Arguments.Count == 2); if (!argSet.Errors.IsNullOrEmpty()) { argSet.ReportErrors(); return(Interpreter.UnknownType); } // Get name argument and make sure it is a string string name = null; var nameArg = argSet.Argument <IMember>(0); nameArg?.TryGetConstant(out name); if (name != null) { // Get type argument and create alias var tpArg = argSet.Argument <IMember>(1); return(new TypeAlias(name, tpArg?.GetPythonType() ?? Interpreter.UnknownType)); } // If user provided first argument that is not a string, give diagnostic if (!nameArg.IsUnknown()) { var eval = argSet.Eval; var argExpr = argSet.Arguments[0].ValueExpression; eval.ReportDiagnostics( eval.Module?.Uri, new DiagnosticsEntry(Resources.NewTypeFirstArgument, eval.GetLocation(argExpr).Span, Diagnostics.ErrorCodes.TypingNewTypeArguments, Severity.Warning, DiagnosticSource.Analysis) ); } return(Interpreter.UnknownType); }