Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/Analysis/Ast/Impl/Analyzer/Symbols/FunctionEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override void Evaluate() {
// Do process body of constructors since they may be declaring
// variables that are later used to determine return type of other
// methods and properties.
var ctor = _function.Name.EqualsOrdinal("__init__") || _function.Name.EqualsOrdinal("__new__");
var ctor = _function.IsDunderInit() || _function.IsDunderNew();
if (ctor || returnType.IsUnknown() || Module.ModuleType == ModuleType.User) {
// Return type from the annotation is sufficient for libraries and stubs, no need to walk the body.
FunctionDefinition.Body?.Walk(this);
Expand Down Expand Up @@ -119,10 +119,8 @@ public override bool Walk(ReturnStatement node) {
var value = Eval.GetValueFromExpression(node.Expression);
if (value != null) {
// although technically legal, __init__ in a constructor should not have a not-none return value
if (FunctionDefinition.Name.EqualsOrdinal("__init__") && _function.DeclaringType.MemberType == PythonMemberType.Class
&& !value.IsOfType(BuiltinTypeId.NoneType)) {

Eval.ReportDiagnostics(Module.Uri, new Diagnostics.DiagnosticsEntry(
if (_function.IsDunderInit() && !value.IsOfType(BuiltinTypeId.NoneType)) {
Eval.ReportDiagnostics(Module.Uri, new DiagnosticsEntry(
Resources.ReturnInInit,
node.GetLocation(Eval).Span,
ErrorCodes.ReturnInInit,
Expand Down
13 changes: 13 additions & 0 deletions src/Analysis/Ast/Impl/Extensions/FunctionDefinitionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Microsoft.Python.Analysis.Types;

namespace Microsoft.Python.Analysis {
public static class ClassMemberExtensions {
public static bool IsDunderInit(this IPythonClassMember member) {
return member.Name == "__init__" && member.DeclaringType?.MemberType == PythonMemberType.Class;
}

public static bool IsDunderNew(this IPythonClassMember member) {
return member.Name == "__new__" && member.DeclaringType?.MemberType == PythonMemberType.Class;
}
}
}
3 changes: 0 additions & 3 deletions src/Analysis/Ast/Impl/Extensions/PythonTypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,5 @@ public static void TransferDocumentationAndLocation(this IPythonType s, IPythonT
dst.Location = src.Location;
}
}

public static bool IsConstructor(this IPythonClassMember m)
=> m.Name.EqualsOrdinal("__init__") || m.Name.EqualsOrdinal("__new__");
}
}