Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Better bound/unbound analysis when checking parameter mismatch.

  • Loading branch information...
commit 669112942c689f05c63e003f8c1f5e45e7e8f4c6 1 parent 2b34d27
@fabioz fabioz authored
View
97 .../com.python.pydev.analysis/src/com/python/pydev/analysis/scopeanalysis/AbstractScopeAnalyzerVisitor.java
@@ -131,6 +131,8 @@
private Set<String> builtinTokens = new HashSet<String>();
+ protected boolean analyzeArgumentsMismatch;
+
/**
* Constructor
* @param prefs
@@ -144,6 +146,7 @@ public AbstractScopeAnalyzerVisitor(IPythonNature nature, String moduleName, IMo
this.moduleName = moduleName;
this.document = document;
this.scope = new Scope(this, nature, moduleName);
+ analyzeArgumentsMismatch = false;
if(current instanceof SourceModule){
this.currentLocalScope.getScopeStack().push(((SourceModule) current).getAst());
}
@@ -199,42 +202,78 @@ public Object visitCall(Call callNode) throws Exception {
FunctionDef functionDefinitionReferenced = null;
IToken nameToken = null;
boolean callingBoundMethod = false;
- if (callNode.func != null){
- if(callNode.func instanceof Name){
- Name name = (Name) callNode.func;
- startRecordFound();
- nameToken = (IToken) visitName(name);
- IToken found = popFound();
- if(found instanceof SourceToken){
- SourceToken sourceToken = (SourceToken) found;
- SimpleNode ast = sourceToken.getAst();
- if(ast instanceof FunctionDef){
- functionDefinitionReferenced = (FunctionDef) ast;
-
- }else if(ast instanceof ClassDef){
- ClassDef classDef = (ClassDef) ast;
- String className = ((NameTok)classDef.name).id;
-
- Definition foundDef = sourceToken.getDefinition();
- IModule mod = this.current;
- if(foundDef != null){
- mod = foundDef.module;
+
+ if(!analyzeArgumentsMismatch){
+ callNode.func.accept(this);
+ }else{
+ if (callNode.func != null){
+ if(callNode.func instanceof Name){
+ Name name = (Name) callNode.func;
+ startRecordFound();
+ nameToken = (IToken) visitName(name);
+ IToken found = popFound();
+ if(found instanceof SourceToken){
+ SourceToken sourceToken = (SourceToken) found;
+ SimpleNode ast = sourceToken.getAst();
+ if(ast instanceof FunctionDef){
+ functionDefinitionReferenced = (FunctionDef) ast;
+
+ }else if(ast instanceof ClassDef){
+ ClassDef classDef = (ClassDef) ast;
+ String className = ((NameTok)classDef.name).id;
+
+ Definition foundDef = sourceToken.getDefinition();
+ IModule mod = this.current;
+ if(foundDef != null){
+ mod = foundDef.module;
+ }
+
+ IDefinition[] definition = mod.findDefinition(
+ CompletionStateFactory.getEmptyCompletionState(className+".__init__", nature, completionCache), -1, -1, nature);
+ callingBoundMethod = true;
+ for (IDefinition iDefinition : definition) {
+ Definition d = (Definition) iDefinition;
+ if(d.ast instanceof FunctionDef){
+ functionDefinitionReferenced = (FunctionDef) d.ast;
+ break;
+ }
+ }
}
-
- IDefinition[] definition = mod.findDefinition(
- CompletionStateFactory.getEmptyCompletionState(className+".__init__", nature, completionCache), -1, -1, nature);
- callingBoundMethod = true;
+ }
+ }else{
+ startRecordFound();
+ callNode.func.accept(this);
+ nameToken = popFound();
+ if(nameToken instanceof SourceToken){
+ String rep = nameToken.getRepresentation();
+ IDefinition[] definition = this.current.findDefinition(
+ CompletionStateFactory.getEmptyCompletionState(rep, nature, completionCache), -1, -1, nature);
for (IDefinition iDefinition : definition) {
Definition d = (Definition) iDefinition;
if(d.ast instanceof FunctionDef){
functionDefinitionReferenced = (FunctionDef) d.ast;
+
+ FastStack scopeStack = d.scope.getScopeStack();
+ if(scopeStack.size() > 1 && scopeStack.peek(1) instanceof ClassDef){
+ callingBoundMethod = true;
+ String withoutLast = FullRepIterable.getWithoutLastPart(rep);
+ definition = this.current.findDefinition(
+ CompletionStateFactory.getEmptyCompletionState(withoutLast, nature, completionCache), -1, -1, nature);
+ for (IDefinition iDefinition2 : definition) {
+ Definition d2 = (Definition) iDefinition2;
+ if(d2.ast instanceof ClassDef){
+ callingBoundMethod = false;
+ break;
+ }
+ }
+ }else{
+ callingBoundMethod = false;
+ }
break;
}
}
}
}
- }else{
- callNode.func.accept(this);
}
}
@@ -279,8 +318,8 @@ private void analyzeCallAndFunctionMatch(Call callNode, FunctionDef functionDefi
}
String rep = NodeUtils.getRepresentationString(functionDefinitionReferenced.args.args[i]);
if(functionDefinitionReferenced.args.defaults == null ||
- functionDefinitionReferenced.args.defaults.length < i ||
- functionDefinitionReferenced.args.defaults[i] == null){
+ (functionDefinitionReferenced.args.defaults.length > i &&
+ functionDefinitionReferenced.args.defaults[i] == null)){
//it's null, so, it's required
functionRequiredArgs.add(rep);
}else{
@@ -1267,7 +1306,7 @@ protected boolean markRead(IToken token, String rep, boolean addToNotDefined, bo
for (IDefinition iDefinition : definition) {
Definition d = (Definition) iDefinition;
if(d.ast instanceof FunctionDef || d.ast instanceof ClassDef){
- SourceToken tok = AbstractVisitor.makeToken(d.ast, d.module != null?d.module.getName():"");
+ SourceToken tok = AbstractVisitor.makeToken(d.ast, token.getRepresentation(), d.module != null?d.module.getName():"");
tok.setDefinition(d);
onFound(tok);
reportFound = false;
View
1  plugins/com.python.pydev.analysis/src/com/python/pydev/analysis/visitors/OccurrencesVisitor.java
@@ -70,6 +70,7 @@
public OccurrencesVisitor(IPythonNature nature, String moduleName, IModule current, IAnalysisPreferences prefs, IDocument document, IProgressMonitor monitor) {
super(nature, moduleName, current, document, monitor);
+ this.analyzeArgumentsMismatch = true;
this.messagesManager = new MessagesManager(prefs, moduleName, document);
this.duplicationChecker = new DuplicationChecker(this);
this.noSelfChecker = new NoSelfChecker(this, moduleName);
View
37 plugins/com.python.pydev.analysis/tests/com/python/pydev/analysis/OccurrencesAnalyzer2Test.java
@@ -31,7 +31,7 @@ public static void main(String[] args) {
try {
OccurrencesAnalyzer2Test analyzer2 = new OccurrencesAnalyzer2Test();
analyzer2.setUp();
- analyzer2.testParameterAnalysis17a();
+ analyzer2.testParameterAnalysis19b();
analyzer2.tearDown();
System.out.println("finished");
@@ -384,7 +384,7 @@ public void testParameterAnalysis7() throws IOException{
public void testParameterAnalysis8() throws IOException{
doc = new Document(
- "from extendable.calltips.mod1.sub1 import method1\n"+
+ "from extendable.calltips.mod1.sub1 import method1\n"+ //method1(a, b)
"method1(10)"
);
IMessage[] errors = checkError(1);
@@ -536,14 +536,31 @@ public void testParameterAnalysis18() throws IOException{
checkError("SomeOtherTest: arguments don't match");
}
-// public void testParameterAnalysis19() throws IOException{
-// doc = new Document(
-// "from extendable.parameters.check import Foo\n" + //class with __init__ == __init__(self, a, b)
-// "foo = Foo(10, 20)\n" +
-// "foo.Method()\n" //Method(self, a)
-// );
-// checkError("Method: arguments don't match");
-// }
+ public void testParameterAnalysis19() throws IOException{
+ doc = new Document(
+ "from extendable.parameters.check import Foo\n" + //class with __init__ == __init__(self, a, b)
+ "foo = Foo(10, 20)\n" +
+ "foo.Method()\n" //Method(self, a)
+ );
+ checkError("foo.Method: arguments don't match");
+ }
+
+ public void testParameterAnalysis19a() throws IOException{
+ doc = new Document(
+ "from extendable.parameters.check import Foo\n" + //class with __init__ == __init__(self, a, b)
+ "foo = Foo(10, 20)\n" +
+ "Foo.Method(foo, 10)\n" //Method(self, a)
+ );
+ checkNoError();
+ }
+
+ public void testParameterAnalysis19b() throws IOException{
+ doc = new Document(
+ "from extendable.parameters.check import Foo\n" + //class with __init__ == __init__(self, a, b)
+ "Foo.Method(10)\n" //Method(self, a)
+ );
+ checkError("Foo.Method: arguments don't match");
+ }
public void testParameterAnalysis20() throws IOException{
doc = new Document(
View
8 ...hon.pydev/src_completions/org/python/pydev/editor/codecompletion/revisited/visitors/AbstractVisitor.java
@@ -69,14 +69,14 @@ protected SourceToken addToken(SimpleNode node) {
}
- /**
- * @param node
- * @return
- */
public static SourceToken makeToken(SimpleNode node, String moduleName) {
return new SourceToken(node, NodeUtils.getRepresentationString(node), NodeUtils.getNodeArgs(node), NodeUtils.getNodeDocString(node), moduleName);
}
+ public static SourceToken makeToken(SimpleNode node, String rep, String moduleName) {
+ return new SourceToken(node, rep, NodeUtils.getNodeArgs(node), NodeUtils.getNodeDocString(node), moduleName);
+ }
+
/**
* same as make token, but returns the full representation for a token, instead of just a 'partial' name
*/
Please sign in to comment.
Something went wrong with that request. Please try again.