Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c0074b

Browse files
authored
Fixing for extending member from typing module (microsoft#1366)
* Fixing for extending a member from typing module
1 parent f0f325c commit 0c0074b

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
using System.Linq;
1919
using Microsoft.Python.Analysis.Analyzer.Evaluation;
2020
using Microsoft.Python.Analysis.Diagnostics;
21+
using Microsoft.Python.Analysis.Modules;
22+
using Microsoft.Python.Analysis.Specializations.Typing;
2123
using Microsoft.Python.Analysis.Types;
2224
using Microsoft.Python.Analysis.Values;
2325
using Microsoft.Python.Core;
@@ -117,17 +119,26 @@ private IEnumerable<IPythonType> ProcessBases(Scope outerScope) {
117119
var expr = a.Expression;
118120
var m = Eval.GetValueFromExpression(expr);
119121

120-
switch (m?.MemberType) {
121-
case PythonMemberType.Method:
122-
case PythonMemberType.Function:
123-
case PythonMemberType.Property:
124-
case PythonMemberType.Instance:
125-
case PythonMemberType.Variable when m is IPythonConstant:
126-
// all invalid types to inherit from
127-
ReportInvalidBase(a.ToCodeString(Eval.Ast, CodeFormattingOptions.Traditional));
122+
switch (m) {
123+
// Allow any members from typing module
124+
// TODO handle typing module specialization better: https://github.com/microsoft/python-language-server/issues/1367
125+
case ILocatedMember l when l.DeclaringModule is TypingModule:
126+
TryAddBase(bases, a);
128127
break;
129128
default:
130-
TryAddBase(bases, a);
129+
switch (m?.MemberType) {
130+
case PythonMemberType.Method:
131+
case PythonMemberType.Function:
132+
case PythonMemberType.Property:
133+
case PythonMemberType.Instance:
134+
case PythonMemberType.Variable when m is IPythonConstant:
135+
// all invalid types to inherit from
136+
ReportInvalidBase(a.ToCodeString(Eval.Ast, CodeFormattingOptions.Traditional));
137+
break;
138+
default:
139+
TryAddBase(bases, a);
140+
break;
141+
}
131142
break;
132143
}
133144
}

src/Analysis/Ast/Test/LintInheritNonClassTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,6 @@ def method(self):
146146
}
147147

148148

149-
/// <summary>
150-
/// Because typing module is specialized with functions instead of classes,
151-
/// we think that we are extending a function instead of a class so we would erroneously
152-
/// give a diagnostic message
153-
/// </summary>
154-
/// <returns></returns>
155-
[Ignore]
156149
[TestMethod, Priority(0)]
157150
public async Task InheritFromTypingModule() {
158151
const string code = @"
@@ -166,6 +159,18 @@ def method(self):
166159
analysis.Diagnostics.Should().BeEmpty();
167160
}
168161

162+
[TestMethod, Priority(0)]
163+
public async Task InheritFromTypingModuleNamedTuple() {
164+
const string code = @"
165+
from typing import NamedTuple
166+
167+
class X(NamedTuple):
168+
y: str
169+
";
170+
var analysis = await GetAnalysisAsync(code);
171+
analysis.Diagnostics.Should().BeEmpty();
172+
}
173+
169174
[TestMethod, Priority(0)]
170175
public async Task InheritFromOtherModule() {
171176
var module1Code = @"

0 commit comments

Comments
 (0)