PY-74231 Fix false positive "Statement expected, found Py:DEDENT" for a nested type alias

GitOrigin-RevId: 03d64abe2c949a5912eb5c16ef48a5149568d66f
This commit is contained in:
Daniil Kalinin
2024-10-01 12:21:42 +02:00
committed by intellij-monorepo-bot
parent 2f492127fc
commit b934cfe38a
8 changed files with 225 additions and 0 deletions

View File

@@ -147,6 +147,7 @@ public class StatementParsing extends Parsing implements ITokenTypeRemapper {
parseTypeParameterList();
checkMatches(PyTokenTypes.EQ, PyParsingBundle.message("PARSE.eq.expected"));
myContext.getExpressionParser().parseExpression();
checkEndOfStatement();
mark.done(PyElementTypes.TYPE_ALIAS_STATEMENT);
return true;
}

View File

@@ -0,0 +1,5 @@
class Foo:
type MyInt = int
class Inner:
type MyInner = str

View File

@@ -0,0 +1,38 @@
PyFile:TypeAliasStatementInClassBody.py
PyClass: Foo
PsiElement(Py:CLASS_KEYWORD)('class')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('Foo')
PyArgumentList
<empty list>
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('MyInt')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: int
PsiElement(Py:IDENTIFIER)('int')
PsiWhiteSpace('\n\n ')
PyClass: Inner
PsiElement(Py:CLASS_KEYWORD)('class')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('Inner')
PyArgumentList
<empty list>
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('MyInner')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: str
PsiElement(Py:IDENTIFIER)('str')

View File

@@ -0,0 +1,4 @@
def foo():
type Inner = str
def bar():
type innerBar = int

View File

@@ -0,0 +1,40 @@
PyFile:TypeAliasStatementInFunctionBody.py
PyFunction('foo')
PsiElement(Py:DEF_KEYWORD)('def')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('foo')
PyParameterList
PsiElement(Py:LPAR)('(')
PsiElement(Py:RPAR)(')')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('Inner')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: str
PsiElement(Py:IDENTIFIER)('str')
PsiWhiteSpace('\n ')
PyFunction('bar')
PsiElement(Py:DEF_KEYWORD)('def')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('bar')
PyParameterList
PsiElement(Py:LPAR)('(')
PsiElement(Py:RPAR)(')')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('innerBar')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: int
PsiElement(Py:IDENTIFIER)('int')

View File

@@ -0,0 +1,13 @@
if True:
type myType = int
else:
type myType = str
for i in range(10):
type myType = str
match num:
case 1:
type myType = int
case 2:
type myType = str

View File

@@ -0,0 +1,109 @@
PyFile:TypeAliasStatementInsideStatementListContainers.py
PyIfStatement
PyIfPartIf
PsiElement(Py:IF_KEYWORD)('if')
PsiWhiteSpace(' ')
PyBoolLiteralExpression
PsiElement(Py:TRUE_KEYWORD)('True')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('myType')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: int
PsiElement(Py:IDENTIFIER)('int')
PsiWhiteSpace('\n')
PyElsePart
PsiElement(Py:ELSE_KEYWORD)('else')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('myType')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: str
PsiElement(Py:IDENTIFIER)('str')
PsiWhiteSpace('\n\n')
PyForStatement
PyForPart
PsiElement(Py:FOR_KEYWORD)('for')
PsiWhiteSpace(' ')
PyTargetExpression: i
PsiElement(Py:IDENTIFIER)('i')
PsiWhiteSpace(' ')
PsiElement(Py:IN_KEYWORD)('in')
PsiWhiteSpace(' ')
PyCallExpression: range
PyReferenceExpression: range
PsiElement(Py:IDENTIFIER)('range')
PyArgumentList
PsiElement(Py:LPAR)('(')
PyNumericLiteralExpression
PsiElement(Py:INTEGER_LITERAL)('10')
PsiElement(Py:RPAR)(')')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('myType')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: str
PsiElement(Py:IDENTIFIER)('str')
PsiWhiteSpace('\n\n')
PyMatchStatement
PsiElement(Py:MATCH_KEYWORD)('match')
PsiWhiteSpace(' ')
PyReferenceExpression: num
PsiElement(Py:IDENTIFIER)('num')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyCaseClause
PsiElement(Py:CASE_KEYWORD)('case')
PsiWhiteSpace(' ')
PyLiteralPattern
PyNumericLiteralExpression
PsiElement(Py:INTEGER_LITERAL)('1')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('myType')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: int
PsiElement(Py:IDENTIFIER)('int')
PsiWhiteSpace('\n ')
PyCaseClause
PsiElement(Py:CASE_KEYWORD)('case')
PsiWhiteSpace(' ')
PyLiteralPattern
PyNumericLiteralExpression
PsiElement(Py:INTEGER_LITERAL)('2')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeAliasStatement
PsiElement(Py:TYPE_KEYWORD)('type')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('myType')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyReferenceExpression: str
PsiElement(Py:IDENTIFIER)('str')

View File

@@ -1364,6 +1364,21 @@ public class PythonParsingTest extends ParsingTestCase {
doTest(LanguageLevel.PYTHON313);
}
// PY-74231
public void testTypeAliasStatementInClassBody() {
doTest(LanguageLevel.PYTHON312);
}
// PY-74231
public void testTypeAliasStatementInFunctionBody() {
doTest(LanguageLevel.PYTHON312);
}
// PY-74321
public void testTypeAliasStatementInsideStatementListContainers() {
doTest(LanguageLevel.PYTHON312);
}
public void testTypeKeywordAsIdentifier() {
doTest(LanguageLevel.PYTHON312);
}