PY-40313 Fix for "Python string literals in Markdown documents are treated as 'bytes' for type-checking purposes"

(cherry picked from commit 2a423d0fbbf4524844c52189a6be4cf9f43f7407)

IJ-CR-18590

GitOrigin-RevId: 33739b79cb8bc76c447d69080940985f431641dc
This commit is contained in:
Daniil Kalinin
2021-12-27 09:25:39 +00:00
committed by intellij-monorepo-bot
parent c1c97dde02
commit dda63f05d8
2 changed files with 34 additions and 9 deletions
@@ -1,6 +1,7 @@
package com.jetbrains.python.markdown
import com.jetbrains.python.fixtures.PyTestCase
import com.jetbrains.python.inspections.PyTypeCheckerInspection
/**
* Tests for [PyCodeFenceLanguageProvider].
@@ -127,6 +128,28 @@ class PyCodeFenceTest : PyTestCase() {
assertContainsElements(lookupStrings, "doctest", "python", "pycon")
}
//PY-40313
fun testStringsAndBytesResolvedCorrectlyInPythonFragment() {
myFixture.configureByText("a.md", """
```python
def expect_str(s:str):
pass
def expect_bytes(b:bytes):
pass
# Should not warn
expect_str("abc")
# Should warn
expect_bytes(<warning descr="Expected type 'bytes', got 'str' instead">"abc"</warning>)
```
""".trimIndent())
myFixture.enableInspections(PyTypeCheckerInspection::class.java);
myFixture.checkHighlighting(true, false, true);
assertSdkRootsNotParsed(myFixture.file)
}
private val lookupStrings: List<String>
get() = myFixture.lookupElementStrings ?: emptyList()
}
@@ -175,18 +175,20 @@ public class PyStringLiteralExpressionImpl extends PyElementImpl implements PySt
else if (firstNode.getElementType() == PyTokenTypes.DOCSTRING) {
return builtinCache.getStrType();
}
else if (((PyStringElement)firstNode).isBytes()) {
return builtinCache.getBytesType(languageLevel);
}
if (file != null) {
final IElementType type = PythonHighlightingLexer.convertStringType(firstNode.getElementType(),
firstNode.getText(),
languageLevel,
file.hasImportFromFuture(FutureFeature.UNICODE_LITERALS));
if (PyTokenTypes.UNICODE_NODES.contains(type)) {
return builtinCache.getUnicodeType(languageLevel);
}
final IElementType type = PythonHighlightingLexer.convertStringType(firstNode.getElementType(),
firstNode.getText(),
languageLevel,
(file != null &&
file.hasImportFromFuture(FutureFeature.UNICODE_LITERALS)));
if (PyTokenTypes.UNICODE_NODES.contains(type)) {
return builtinCache.getUnicodeType(languageLevel);
}
}
return builtinCache.getBytesType(languageLevel);
return builtinCache.getStrType();
}
@Override