From f026238360b9cf2f5efd650afa37dd2dd5ab8f9b Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Thu, 16 Aug 2012 18:26:08 +0400 Subject: [PATCH] Removed PyWeakType in favor of PyDynamicallyEvaluatedType --- .../debugger/PyCallSignatureTypeProvider.java | 22 ++++--------- .../PyUnresolvedReferencesInspection.java | 7 ---- .../python/psi/impl/PyCallExpressionImpl.java | 2 +- .../python/psi/impl/PyFunctionImpl.java | 11 ++++--- .../python/psi/impl/PyWeakTypeFactory.java | 26 --------------- .../psi/types/PyDynamicallyEvaluatedType.java | 33 +++++++++++++++++++ .../python/psi/types/PyTypeChecker.java | 3 -- .../python/psi/types/PyWeakClassType.java | 13 -------- .../python/psi/types/PyWeakNoneType.java | 11 ------- .../python/psi/types/PyWeakType.java | 7 ---- .../python/psi/types/PyWeakUnionType.java | 12 ------- 11 files changed, 47 insertions(+), 100 deletions(-) delete mode 100644 python/src/com/jetbrains/python/psi/impl/PyWeakTypeFactory.java create mode 100644 python/src/com/jetbrains/python/psi/types/PyDynamicallyEvaluatedType.java delete mode 100644 python/src/com/jetbrains/python/psi/types/PyWeakClassType.java delete mode 100644 python/src/com/jetbrains/python/psi/types/PyWeakNoneType.java delete mode 100644 python/src/com/jetbrains/python/psi/types/PyWeakType.java delete mode 100644 python/src/com/jetbrains/python/psi/types/PyWeakUnionType.java diff --git a/python/src/com/jetbrains/python/debugger/PyCallSignatureTypeProvider.java b/python/src/com/jetbrains/python/debugger/PyCallSignatureTypeProvider.java index 750edbb342b2..19d9f5009934 100644 --- a/python/src/com/jetbrains/python/debugger/PyCallSignatureTypeProvider.java +++ b/python/src/com/jetbrains/python/debugger/PyCallSignatureTypeProvider.java @@ -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); - } } diff --git a/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java b/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java index bf3ebab42a0f..219e21aa3459 100644 --- a/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyUnresolvedReferencesInspection.java @@ -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) diff --git a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionImpl.java index 590ef28b9b57..8ebf3b7b14ad 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyCallExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyCallExpressionImpl.java @@ -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; diff --git a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java index fdcc850eebcd..7168998b8506 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyFunctionImpl.java @@ -166,8 +166,8 @@ public class PyFunctionImpl extends PyPresentableElementImpl 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 imp return type; } - private static boolean isWeak(Map arguments, TypeEvalContext context) { - for (PyNamedParameter param : arguments.values()) { - if (param.getType(context) instanceof PyWeakType) { + private static boolean isDynamicallyEvaluated(@NotNull Collection parameters, @NotNull TypeEvalContext context) { + for (PyNamedParameter parameter : parameters) { + final PyType type = parameter.getType(context); + if (type instanceof PyDynamicallyEvaluatedType) { return true; } } diff --git a/python/src/com/jetbrains/python/psi/impl/PyWeakTypeFactory.java b/python/src/com/jetbrains/python/psi/impl/PyWeakTypeFactory.java deleted file mode 100644 index 71acfd411e56..000000000000 --- a/python/src/com/jetbrains/python/psi/impl/PyWeakTypeFactory.java +++ /dev/null @@ -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()); - } - } -} diff --git a/python/src/com/jetbrains/python/psi/types/PyDynamicallyEvaluatedType.java b/python/src/com/jetbrains/python/psi/types/PyDynamicallyEvaluatedType.java new file mode 100644 index 000000000000..01b535c92222 --- /dev/null +++ b/python/src/com/jetbrains/python/psi/types/PyDynamicallyEvaluatedType.java @@ -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 members) { + super(members); + } + + @NotNull + public static PyDynamicallyEvaluatedType create(@NotNull PyType type) { + final List members = new ArrayList(); + 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); + } +} diff --git a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java index 500046cb6ae0..cee945351bdc 100644 --- a/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java +++ b/python/src/com/jetbrains/python/psi/types/PyTypeChecker.java @@ -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())) { diff --git a/python/src/com/jetbrains/python/psi/types/PyWeakClassType.java b/python/src/com/jetbrains/python/psi/types/PyWeakClassType.java deleted file mode 100644 index 801d7f117ee1..000000000000 --- a/python/src/com/jetbrains/python/psi/types/PyWeakClassType.java +++ /dev/null @@ -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); - } -} diff --git a/python/src/com/jetbrains/python/psi/types/PyWeakNoneType.java b/python/src/com/jetbrains/python/psi/types/PyWeakNoneType.java deleted file mode 100644 index ab2ea192fea6..000000000000 --- a/python/src/com/jetbrains/python/psi/types/PyWeakNoneType.java +++ /dev/null @@ -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() { - } -} diff --git a/python/src/com/jetbrains/python/psi/types/PyWeakType.java b/python/src/com/jetbrains/python/psi/types/PyWeakType.java deleted file mode 100644 index 7a1792e49ac4..000000000000 --- a/python/src/com/jetbrains/python/psi/types/PyWeakType.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.jetbrains.python.psi.types; - -/** - * - */ -public interface PyWeakType extends PyType { -} diff --git a/python/src/com/jetbrains/python/psi/types/PyWeakUnionType.java b/python/src/com/jetbrains/python/psi/types/PyWeakUnionType.java deleted file mode 100644 index ddc306e53cee..000000000000 --- a/python/src/com/jetbrains/python/psi/types/PyWeakUnionType.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.jetbrains.python.psi.types; - -import java.util.Collection; - -/** - * - */ -public class PyWeakUnionType extends PyUnionType implements PyWeakType{ - private PyWeakUnionType(Collection members) { - super(members); - } -}