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

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
29 changes: 20 additions & 9 deletions src/Analysis/Ast/Impl/Analyzer/Symbols/ClassEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
using System.Linq;
using Microsoft.Python.Analysis.Analyzer.Evaluation;
using Microsoft.Python.Analysis.Diagnostics;
using Microsoft.Python.Analysis.Modules;
using Microsoft.Python.Analysis.Specializations.Typing;
using Microsoft.Python.Analysis.Types;
using Microsoft.Python.Analysis.Values;
using Microsoft.Python.Core;
Expand Down Expand Up @@ -117,17 +119,26 @@ private IEnumerable<IPythonType> ProcessBases(Scope outerScope) {
var expr = a.Expression;
var m = Eval.GetValueFromExpression(expr);

switch (m?.MemberType) {
case PythonMemberType.Method:
case PythonMemberType.Function:
case PythonMemberType.Property:
case PythonMemberType.Instance:
case PythonMemberType.Variable when m is IPythonConstant:
// all invalid types to inherit from
ReportInvalidBase(a.ToCodeString(Eval.Ast, CodeFormattingOptions.Traditional));
switch (m) {
// Allow any members from typing module
// TODO handle typing module specialization better: https://github.com/microsoft/python-language-server/issues/1367
case ILocatedMember l when l.DeclaringModule is TypingModule:
TryAddBase(bases, a);
break;
default:
TryAddBase(bases, a);
switch (m?.MemberType) {
case PythonMemberType.Method:
case PythonMemberType.Function:
case PythonMemberType.Property:
case PythonMemberType.Instance:
case PythonMemberType.Variable when m is IPythonConstant:
// all invalid types to inherit from
ReportInvalidBase(a.ToCodeString(Eval.Ast, CodeFormattingOptions.Traditional));
break;
default:
TryAddBase(bases, a);
break;
}
break;
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/Analysis/Ast/Test/LintInheritNonClassTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,6 @@ def method(self):
}


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

[TestMethod, Priority(0)]
public async Task InheritFromTypingModuleNamedTuple() {
const string code = @"
from typing import NamedTuple

class X(NamedTuple):
y: str
";
var analysis = await GetAnalysisAsync(code);
analysis.Diagnostics.Should().BeEmpty();
}

[TestMethod, Priority(0)]
public async Task InheritFromOtherModule() {
var module1Code = @"
Expand Down