From 7fa9888621a7a263ac6b378111968d36a9eda2e2 Mon Sep 17 00:00:00 2001 From: Mikhail Golubev Date: Wed, 18 Jun 2025 16:53:16 +0300 Subject: [PATCH] PY-24834 Fix Py3CompletionTest.testTypedParameterStringPath GitOrigin-RevId: 628a4a396de46b8e73cdfbec174394987527a0a4 --- .../PySoftFileReferenceContributor.kt | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/PySoftFileReferenceContributor.kt b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/PySoftFileReferenceContributor.kt index b8448250f12a..97e8b21acf4b 100644 --- a/python/python-psi-impl/src/com/jetbrains/python/codeInsight/PySoftFileReferenceContributor.kt +++ b/python/python-psi-impl/src/com/jetbrains/python/codeInsight/PySoftFileReferenceContributor.kt @@ -17,7 +17,9 @@ import com.jetbrains.python.psi.resolve.PyResolveContext import com.jetbrains.python.psi.resolve.PyResolveUtil import com.jetbrains.python.psi.resolve.fromFoothold import com.jetbrains.python.psi.resolve.resolveTopLevelMember +import com.jetbrains.python.psi.types.PyType import com.jetbrains.python.psi.types.PyTypeChecker +import com.jetbrains.python.psi.types.PyTypeUtil import com.jetbrains.python.psi.types.PyUnionType import com.jetbrains.python.psi.types.TypeEvalContext import java.util.Locale @@ -102,20 +104,20 @@ open class PySoftFileReferenceContributor : PsiReferenceContributor() { ) as? PyTypedElement ?: return false val osPathLikeType = typeEvalContext.getType(osPathLike) ?: return false - return callExpr.multiResolveCallee(PyResolveContext.defaultContext(typeEvalContext)) + val argumentTypes = callExpr.multiResolveCallee(PyResolveContext.defaultContext(typeEvalContext)) .asSequence() .mapNotNull { - val mapping = callExpr.mapArguments( it, typeEvalContext) + val mapping = callExpr.mapArguments(it, typeEvalContext) mapping.mappedParameters[expr]?.getArgumentType(typeEvalContext) } - .mapNotNull(PyUnionType::toNonWeakType) - .toList() - .let(PyUnionType::union) - .let { - it != null && - PyTypeChecker.match(bytesOrUnicodeType, it, typeEvalContext) && - PyTypeChecker.match(osPathLikeType, it, typeEvalContext) - } + + // We can't use PyTypeChecker.match directly because the type `str | PathLike` is considered incompatible + // with neither str nor PathLike (strict union semantics). + fun PyType.allowsValuesCompatibleWith(superType: PyType): Boolean = + PyTypeUtil.toStream(this).anyMatch { it != null && PyTypeChecker.match(superType, it, typeEvalContext) } + + return argumentTypes.any { it.allowsValuesCompatibleWith(bytesOrUnicodeType) } && + argumentTypes.any { it.allowsValuesCompatibleWith(osPathLikeType) } } }