|
13 | 13 | // See the Apache Version 2.0 License for specific language governing |
14 | 14 | // permissions and limitations under the License. |
15 | 15 |
|
16 | | -using System.Collections; |
17 | 16 | using System.Collections.Generic; |
18 | 17 | using System.Linq; |
19 | 18 | using Microsoft.Python.Analysis.Analyzer.Evaluation; |
20 | | -using Microsoft.Python.Analysis.Diagnostics; |
21 | 19 | using Microsoft.Python.Analysis.Types; |
22 | 20 | using Microsoft.Python.Analysis.Values; |
23 | 21 | using Microsoft.Python.Core; |
24 | | -using Microsoft.Python.Parsing; |
25 | 22 | using Microsoft.Python.Parsing.Ast; |
26 | 23 |
|
27 | 24 | namespace Microsoft.Python.Analysis.Analyzer.Symbols { |
@@ -53,12 +50,22 @@ public void EvaluateClass() { |
53 | 50 | EvaluateInnerClasses(_classDef); |
54 | 51 |
|
55 | 52 | _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 | + } |
59 | 64 | _class.SetBases(bases); |
| 65 | + |
60 | 66 | // Declare __class__ variable in the scope. |
61 | 67 | Eval.DeclareVariable("__class__", _class, VariableSource.Declaration); |
| 68 | + |
62 | 69 | ProcessClassBody(); |
63 | 70 | } |
64 | 71 | } |
@@ -111,47 +118,6 @@ private void ProcessClassBody() { |
111 | 118 | UpdateClassMembers(); |
112 | 119 | } |
113 | 120 |
|
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 | | - |
155 | 121 | private void EvaluateConstructors(ClassDefinition cd) { |
156 | 122 | // Do not use foreach since walker list is dynamically modified and walkers are removed |
157 | 123 | // after processing. Handle __init__ and __new__ first so class variables are initialized. |
@@ -186,17 +152,6 @@ private void UpdateClassMembers() { |
186 | 152 | _class.AddMembers(members, false); |
187 | 153 | } |
188 | 154 |
|
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 | | - |
200 | 155 | // Classes and functions are walked by their respective evaluators |
201 | 156 | public override bool Walk(ClassDefinition node) => false; |
202 | 157 | public override bool Walk(FunctionDefinition node) => false; |
|
0 commit comments