Infer correct type for docstrings (PY-35885)

GitOrigin-RevId: 2994a2e312611b038c72ec18abb2cd96880213e9
This commit is contained in:
Semyon Proshev
2019-10-08 21:17:41 +03:00
committed by intellij-monorepo-bot
parent 39c4f75298
commit 161a28d32a
3 changed files with 21 additions and 0 deletions

View File

@@ -165,6 +165,9 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt
// f-strings can't have "b" prefix so they are always unicode
return builtinCache.getUnicodeType(languageLevel);
}
else if (firstNode.getElementType() == PyTokenTypes.DOCSTRING) {
return builtinCache.getStrType();
}
if (file != null) {
final IElementType type = PythonHighlightingLexer.convertStringType(firstNode.getElementType(),

View File

@@ -1133,6 +1133,15 @@ public class Py3TypeTest extends PyTestCase {
"expr = f'foo'");
}
// PY-35885
public void testFunctionDunderDoc() {
doTest("str",
"def example():\n" +
" \"\"\"Example Docstring\"\"\"\n" +
" return 0\n" +
"expr = example.__doc__");
}
private void doTest(final String expectedType, final String text) {
myFixture.configureByText(PythonFileType.INSTANCE, text);
final PyExpression expr = myFixture.findElementByText("expr", PyExpression.class);

View File

@@ -3768,6 +3768,15 @@ public class PyTypeTest extends PyTestCase {
);
}
// PY-35885
public void testFunctionDunderDoc() {
doTest("str",
"def example():\n" +
" \"\"\"Example Docstring\"\"\"\n" +
" return 0\n" +
"expr = example.__doc__");
}
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());