Skip to content

Commit e044f9e

Browse files
SprutSDMintellij-monorepo-bot
authored andcommitted
PY-39384: Introduce PyTypeHintProvider
`PyTypeHintProvider` allows providing type hints for resolved elements before the main logic of `PyTypingTypeProvider`. See an example of `django-stubs`: typeddjango/django-stubs#2335. `_UserModel` type might be replaced with a custom user model, provided in `settings.py` (`AUTH_USER_MODEL`). GitOrigin-RevId: 986fb91c800be3ccfbc002c73c673896efec8a1a
1 parent 45a808f commit e044f9e

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

python/python-psi-api/resources/META-INF/PythonPsi.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
interface="com.jetbrains.python.psi.resolve.PyReferenceResolveProvider"
55
dynamic="true"/>
66
<extensionPoint qualifiedName="Pythonid.typeProvider" interface="com.jetbrains.python.psi.impl.PyTypeProvider" dynamic="true"/>
7+
<extensionPoint qualifiedName="Pythonid.typeHintProvider" interface="com.jetbrains.python.codeInsight.typing.PyTypeHintProvider" dynamic="true"/>
78
<extensionPoint qualifiedName="Pythonid.pySuperMethodsSearch" interface="com.intellij.util.QueryExecutor" dynamic="true"/>
89
<extensionPoint qualifiedName="Pythonid.pyClassMembersProvider"
910
interface="com.jetbrains.python.psi.types.PyClassMembersProvider"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.jetbrains.python.codeInsight.typing
2+
3+
import com.intellij.openapi.extensions.ExtensionPointName
4+
import com.intellij.openapi.util.Ref
5+
import com.intellij.psi.PsiElement
6+
import com.jetbrains.python.psi.PyExpression
7+
import com.jetbrains.python.psi.PyQualifiedNameOwner
8+
import com.jetbrains.python.psi.types.PyType
9+
import com.jetbrains.python.psi.types.TypeEvalContext
10+
import org.jetbrains.annotations.ApiStatus
11+
import org.jetbrains.annotations.ApiStatus.Experimental
12+
13+
@Experimental
14+
@ApiStatus.Internal
15+
interface PyTypeHintProvider {
16+
fun parseTypeHint(typeHint: PyExpression, alias: PyQualifiedNameOwner?, resolved: PsiElement, context: TypeEvalContext): Ref<PyType>?
17+
18+
companion object {
19+
private val EP_NAME: ExtensionPointName<PyTypeHintProvider> = ExtensionPointName.create("Pythonid.typeHintProvider");
20+
21+
fun parseTypeHint(typeHint: PyExpression, alias: PyQualifiedNameOwner?, resolved: PsiElement, context: TypeEvalContext): Ref<PyType>? {
22+
return EP_NAME.extensionList.firstNotNullOfOrNull { it.parseTypeHint(typeHint, alias, resolved, context) }
23+
}
24+
}
25+
}

python/python-psi-impl/src/com/jetbrains/python/codeInsight/typing/PyTypingTypeProvider.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,15 @@ private static Ref<PyType> getTypeForResolvedElement(@NotNull PyExpression typeH
865865
context.getTypeAliasStack().add(alias);
866866
}
867867
try {
868+
final Ref<PyType> typeHintFromProvider = PyTypeHintProvider.Companion.parseTypeHint(
869+
typeHint,
870+
alias,
871+
resolved,
872+
context.getTypeContext()
873+
);
874+
if (typeHintFromProvider != null) {
875+
return typeHintFromProvider;
876+
}
868877
final Ref<PyType> typeFromParenthesizedExpression = getTypeFromParenthesizedExpression(resolved, context);
869878
if (typeFromParenthesizedExpression != null) {
870879
return typeFromParenthesizedExpression;

0 commit comments

Comments
 (0)