mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
PY-25157 Fixed: Inferred type shows incorrect return type with namedtuple
Implement equals and hashCode in PyNamedTupleType so different types are not the same.
This commit is contained in:
@@ -135,6 +135,23 @@ public class PyNamedTupleType extends PyClassTypeImpl implements PyCallableType
|
||||
return "PyNamedTupleType: " + myName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (o == this) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
if (!super.equals(o)) return false;
|
||||
|
||||
final PyNamedTupleType type = (PyNamedTupleType)o;
|
||||
return Objects.equals(myName, type.myName) &&
|
||||
Objects.equals(myFields.keySet(), type.myFields.keySet()) &&
|
||||
myDefinitionLevel == type.myDefinitionLevel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(super.hashCode(), myName, myFields.keySet(), myDefinitionLevel);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Set<String> getMemberNames(boolean inherited, @NotNull TypeEvalContext context) {
|
||||
|
||||
@@ -2055,6 +2055,20 @@ public class PyTypeTest extends PyTestCase {
|
||||
" expr = x");
|
||||
}
|
||||
|
||||
// PY-25157
|
||||
public void testFunctionWithDifferentNamedTuplesAsParameterAndReturnTypes() {
|
||||
runWithLanguageLevel(
|
||||
LanguageLevel.PYTHON35,
|
||||
() -> doTest("(a: MyType1) -> MyType2",
|
||||
"from collections import namedtuple\n" +
|
||||
"MyType1 = namedtuple('MyType1', 'x y')\n" +
|
||||
"MyType2 = namedtuple('MyType2', 'x y')\n" +
|
||||
"def foo(a: MyType1) -> MyType2:\n" +
|
||||
" pass\n" +
|
||||
"expr = foo")
|
||||
);
|
||||
}
|
||||
|
||||
private static List<TypeEvalContext> getTypeEvalContexts(@NotNull PyExpression element) {
|
||||
return ImmutableList.of(TypeEvalContext.codeAnalysis(element.getProject(), element.getContainingFile()).withTracing(),
|
||||
TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()).withTracing());
|
||||
|
||||
Reference in New Issue
Block a user