Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Commit 8494a48

Browse files
author
Mikhail Arkhipov
committed
Revert "When a class inherits from something that is not a class, give a diagnostic error (#1277)"
This reverts commit 39d7437.
1 parent ace090e commit 8494a48

File tree

6 files changed

+14
-248
lines changed

6 files changed

+14
-248
lines changed

src/Analysis/Ast/Impl/Analyzer/Symbols/ClassEvaluator.cs

Lines changed: 13 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@
1313
// See the Apache Version 2.0 License for specific language governing
1414
// permissions and limitations under the License.
1515

16-
using System.Collections;
1716
using System.Collections.Generic;
1817
using System.Linq;
1918
using Microsoft.Python.Analysis.Analyzer.Evaluation;
20-
using Microsoft.Python.Analysis.Diagnostics;
2119
using Microsoft.Python.Analysis.Types;
2220
using Microsoft.Python.Analysis.Values;
2321
using Microsoft.Python.Core;
24-
using Microsoft.Python.Parsing;
2522
using Microsoft.Python.Parsing.Ast;
2623

2724
namespace Microsoft.Python.Analysis.Analyzer.Symbols {
@@ -53,12 +50,22 @@ public void EvaluateClass() {
5350
EvaluateInnerClasses(_classDef);
5451

5552
_class = classInfo;
56-
57-
var bases = ProcessBases(outerScope);
58-
53+
// Set bases to the class.
54+
var bases = new List<IPythonType>();
55+
foreach (var a in _classDef.Bases.Where(a => string.IsNullOrEmpty(a.Name))) {
56+
// We cheat slightly and treat base classes as annotations.
57+
var b = Eval.GetTypeFromAnnotation(a.Expression);
58+
if (b != null) {
59+
var t = b.GetPythonType();
60+
bases.Add(t);
61+
t.AddReference(Eval.GetLocationOfName(a.Expression));
62+
}
63+
}
5964
_class.SetBases(bases);
65+
6066
// Declare __class__ variable in the scope.
6167
Eval.DeclareVariable("__class__", _class, VariableSource.Declaration);
68+
6269
ProcessClassBody();
6370
}
6471
}
@@ -111,47 +118,6 @@ private void ProcessClassBody() {
111118
UpdateClassMembers();
112119
}
113120

114-
private IEnumerable<IPythonType> ProcessBases(Scope outerScope) {
115-
var bases = new List<IPythonType>();
116-
foreach (var a in _classDef.Bases.Where(a => string.IsNullOrEmpty(a.Name))) {
117-
var expr = a.Expression;
118-
119-
switch (expr) {
120-
// class declared in the current module
121-
case NameExpression nameExpression:
122-
var name = Eval.GetInScope(nameExpression.Name, outerScope);
123-
switch (name) {
124-
case PythonClassType classType:
125-
bases.Add(classType);
126-
break;
127-
case IPythonConstant constant:
128-
ReportInvalidBase(constant.Value);
129-
break;
130-
default:
131-
TryAddBase(bases, a);
132-
break;
133-
}
134-
break;
135-
default:
136-
TryAddBase(bases, a);
137-
break;
138-
}
139-
}
140-
return bases;
141-
}
142-
143-
private void TryAddBase(List<IPythonType> bases, Arg arg) {
144-
// We cheat slightly and treat base classes as annotations.
145-
var b = Eval.GetTypeFromAnnotation(arg.Expression);
146-
if (b != null) {
147-
var t = b.GetPythonType();
148-
bases.Add(t);
149-
t.AddReference(Eval.GetLocationOfName(arg.Expression));
150-
} else {
151-
ReportInvalidBase(arg.ToCodeString(Eval.Ast, CodeFormattingOptions.Traditional));
152-
}
153-
}
154-
155121
private void EvaluateConstructors(ClassDefinition cd) {
156122
// Do not use foreach since walker list is dynamically modified and walkers are removed
157123
// after processing. Handle __init__ and __new__ first so class variables are initialized.
@@ -186,17 +152,6 @@ private void UpdateClassMembers() {
186152
_class.AddMembers(members, false);
187153
}
188154

189-
private void ReportInvalidBase(object argVal) {
190-
Eval.ReportDiagnostics(Eval.Module.Uri,
191-
new DiagnosticsEntry(
192-
Resources.InheritNonClass.FormatInvariant(argVal),
193-
_classDef.NameExpression.GetLocation(Eval)?.Span ?? default,
194-
Diagnostics.ErrorCodes.InheritNonClass,
195-
Severity.Error,
196-
DiagnosticSource.Analysis
197-
));
198-
}
199-
200155
// Classes and functions are walked by their respective evaluators
201156
public override bool Walk(ClassDefinition node) => false;
202157
public override bool Walk(FunctionDefinition node) => false;

src/Analysis/Ast/Impl/Diagnostics/ErrorCodes.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ public static class ErrorCodes {
2929
public const string ReturnInInit = "return-in-init";
3030
public const string TypingNewTypeArguments = "typing-newtype-arguments";
3131
public const string TypingGenericArguments = "typing-generic-arguments";
32-
public const string InheritNonClass = "inherit-non-class";
3332
}
3433
}

src/Analysis/Ast/Impl/Resources.Designer.cs

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Analysis/Ast/Impl/Resources.resx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,4 @@
204204
<data name="GenericNotAllUnique" xml:space="preserve">
205205
<value>Arguments to Generic must all be unique.</value>
206206
</data>
207-
<data name="InheritNonClass" xml:space="preserve">
208-
<value>Inheriting '{0}', which is not a class.</value>
209-
</data>
210-
</root>
207+
</root>

src/Analysis/Ast/Impl/Types/PythonClassType.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
using System.Collections.Generic;
1818
using System.Diagnostics;
1919
using System.Linq;
20-
using Microsoft.Python.Analysis.Analyzer;
21-
using Microsoft.Python.Analysis.Diagnostics;
2220
using Microsoft.Python.Analysis.Modules;
2321
using Microsoft.Python.Analysis.Specializations.Typing;
2422
using Microsoft.Python.Analysis.Types.Collections;
@@ -192,7 +190,6 @@ internal void SetBases(IEnumerable<IPythonType> bases) {
192190
}
193191

194192
bases = bases != null ? bases.Where(b => !b.GetPythonType().IsUnknown()).ToArray() : Array.Empty<IPythonType>();
195-
196193
// For Python 3+ attach object as a base class by default except for the object class itself.
197194
if (DeclaringModule.Interpreter.LanguageVersion.Is3x() && DeclaringModule.ModuleType != ModuleType.Builtins) {
198195
var objectType = DeclaringModule.Interpreter.GetBuiltinType(BuiltinTypeId.Object);

src/Analysis/Ast/Test/InheritNonClassTests.cs

Lines changed: 0 additions & 173 deletions
This file was deleted.

0 commit comments

Comments
 (0)