From 137308caf6c4d3f74b442e9bbbd04fbd300e3473 Mon Sep 17 00:00:00 2001 From: TeamSpen210 Date: Mon, 25 Mar 2019 08:14:55 +1000 Subject: [PATCH 1/6] [Python] Add syntax highlighting for type comments --- Python/Python.sublime-syntax | 62 ++++++++++++++++++++++++++++++++++++ Python/syntax_test_python.py | 27 ++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index d15e5eb32c..639a92f163 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -402,6 +402,68 @@ contexts: pop: true comments: + # Type-ignore must be the only hint allowed! + - match: '(#)\s*(type)(:)\s*(ignore)' + captures: + 1: punctuation.definition.comment.python comment.line.number-sign.python + 2: keyword.other.annotation.type-comment + 3: punctuation.separator.annotation.type-comment + 4: keyword.other.annotation.type-comment.ignore + set: + - meta_scope: comment.line.type-hint + - include: comments_noannotation + - match: \n + pop: true + - match: \S + scope: invalid.illegal + - match: '(#)\s*(type)(:)' + scope: comment.line.type-hint + captures: + 1: punctuation.definition.comment.python comment.line.number-sign.python + 2: keyword.other.annotation.type-comment + 3: punctuation.separator.annotation.type-comment + embed: type-comment + escape: '\n' + embed_scope: comment.line.type-hint + + - include: comments_noannotation + + type-comment: + - include: comments_noannotation + + # Don't use expressions here, only a few types are reasonable + # as annotations. + - include: constants + - include: builtin-exceptions + - include: builtin-types + - include: builtin-functions + - match: '{{identifier}}' + scope: support.class.python + + - match: '\s*(\[)' + captures: + 1: meta.item-access.python punctuation.section.brackets.begin.python + push: + - meta_content_scope: meta.item-access.arguments.python + - match: \] + scope: meta.item-access.python punctuation.section.brackets.end.python + pop: true + - match: ':' + scope: punctuation.separator.slice.python + - include: type-comment + + - match: ',' + scope: punctuation.separator.sequence.python + - match: \) + scope: invalid.illegal.stray.brace.round.python + - match: \] + scope: invalid.illegal.stray.brace.square.python + - match: \} + scope: invalid.illegal.stray.brace.curly.python + - match: \S + scope: invalid.illegal + + comments_noannotation: - match: "#" scope: punctuation.definition.comment.python push: diff --git a/Python/syntax_test_python.py b/Python/syntax_test_python.py index 1302866116..cefdb191f5 100644 --- a/Python/syntax_test_python.py +++ b/Python/syntax_test_python.py @@ -1350,6 +1350,33 @@ class Starship: # ^ punctuation.separator.annotation.variable.python # ^ keyword.operator.assignment +primes = 5 # type: ignore # type: not-a-type-comment +# ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python +# ^^^^ comment.line.type-hint keyword.other.annotation.type-comment +# ^ comment.line.type-hint punctuation.separator.annotation.type-comment +# ^^^^^^ comment.line.type-hint keyword.other.annotation.type-comment.ignore +# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python +# ^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint comment.line.number-sign.python + +primes = 5 # type: List[Dict[property:str, ...]], bool # comment +# ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python +# ^^^^ comment.line.type-hint keyword.other.annotation.type-comment +# ^ comment.line.type-hint punctuation.separator.annotation.type-comment +# ^^^^ comment.line.type-hint support.class.python +# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.begin.python +# ^^^^ comment.line.type-hint meta.item-access.arguments.python support.class.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.begin.python +# ^^^^^^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.function.builtin.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.slice.python +# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.type.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.sequence.python +# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python constant.language.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint punctuation.separator.sequence.python +# ^^^^ comment.line.type-hint support.type.python +# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python + # <- - meta # ensure we're not leaking a context From ed61bb433a26e8f6e386472bcd210d085e111250 Mon Sep 17 00:00:00 2001 From: TeamSpen210 Date: Wed, 27 Mar 2019 09:48:38 +1000 Subject: [PATCH 2/6] [Python] Add function type hint comment syntax --- Python/Python.sublime-syntax | 28 ++++++++++++++++++++++++ Python/syntax_test_python.py | 41 ++++++++++++++++++++++++++---------- 2 files changed, 58 insertions(+), 11 deletions(-) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index 639a92f163..ec68794b80 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -452,6 +452,34 @@ contexts: scope: punctuation.separator.slice.python - include: type-comment + - match: '\(' + scope: punctuation.section.parens.begin.python + push: + - meta_content_scope: meta.function.parameters.python + - match: \) + scope: punctuation.section.parens.end.python + pop: true + - match: \( + scope: invalid.illegal.stray.brace.round.python + + - match: \*{3,} + scope: invalid.illegal.syntax.python + - match: \*\* + scope: keyword.operator.unpacking.mapping.python + - match: \* + scope: keyword.operator.unpacking.sequence.python + + - match: '->' + scope: invalid.illegal.stray.return.arrow.python + - include: type-comment + + - match: '->' + scope: punctuation.separator.sequence.python meta.function.return-type.python + set: + - match: '->' + scope: invalid.illegal.stray.return.arrow.python + - include: type-comment + - meta_scope: meta.function.return-type.python - match: ',' scope: punctuation.separator.sequence.python - match: \) diff --git a/Python/syntax_test_python.py b/Python/syntax_test_python.py index cefdb191f5..73cf87ff7e 100644 --- a/Python/syntax_test_python.py +++ b/Python/syntax_test_python.py @@ -1350,6 +1350,8 @@ class Starship: # ^ punctuation.separator.annotation.variable.python # ^ keyword.operator.assignment +# Type comments - type: ignore must be by itself. + primes = 5 # type: ignore # type: not-a-type-comment # ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python # ^^^^ comment.line.type-hint keyword.other.annotation.type-comment @@ -1358,7 +1360,7 @@ class Starship: # ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python # ^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint comment.line.number-sign.python -primes = 5 # type: List[Dict[property:str, ...]], bool # comment +primes = 5 # type: List[Dict[*property:str, ...]], bool # comment # ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python # ^^^^ comment.line.type-hint keyword.other.annotation.type-comment # ^ comment.line.type-hint punctuation.separator.annotation.type-comment @@ -1366,16 +1368,33 @@ class Starship: # ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.begin.python # ^^^^ comment.line.type-hint meta.item-access.arguments.python support.class.python # ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.begin.python -# ^^^^^^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.function.builtin.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.slice.python -# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.type.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.sequence.python -# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python constant.language.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.end.python -# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.end.python -# ^ comment.line.type-hint punctuation.separator.sequence.python -# ^^^^ comment.line.type-hint support.type.python -# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python invalid.illegal +# ^^^^^^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.function.builtin.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.slice.python +# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.type.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.sequence.python +# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python constant.language.python +# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint punctuation.separator.sequence.python +# ^^^^ comment.line.type-hint support.type.python +# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python + +# Python 2.7 function annotations. +def function(a, b, *c, **d): + # type: (int, str, *List[str], **bool) -> Dict[str, str] # type: noncomment +# ^ comment.line.type-hint punctuation.section.parens.begin.python +# ^^^ comment.line.type-hint meta.function.parameters.python support.type.python +# ^ comment.line.type-hint meta.function.parameters.python punctuation.separator.sequence.python +# ^ comment.line.type-hint meta.function.parameters.python keyword.operator.unpacking.sequence.python +# ^^^^ comment.line.type-hint meta.function.parameters.python support.class.python +# ^^ comment.line.type-hint meta.function.parameters.python keyword.operator.unpacking.mapping.python +# ^^^^ comment.line.type-hint meta.function.parameters.python support.type.python +# ^ comment.line.type-hint punctuation.section.parens.end.python +# ^^ comment.line.type-hint meta.function.return-type.python punctuation.separator.sequence.python meta.function.return-type.python +# ^^^^ meta.function.return-type.python support.class.python +# ^ meta.function.return-type.python comment.line.number-sign.python punctuation.definition.comment.python +# ^^^^^^^^^^^^^^^^ meta.function.return-type.python comment.line.number-sign.python # <- - meta From 56ff18ef3ea428049830a25c12c72c1609523f72 Mon Sep 17 00:00:00 2001 From: TeamSpen210 Date: Thu, 17 Oct 2019 18:07:20 +1000 Subject: [PATCH 3/6] [Python] Small fixes/improvements to type comments - Add Python to type hint scopes - Highlight Mypy's ignore codes - handle type: ignore_something_else as a non-special class name --- Python/Python.sublime-syntax | 58 +++++++++++++++++++-------- Python/syntax_test_python.py | 76 +++++++++++++++++++++++------------- 2 files changed, 90 insertions(+), 44 deletions(-) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index ec68794b80..d51fb8701b 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -402,32 +402,56 @@ contexts: pop: true comments: - # Type-ignore must be the only hint allowed! - - match: '(#)\s*(type)(:)\s*(ignore)' + # Type-ignore can only be used by itself. + - match: '(#)\s*(type)(:)\s*(ignore)\b' + scope: comment.line.type-hint.python captures: 1: punctuation.definition.comment.python comment.line.number-sign.python - 2: keyword.other.annotation.type-comment - 3: punctuation.separator.annotation.type-comment - 4: keyword.other.annotation.type-comment.ignore - set: - - meta_scope: comment.line.type-hint - - include: comments_noannotation - - match: \n - pop: true - - match: \S - scope: invalid.illegal + 2: keyword.other.annotation.type-comment.python + 3: punctuation.separator.annotation.type-comment.python + 4: keyword.other.annotation.type-comment.ignore.python + set: comments_afterignore - match: '(#)\s*(type)(:)' - scope: comment.line.type-hint + scope: comment.line.type-hint.python captures: 1: punctuation.definition.comment.python comment.line.number-sign.python - 2: keyword.other.annotation.type-comment - 3: punctuation.separator.annotation.type-comment + 2: keyword.other.annotation.type-comment.python + 3: punctuation.separator.annotation.type-comment.python embed: type-comment escape: '\n' - embed_scope: comment.line.type-hint + embed_scope: comment.line.type-hint.python - include: comments_noannotation + comments_afterignore: + - meta_scope: comment.line.type-hint.python + - include: comments_noannotation + - match: \n + pop: true + - match: \[ + # Mypy's # type: ignore[error-code1, error-code2] + scope: punctuation.section.brackets.begin.python + set: + - meta_scope: comment.line.type-hint.python + - match: \[ + scope: invalid.illegal.python + - match: '[a-zA-Z_-][a-zA-Z_0-9-]+' + scope: comment.line.type-hint.python.mypy-errorcode + - match: '[0-9]' + scope: invalid.illegal.python + - match: ',' + scope: punctuation.separator.sequence.python.mypy-errorcode + - match: \] + scope: punctuation.section.brackets.end.python + set: + # Don't allow a second block. + - match: \[ + scope: invalid.illegal.python + pop: true + - include: comments_afterignore + - match: \S + scope: invalid.illegal.python + type-comment: - include: comments_noannotation @@ -489,7 +513,7 @@ contexts: - match: \} scope: invalid.illegal.stray.brace.curly.python - match: \S - scope: invalid.illegal + scope: invalid.illegal.python comments_noannotation: - match: "#" diff --git a/Python/syntax_test_python.py b/Python/syntax_test_python.py index 73cf87ff7e..6b718ddefd 100644 --- a/Python/syntax_test_python.py +++ b/Python/syntax_test_python.py @@ -1360,38 +1360,60 @@ class Starship: # ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python # ^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint comment.line.number-sign.python +# This is not a type-ignore comment. +value = 3 # type: ignore_class_starting_with_ignore +# ^ comment.line.type-hint.python punctuation.definition.comment.python comment.line.number-sign.python +# ^^^^ comment.line.type-hint.python keyword.other.annotation.type-comment.python +# ^ comment.line.type-hint.python punctuation.separator.annotation.type-comment.python +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint.python support.class.python + +# Mypy ignore extension +value = 3 # type: ignore[error-code-1, 4noint][error] +# ^^^^^^ comment.line.type-hint.python keyword.other.annotation.type-comment.ignore +# ^ comment.line.type-hint.python comment.line.type-hint.python punctuation.section.brackets.begin.python +# ^^^^^^^^^^^^ comment.line.type-hint.python comment.line.type-hint.python.mypy-errorcode +# ^ comment.line.type-hint.python punctuation.separator.sequence.python.mypy-errorcode +# ^ invalid.illegal.python +# ^ comment.line.type-hint.python punctuation.section.brackets.end.python +# ^ invalid.illegal.python + +# With/for allow commas +for a, b in lst: # type: str, int +# ^ comment.line.type-hint.python punctuation.separator.sequence.python + + primes = 5 # type: List[Dict[*property:str, ...]], bool # comment -# ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python -# ^^^^ comment.line.type-hint keyword.other.annotation.type-comment -# ^ comment.line.type-hint punctuation.separator.annotation.type-comment -# ^^^^ comment.line.type-hint support.class.python -# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.begin.python -# ^^^^ comment.line.type-hint meta.item-access.arguments.python support.class.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.begin.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python invalid.illegal -# ^^^^^^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.function.builtin.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.slice.python -# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python support.type.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.sequence.python -# ^^^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.arguments.python constant.language.python -# ^ comment.line.type-hint meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.end.python -# ^ comment.line.type-hint meta.item-access.python punctuation.section.brackets.end.python -# ^ comment.line.type-hint punctuation.separator.sequence.python -# ^^^^ comment.line.type-hint support.type.python -# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python +# ^ comment.line.type-hint.python punctuation.definition.comment.python comment.line.number-sign.python +# ^^^^ comment.line.type-hint.python keyword.other.annotation.type-comment.python +# ^ comment.line.type-hint.python punctuation.separator.annotation.type-comment.python +# ^^^^ comment.line.type-hint.python support.class.python +# ^ comment.line.type-hint.python meta.item-access.python punctuation.section.brackets.begin.python +# ^^^^ comment.line.type-hint.python meta.item-access.arguments.python support.class.python +# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.begin.python +# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python invalid.illegal.python +# ^^^^^^^^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python support.function.builtin.python +# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.slice.python +# ^^^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python support.type.python +# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.sequence.python +# ^^^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python constant.language.python +# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint.python meta.item-access.python punctuation.section.brackets.end.python +# ^ comment.line.type-hint.python punctuation.separator.sequence.python +# ^^^^ comment.line.type-hint.python support.type.python +# ^ comment.line.type-hint.python comment.line.number-sign.python punctuation.definition.comment.python # Python 2.7 function annotations. def function(a, b, *c, **d): # type: (int, str, *List[str], **bool) -> Dict[str, str] # type: noncomment -# ^ comment.line.type-hint punctuation.section.parens.begin.python -# ^^^ comment.line.type-hint meta.function.parameters.python support.type.python -# ^ comment.line.type-hint meta.function.parameters.python punctuation.separator.sequence.python -# ^ comment.line.type-hint meta.function.parameters.python keyword.operator.unpacking.sequence.python -# ^^^^ comment.line.type-hint meta.function.parameters.python support.class.python -# ^^ comment.line.type-hint meta.function.parameters.python keyword.operator.unpacking.mapping.python -# ^^^^ comment.line.type-hint meta.function.parameters.python support.type.python -# ^ comment.line.type-hint punctuation.section.parens.end.python -# ^^ comment.line.type-hint meta.function.return-type.python punctuation.separator.sequence.python meta.function.return-type.python +# ^ comment.line.type-hint.python punctuation.section.parens.begin.python +# ^^^ comment.line.type-hint.python meta.function.parameters.python support.type.python +# ^ comment.line.type-hint.python meta.function.parameters.python punctuation.separator.sequence.python +# ^ comment.line.type-hint.python meta.function.parameters.python keyword.operator.unpacking.sequence.python +# ^^^^ comment.line.type-hint.python meta.function.parameters.python support.class.python +# ^^ comment.line.type-hint.python meta.function.parameters.python keyword.operator.unpacking.mapping.python +# ^^^^ comment.line.type-hint.python meta.function.parameters.python support.type.python +# ^ comment.line.type-hint.python punctuation.section.parens.end.python +# ^^ comment.line.type-hint.python meta.function.return-type.python punctuation.separator.sequence.python meta.function.return-type.python # ^^^^ meta.function.return-type.python support.class.python # ^ meta.function.return-type.python comment.line.number-sign.python punctuation.definition.comment.python # ^^^^^^^^^^^^^^^^ meta.function.return-type.python comment.line.number-sign.python From 14bf602507aa0bc4b0d53dc343faf0fe9c1111eb Mon Sep 17 00:00:00 2001 From: wbond Date: Tue, 21 Jul 2020 12:18:32 -0400 Subject: [PATCH 4/6] [Python] Clean up type comment scopes - Removes some out-of-order stacked scopes - Removes some duplicated scopes - Removes some improper suffixes - Changes context names to fix existing style - Simplifies scope assertions in test file --- Python/Python.sublime-syntax | 70 +++++++++++------------ Python/syntax_test_python.py | 108 +++++++++++++++++++---------------- 2 files changed, 94 insertions(+), 84 deletions(-) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index ca128548df..04726498f6 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -415,55 +415,67 @@ contexts: - match: '(#)\s*(type)(:)\s*(ignore)\b' scope: comment.line.type-hint.python captures: - 1: punctuation.definition.comment.python comment.line.number-sign.python + 1: punctuation.definition.comment.python 2: keyword.other.annotation.type-comment.python 3: punctuation.separator.annotation.type-comment.python 4: keyword.other.annotation.type-comment.ignore.python - set: comments_afterignore + push: comments-afterignore - match: '(#)\s*(type)(:)' scope: comment.line.type-hint.python captures: - 1: punctuation.definition.comment.python comment.line.number-sign.python + 1: punctuation.definition.comment.python 2: keyword.other.annotation.type-comment.python 3: punctuation.separator.annotation.type-comment.python - embed: type-comment - escape: '\n' - embed_scope: comment.line.type-hint.python + push: type-comment + - match: "#" + scope: punctuation.definition.comment.python + push: comments-basic-remainder + + comments-basic: + - match: "#" + scope: punctuation.definition.comment.python + set: comments-basic-remainder - - include: comments_noannotation + comments-basic-remainder: + - meta_scope: comment.line.number-sign.python + - match: \n + pop: true - comments_afterignore: - - meta_scope: comment.line.type-hint.python - - include: comments_noannotation + comments-afterignore: + - meta_content_scope: comment.line.type-hint.python - match: \n pop: true + - match: '(?=#)' + set: comments-basic - match: \[ # Mypy's # type: ignore[error-code1, error-code2] scope: punctuation.section.brackets.begin.python set: - - meta_scope: comment.line.type-hint.python + - meta_content_scope: comment.line.type-hint.python - match: \[ scope: invalid.illegal.python - match: '[a-zA-Z_-][a-zA-Z_0-9-]+' - scope: comment.line.type-hint.python.mypy-errorcode - - match: '[0-9]' + scope: constant.other.error-code.python + - match: '[0-9]\w*' scope: invalid.illegal.python - match: ',' - scope: punctuation.separator.sequence.python.mypy-errorcode + scope: punctuation.separator.sequence.python - match: \] scope: punctuation.section.brackets.end.python set: - # Don't allow a second block. - - match: \[ + # Don't allow a second block. + - meta_content_scope: comment.line.type-hint.python + - match: '(?=#)' + set: comments-basic + - match: \S scope: invalid.illegal.python - pop: true - - include: comments_afterignore - match: \S scope: invalid.illegal.python type-comment: - - include: comments_noannotation - + - meta_content_scope: comment.line.type-hint.python + - match: '(?=#)' + set: comments-basic # Don't use expressions here, only a few types are reasonable # as annotations. - include: constants @@ -472,7 +484,6 @@ contexts: - include: builtin-functions - match: '{{identifier}}' scope: support.class.python - - match: '\s*(\[)' captures: 1: meta.item-access.python punctuation.section.brackets.begin.python @@ -483,8 +494,8 @@ contexts: pop: true - match: ':' scope: punctuation.separator.slice.python + - match: '(?=#)' - include: type-comment - - match: '\(' scope: punctuation.section.parens.begin.python push: @@ -494,25 +505,22 @@ contexts: pop: true - match: \( scope: invalid.illegal.stray.brace.round.python - - match: \*{3,} scope: invalid.illegal.syntax.python - match: \*\* scope: keyword.operator.unpacking.mapping.python - match: \* scope: keyword.operator.unpacking.sequence.python - - match: '->' scope: invalid.illegal.stray.return.arrow.python - include: type-comment - - match: '->' - scope: punctuation.separator.sequence.python meta.function.return-type.python + scope: meta.function.return-type.python punctuation.separator.sequence.python set: + - meta_content_scope: comment.line.type-hint.python meta.function.return-type.python - match: '->' scope: invalid.illegal.stray.return.arrow.python - include: type-comment - - meta_scope: meta.function.return-type.python - match: ',' scope: punctuation.separator.sequence.python - match: \) @@ -524,14 +532,6 @@ contexts: - match: \S scope: invalid.illegal.python - comments_noannotation: - - match: "#" - scope: punctuation.definition.comment.python - push: - - meta_scope: comment.line.number-sign.python - - match: \n - pop: true - constants: - match: \b(None|True|False|Ellipsis|NotImplemented|__debug__)\b scope: constant.language.python diff --git a/Python/syntax_test_python.py b/Python/syntax_test_python.py index 6925e7bef2..8438960568 100644 --- a/Python/syntax_test_python.py +++ b/Python/syntax_test_python.py @@ -1490,70 +1490,80 @@ class Starship: # Type comments - type: ignore must be by itself. primes = 5 # type: ignore # type: not-a-type-comment -# ^ comment.line.type-hint punctuation.definition.comment.python comment.line.number-sign.python -# ^^^^ comment.line.type-hint keyword.other.annotation.type-comment -# ^ comment.line.type-hint punctuation.separator.annotation.type-comment -# ^^^^^^ comment.line.type-hint keyword.other.annotation.type-comment.ignore -# ^ comment.line.type-hint comment.line.number-sign.python punctuation.definition.comment.python -# ^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint comment.line.number-sign.python +# ^^^^^^^^^^^^^^^ comment.line.type-hint - comment.line.number-sign +# ^ punctuation.definition.comment +# ^^^^ keyword.other.annotation.type-comment +# ^ punctuation.separator.annotation.type-comment +# ^^^^^^ keyword.other.annotation.type-comment.ignore +# ^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.number-sign - comment.line.type-hint +# ^ punctuation.definition.comment # This is not a type-ignore comment. value = 3 # type: ignore_class_starting_with_ignore -# ^ comment.line.type-hint.python punctuation.definition.comment.python comment.line.number-sign.python -# ^^^^ comment.line.type-hint.python keyword.other.annotation.type-comment.python -# ^ comment.line.type-hint.python punctuation.separator.annotation.type-comment.python -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint.python support.class.python +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint - comment.line.number-sign +# ^ punctuation.definition.comment +# ^^^^ keyword.other.annotation.type-comment +# ^ punctuation.separator.annotation.type-comment +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ support.class # Mypy ignore extension -value = 3 # type: ignore[error-code-1, 4noint][error] -# ^^^^^^ comment.line.type-hint.python keyword.other.annotation.type-comment.ignore -# ^ comment.line.type-hint.python comment.line.type-hint.python punctuation.section.brackets.begin.python -# ^^^^^^^^^^^^ comment.line.type-hint.python comment.line.type-hint.python.mypy-errorcode -# ^ comment.line.type-hint.python punctuation.separator.sequence.python.mypy-errorcode -# ^ invalid.illegal.python -# ^ comment.line.type-hint.python punctuation.section.brackets.end.python -# ^ invalid.illegal.python +value = 3 # type: ignore[error-code-1, 4noint][error] # foo +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint - comment.line.number-sign +# ^^^^^^ keyword.other.annotation.type-comment.ignore +# ^ punctuation.section.brackets.begin +# ^^^^^^^^^^^^ constant.other.error-code +# ^ punctuation.separator.sequence +# ^^^^^^ invalid.illegal +# ^ punctuation.section.brackets.end +# ^^^^^^^ invalid.illegal +# ^^^^^ comment.line.number-sign - comment.line.type-hint # With/for allow commas for a, b in lst: # type: str, int -# ^ comment.line.type-hint.python punctuation.separator.sequence.python +# ^ comment.line.type-hint punctuation.separator.sequence primes = 5 # type: List[Dict[*property:str, ...]], bool # comment -# ^ comment.line.type-hint.python punctuation.definition.comment.python comment.line.number-sign.python -# ^^^^ comment.line.type-hint.python keyword.other.annotation.type-comment.python -# ^ comment.line.type-hint.python punctuation.separator.annotation.type-comment.python -# ^^^^ comment.line.type-hint.python support.class.python -# ^ comment.line.type-hint.python meta.item-access.python punctuation.section.brackets.begin.python -# ^^^^ comment.line.type-hint.python meta.item-access.arguments.python support.class.python -# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.begin.python -# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python invalid.illegal.python -# ^^^^^^^^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python support.function.builtin.python -# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.slice.python -# ^^^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python support.type.python -# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python punctuation.separator.sequence.python -# ^^^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.arguments.python constant.language.python -# ^ comment.line.type-hint.python meta.item-access.arguments.python meta.item-access.python punctuation.section.brackets.end.python -# ^ comment.line.type-hint.python meta.item-access.python punctuation.section.brackets.end.python -# ^ comment.line.type-hint.python punctuation.separator.sequence.python -# ^^^^ comment.line.type-hint.python support.type.python -# ^ comment.line.type-hint.python comment.line.number-sign.python punctuation.definition.comment.python +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint - comment.line.number-sign +# ^ punctuation.definition.comment +# ^^^^ keyword.other.annotation.type-comment +# ^ punctuation.separator.annotation.type-comment +# ^^^^ support.class +# ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.item-access +# ^punctuation.section.brackets.begin +# ^^^^^^^^^^^^^^^^^^^^^^^^ meta.item-access.arguments +# ^^^^ support.class +# ^ meta.item-access punctuation.section.brackets.begin +# ^ invalid.illegal +# ^^^^^^^^ support.function.builtin +# ^ punctuation.separator.slice +# ^^^ support.type +# ^ punctuation.separator.sequence +# ^^^ constant.language +# ^ punctuation.section.brackets.end +# ^ punctuation.section.brackets.end +# ^ punctuation.separator.sequence +# ^^^^ support.type +# ^^^^^^^^^^ comment.line.number-sign +# ^ punctuation.definition.comment # Python 2.7 function annotations. def function(a, b, *c, **d): # type: (int, str, *List[str], **bool) -> Dict[str, str] # type: noncomment -# ^ comment.line.type-hint.python punctuation.section.parens.begin.python -# ^^^ comment.line.type-hint.python meta.function.parameters.python support.type.python -# ^ comment.line.type-hint.python meta.function.parameters.python punctuation.separator.sequence.python -# ^ comment.line.type-hint.python meta.function.parameters.python keyword.operator.unpacking.sequence.python -# ^^^^ comment.line.type-hint.python meta.function.parameters.python support.class.python -# ^^ comment.line.type-hint.python meta.function.parameters.python keyword.operator.unpacking.mapping.python -# ^^^^ comment.line.type-hint.python meta.function.parameters.python support.type.python -# ^ comment.line.type-hint.python punctuation.section.parens.end.python -# ^^ comment.line.type-hint.python meta.function.return-type.python punctuation.separator.sequence.python meta.function.return-type.python -# ^^^^ meta.function.return-type.python support.class.python -# ^ meta.function.return-type.python comment.line.number-sign.python punctuation.definition.comment.python -# ^^^^^^^^^^^^^^^^ meta.function.return-type.python comment.line.number-sign.python +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint +# ^ punctuation.section.parens.begin +# ^^^ meta.function.parameters support.type +# ^ meta.function.parameters punctuation.separator.sequence +# ^ meta.function.parameters keyword.operator.unpacking.sequence +# ^^^^ meta.function.parameters support.class +# ^^ meta.function.parameters keyword.operator.unpacking.mapping +# ^^^^ meta.function.parameters support.type +# ^ punctuation.section.parens.end +# ^^^^^^^^^^^^^^^^^^ meta.function.return-type +# ^^ punctuation.separator.sequence +# ^^^^ support.class +# ^^^^^^^^^^^^^^^^^^ comment.line.number-sign - comment.line.type-hint +# ^ punctuation.definition.comment ################## From e3b57a9c97b073bd307edbfaae2986268ebcb385 Mon Sep 17 00:00:00 2001 From: wbond Date: Wed, 22 Jul 2020 10:32:29 -0400 Subject: [PATCH 5/6] [Python] Fixed various type comment tests to use valid code --- Python/syntax_test_python.py | 53 ++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/Python/syntax_test_python.py b/Python/syntax_test_python.py index 8438960568..d09fa1224d 100644 --- a/Python/syntax_test_python.py +++ b/Python/syntax_test_python.py @@ -1519,51 +1519,46 @@ class Starship: # ^^^^^ comment.line.number-sign - comment.line.type-hint # With/for allow commas -for a, b in lst: # type: str, int -# ^ comment.line.type-hint punctuation.separator.sequence +for a, b in lst: # type: (str, int) +# ^ comment.line.type-hint punctuation.separator.sequence -primes = 5 # type: List[Dict[*property:str, ...]], bool # comment -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint - comment.line.number-sign +primes = 5 # type: List[Dict[property:str, ...]] # comment +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint - comment.line.number-sign # ^ punctuation.definition.comment # ^^^^ keyword.other.annotation.type-comment # ^ punctuation.separator.annotation.type-comment # ^^^^ support.class -# ^^^^^^^^^^^^^^^^^^^^^^^^^^ meta.item-access +# ^^^^^^^^^^^^^^^^^^^^^^^^^ meta.item-access # ^punctuation.section.brackets.begin -# ^^^^^^^^^^^^^^^^^^^^^^^^ meta.item-access.arguments +# ^^^^^^^^^^^^^^^^^^^^^^^ meta.item-access.arguments # ^^^^ support.class # ^ meta.item-access punctuation.section.brackets.begin -# ^ invalid.illegal -# ^^^^^^^^ support.function.builtin -# ^ punctuation.separator.slice -# ^^^ support.type -# ^ punctuation.separator.sequence -# ^^^ constant.language +# ^^^^^^^^ support.function.builtin +# ^ punctuation.separator.slice +# ^^^ support.type +# ^ punctuation.separator.sequence +# ^^^ constant.language +# ^ punctuation.section.brackets.end # ^ punctuation.section.brackets.end -# ^ punctuation.section.brackets.end -# ^ punctuation.separator.sequence -# ^^^^ support.type -# ^^^^^^^^^^ comment.line.number-sign -# ^ punctuation.definition.comment +# ^^^^^^^^^^ comment.line.number-sign +# ^ punctuation.definition.comment # Python 2.7 function annotations. def function(a, b, *c, **d): - # type: (int, str, *List[str], **bool) -> Dict[str, str] # type: noncomment -# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint + # type: (int, str, List[str], bool) -> Dict[str, str] # type: noncomment +# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ comment.line.type-hint # ^ punctuation.section.parens.begin # ^^^ meta.function.parameters support.type # ^ meta.function.parameters punctuation.separator.sequence -# ^ meta.function.parameters keyword.operator.unpacking.sequence -# ^^^^ meta.function.parameters support.class -# ^^ meta.function.parameters keyword.operator.unpacking.mapping -# ^^^^ meta.function.parameters support.type -# ^ punctuation.section.parens.end -# ^^^^^^^^^^^^^^^^^^ meta.function.return-type -# ^^ punctuation.separator.sequence -# ^^^^ support.class -# ^^^^^^^^^^^^^^^^^^ comment.line.number-sign - comment.line.type-hint -# ^ punctuation.definition.comment +# ^^^^ meta.function.parameters support.class +# ^^^^ meta.function.parameters support.type +# ^ punctuation.section.parens.end +# ^^^^^^^^^^^^^^^^^^ meta.function.return-type +# ^^ punctuation.separator.sequence +# ^^^^ support.class +# ^^^^^^^^^^^^^^^^^^ comment.line.number-sign - comment.line.type-hint +# ^ punctuation.definition.comment ################## From 97ae6387506e87523f9e31232a933cbe8ef01327 Mon Sep 17 00:00:00 2001 From: wbond Date: Wed, 22 Jul 2020 10:41:46 -0400 Subject: [PATCH 6/6] [Python] Ensure type comments are exited at newline --- Python/Python.sublime-syntax | 10 ++++++++++ Python/syntax_test_python.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/Python/Python.sublime-syntax b/Python/Python.sublime-syntax index 45f0acac49..35ee5c01b0 100644 --- a/Python/Python.sublime-syntax +++ b/Python/Python.sublime-syntax @@ -452,6 +452,8 @@ contexts: scope: punctuation.section.brackets.begin.python set: - meta_content_scope: comment.line.type-hint.python + - match: \n + pop: true - match: \[ scope: invalid.illegal.python - match: '[a-zA-Z_-][a-zA-Z_0-9-]+' @@ -465,6 +467,8 @@ contexts: set: # Don't allow a second block. - meta_content_scope: comment.line.type-hint.python + - match: \n + pop: true - match: '(?=#)' set: comments-basic - match: \S @@ -474,6 +478,8 @@ contexts: type-comment: - meta_content_scope: comment.line.type-hint.python + - match: \n + pop: true - match: '(?=#)' set: comments-basic # Don't use expressions here, only a few types are reasonable @@ -489,6 +495,8 @@ contexts: 1: meta.item-access.python punctuation.section.brackets.begin.python push: - meta_content_scope: meta.item-access.arguments.python + - match: (?=\n) + pop: true - match: \] scope: meta.item-access.python punctuation.section.brackets.end.python pop: true @@ -500,6 +508,8 @@ contexts: scope: punctuation.section.parens.begin.python push: - meta_content_scope: meta.function.parameters.python + - match: (?=\n) + pop: true - match: \) scope: punctuation.section.parens.end.python pop: true diff --git a/Python/syntax_test_python.py b/Python/syntax_test_python.py index d09fa1224d..ce95391287 100644 --- a/Python/syntax_test_python.py +++ b/Python/syntax_test_python.py @@ -1560,6 +1560,22 @@ def function(a, b, *c, **d): # ^^^^^^^^^^^^^^^^^^ comment.line.number-sign - comment.line.type-hint # ^ punctuation.definition.comment +class TypeCommentTest: + member = [] # type: List[dict] + + def __del__(self) -> None: +# ^^^^ constant.language +# ^ punctuation.section + pass +# ^^^^ keyword.control + +class TypeCommentTest2: + member = [] # type: List[dict] + + def __del__(self) -> None: + pass +# ^^^^ keyword.control + ################## # Assignment Expressions