Removed PyWeakType in favor of PyDynamicallyEvaluatedType

This commit is contained in:
Andrey Vlasovskikh
2012-08-16 18:26:08 +04:00
parent fc2208c5ad
commit f026238360
11 changed files with 47 additions and 100 deletions
@@ -2,12 +2,7 @@ package com.jetbrains.python.debugger;
import com.jetbrains.python.psi.PyFunction;
import com.jetbrains.python.psi.PyNamedParameter;
import com.jetbrains.python.psi.impl.PyWeakTypeFactory;
import com.jetbrains.python.psi.types.PyType;
import com.jetbrains.python.psi.types.PyTypeParser;
import com.jetbrains.python.psi.types.PyTypeProviderBase;
import com.jetbrains.python.psi.types.TypeEvalContext;
import org.jetbrains.annotations.Nullable;
import com.jetbrains.python.psi.types.*;
/**
* @author traff
@@ -16,17 +11,14 @@ public class PyCallSignatureTypeProvider extends PyTypeProviderBase {
public PyType getParameterType(final PyNamedParameter param, final PyFunction func, TypeEvalContext context) {
final String name = param.getName();
if (name != null) {
final String type = ((PySignatureCacheManagerImpl)PySignatureCacheManager.getInstance(param.getProject())).findParameterType(func, name);
if (type != null) {
final PyType typeByName = PyTypeParser.getTypeByName(param, type);
return buildWeakType(typeByName);
final String typeName = ((PySignatureCacheManagerImpl)PySignatureCacheManager.getInstance(param.getProject())).findParameterType(func, name);
if (typeName != null) {
final PyType type = PyTypeParser.getTypeByName(param, typeName);
if (type != null) {
return PyDynamicallyEvaluatedType.create(type);
}
}
}
return null;
}
@Nullable
private static PyType buildWeakType(PyType type) {
return PyWeakTypeFactory.create(type);
}
}
@@ -292,9 +292,6 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
if (qualifier != null && name != null && isGuardedByHasattr(qualifier, name)) {
return;
}
if (qualifier != null && isWeakQualifier(qualifier)) {
return;
}
}
PsiElement target = null;
boolean unresolved;
@@ -329,10 +326,6 @@ public class PyUnresolvedReferencesInspection extends PyInspection {
}
}
private boolean isWeakQualifier(PyExpression qualifier) {
return qualifier.getType(myTypeEvalContext) instanceof PyWeakType;
}
private static boolean isContainingFileImportAllowed(PyElement node, PsiFile target) {
// import resolving to containing file is allowed when we're importing from the current package and the containing file
// is __init__.py (PY-5265)
@@ -163,7 +163,7 @@ public class PyCallExpressionImpl extends PyElementImpl implements PyCallExpress
if (t instanceof PyTypeReference) {
PyType resolved = ((PyTypeReference)t).resolve(callee, context);
if (resolved == null && cls != null) {
return new PyWeakClassType(cls, false);
return PyUnionType.createWeakType(new PyClassTypeImpl(cls, false));
}
}
return t;
@@ -166,8 +166,8 @@ public class PyFunctionImpl extends PyPresentableElementImpl<PyFunctionStub> imp
}
return null;
}
if (results != null && isWeak(results.getArguments(), context)) {
return PyWeakTypeFactory.create(type);
if (results != null && isDynamicallyEvaluated(results.getArguments().values(), context)) {
return PyUnionType.createWeakType(type);
}
else {
return type;
@@ -192,9 +192,10 @@ public class PyFunctionImpl extends PyPresentableElementImpl<PyFunctionStub> imp
return type;
}
private static boolean isWeak(Map<PyExpression, PyNamedParameter> arguments, TypeEvalContext context) {
for (PyNamedParameter param : arguments.values()) {
if (param.getType(context) instanceof PyWeakType) {
private static boolean isDynamicallyEvaluated(@NotNull Collection<PyNamedParameter> parameters, @NotNull TypeEvalContext context) {
for (PyNamedParameter parameter : parameters) {
final PyType type = parameter.getType(context);
if (type instanceof PyDynamicallyEvaluatedType) {
return true;
}
}
@@ -1,26 +0,0 @@
package com.jetbrains.python.psi.impl;
import com.jetbrains.python.psi.types.*;
import org.jetbrains.annotations.Nullable;
/**
* @author traff
*/
public class PyWeakTypeFactory {
@Nullable
public static PyWeakType create(@Nullable PyType type) {
if (type == null) {
return null;
}
else if (type instanceof PyClassType) {
PyClassType classType = (PyClassType)type;
return new PyWeakClassType(classType.getPyClass(), classType.isDefinition());
}
else if (type instanceof PyNoneType) {
return PyWeakNoneType.INSTANCE;
}
else {
throw new IllegalStateException("For type " + type.getName());
}
}
}
@@ -0,0 +1,33 @@
package com.jetbrains.python.psi.types;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* @author vlan
*/
public class PyDynamicallyEvaluatedType extends PyUnionType {
private PyDynamicallyEvaluatedType(@NotNull Collection<PyType> members) {
super(members);
}
@NotNull
public static PyDynamicallyEvaluatedType create(@NotNull PyType type) {
final List<PyType> members = new ArrayList<PyType>();
if (type instanceof PyUnionType) {
final PyUnionType unionType = (PyUnionType)type;
members.addAll(unionType.getMembers());
if (!unionType.isWeak()) {
members.add(null);
}
}
else {
members.add(type);
members.add(null);
}
return new PyDynamicallyEvaluatedType(members);
}
}
@@ -35,9 +35,6 @@ public class PyTypeChecker {
if (expected == null || actual == null) {
return true;
}
if (expected instanceof PyWeakType || actual instanceof PyWeakType) {
return true;
}
if (expected instanceof PyClassType) {
final PyClass c = ((PyClassType)expected).getPyClass();
if (c != null && "object".equals(c.getName())) {
@@ -1,13 +0,0 @@
package com.jetbrains.python.psi.types;
import com.jetbrains.python.psi.PyClass;
import org.jetbrains.annotations.NotNull;
/**
* @author traff
*/
public class PyWeakClassType extends PyClassTypeImpl implements PyWeakType {
public PyWeakClassType(@NotNull PyClass source, boolean isDefinition) {
super(source, isDefinition);
}
}
@@ -1,11 +0,0 @@
package com.jetbrains.python.psi.types;
/**
* @author traff
*/
public class PyWeakNoneType extends PyNoneType implements PyWeakType{
public static PyWeakNoneType INSTANCE = new PyWeakNoneType();
private PyWeakNoneType() {
}
}
@@ -1,7 +0,0 @@
package com.jetbrains.python.psi.types;
/**
*
*/
public interface PyWeakType extends PyType {
}
@@ -1,12 +0,0 @@
package com.jetbrains.python.psi.types;
import java.util.Collection;
/**
*
*/
public class PyWeakUnionType extends PyUnionType implements PyWeakType{
private PyWeakUnionType(Collection<PyType> members) {
super(members);
}
}