PY-20744 Parse PEP-526 variable annotations

Annotation is preserved at the level of assignment nodes similar to
where CPython keeps them in its AST (in special "augassign" nodes).
For type annotations in form "x: int" without variable initialization
special statement PyTypeDefinitionStatement was introduced.
This commit is contained in:
Mikhail Golubev
2016-09-16 08:16:26 +03:00
parent 1ec6d3d0dd
commit 3266460142
15 changed files with 257 additions and 33 deletions
@@ -0,0 +1,8 @@
class C:
x: int
y: None = 42
def m(self, d):
x: List[bool]
d['foo']: str
(d['bar']): float
@@ -0,0 +1,93 @@
PyFile:VariableAnnotations.py
PyClass: C
PsiElement(Py:CLASS_KEYWORD)('class')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('C')
PyArgumentList
<empty list>
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeDeclarationStatement
PyTargetExpression: x
PsiElement(Py:IDENTIFIER)('x')
PyAnnotation
PsiElement(Py:COLON)(':')
PsiWhiteSpace(' ')
PyReferenceExpression: int
PsiElement(Py:IDENTIFIER)('int')
PsiWhiteSpace('\n ')
PyAssignmentStatement
PyTargetExpression: y
PsiElement(Py:IDENTIFIER)('y')
PyAnnotation
PsiElement(Py:COLON)(':')
PsiWhiteSpace(' ')
PyNoneLiteralExpression
PsiElement(Py:NONE_KEYWORD)('None')
PsiWhiteSpace(' ')
PsiElement(Py:EQ)('=')
PsiWhiteSpace(' ')
PyNumericLiteralExpression
PsiElement(Py:INTEGER_LITERAL)('42')
PsiWhiteSpace('\n\n ')
PyFunction('m')
PsiElement(Py:DEF_KEYWORD)('def')
PsiWhiteSpace(' ')
PsiElement(Py:IDENTIFIER)('m')
PyParameterList
PsiElement(Py:LPAR)('(')
PyNamedParameter('self')
PsiElement(Py:IDENTIFIER)('self')
PsiElement(Py:COMMA)(',')
PsiWhiteSpace(' ')
PyNamedParameter('d')
PsiElement(Py:IDENTIFIER)('d')
PsiElement(Py:RPAR)(')')
PsiElement(Py:COLON)(':')
PsiWhiteSpace('\n ')
PyStatementList
PyTypeDeclarationStatement
PyTargetExpression: x
PsiElement(Py:IDENTIFIER)('x')
PyAnnotation
PsiElement(Py:COLON)(':')
PsiWhiteSpace(' ')
PySubscriptionExpression
PyReferenceExpression: List
PsiElement(Py:IDENTIFIER)('List')
PsiElement(Py:LBRACKET)('[')
PyReferenceExpression: bool
PsiElement(Py:IDENTIFIER)('bool')
PsiElement(Py:RBRACKET)(']')
PsiWhiteSpace('\n ')
PyTypeDeclarationStatement
PySubscriptionExpression
PyReferenceExpression: d
PsiElement(Py:IDENTIFIER)('d')
PsiElement(Py:LBRACKET)('[')
PyStringLiteralExpression: foo
PsiElement(Py:SINGLE_QUOTED_STRING)(''foo'')
PsiElement(Py:RBRACKET)(']')
PyAnnotation
PsiElement(Py:COLON)(':')
PsiWhiteSpace(' ')
PyReferenceExpression: str
PsiElement(Py:IDENTIFIER)('str')
PsiWhiteSpace('\n ')
PyTypeDeclarationStatement
PyParenthesizedExpression
PsiElement(Py:LPAR)('(')
PySubscriptionExpression
PyReferenceExpression: d
PsiElement(Py:IDENTIFIER)('d')
PsiElement(Py:LBRACKET)('[')
PyStringLiteralExpression: bar
PsiElement(Py:SINGLE_QUOTED_STRING)(''bar'')
PsiElement(Py:RBRACKET)(']')
PsiElement(Py:RPAR)(')')
PyAnnotation
PsiElement(Py:COLON)(':')
PsiWhiteSpace(' ')
PyReferenceExpression: float
PsiElement(Py:IDENTIFIER)('float')