Skip to content

Commit

Permalink
[Python] Ensure union operator scope consistency
Browse files Browse the repository at this point in the history
This commit scopes `|` as normal arithmetic operator in type comments for
consistency reasons, because type hints in annotations (variables, parameters,
return-type) need to use normal expression syntax.
  • Loading branch information
deathaxe committed Jun 19, 2023
1 parent 237251b commit 3e550f4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
7 changes: 4 additions & 3 deletions Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,15 @@ contexts:
- include: type-hint-body

type-constants:
# Note: Enables `None = None` in type hints.
- match: None\b
scope: constant.language.null.python

type-separators:
# Note: Scoped as normal arithmetic operator for consistency reasons with
# type-hints in annotations, which share syntax with normal expressions
- match: \|
scope: keyword.operator.logical.union.python
scope: keyword.operator.arithmetic.python
- include: sequence-separators

###[ DECORATORS ]#############################################################
Expand Down Expand Up @@ -911,7 +914,6 @@ contexts:
- function-parameter-annotation-continue
- function-parameter-annotation-end
- include: type-constants
- include: type-separators
- include: expression-in-a-group

function-parameter-annotation-continue:
Expand Down Expand Up @@ -997,7 +999,6 @@ contexts:
- include: line-continuation-or-pop
- include: illegal-assignment-expressions
- include: type-constants
- include: type-separators
- include: expression-in-a-statement

function-definition-end:
Expand Down
35 changes: 30 additions & 5 deletions Python/tests/syntax_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,7 @@ def type_annotations(param1: int, param2: MyType | None , param3: max(2, 3), par
# ^ punctuation.separator.annotation
# ^^^^^^^^^^^^^ meta.type.python
# ^^^^^^ meta.path.python meta.generic-name.python
# ^ keyword.operator.logical.union.python
# ^ keyword.operator.arithmetic.python
# ^^^^ constant.language.null.python
# ^ punctuation.separator.parameters
# ^^^^^^ variable.parameter
Expand Down Expand Up @@ -1821,7 +1821,7 @@ def type_annotation_with_defaults(foo: str | None = None)
# ^ meta.function.parameters.annotation.python - meta.function.parameters.default-value - meta.type
# ^^^^^^ meta.function.parameters.default-value.python - meta.function.parameters.annotation
# ^^^ support.type.python
# ^ keyword.operator.logical.union.python
# ^ keyword.operator.arithmetic.python
# ^^^^ constant.language.null.python
# ^ keyword.operator.assignment.python
# ^^^^ constant.language.null.python
Expand Down Expand Up @@ -2991,7 +2991,7 @@ def __init__(self):
# ^^^^^^^^ - meta.type
# ^ punctuation.separator.annotation.python
# ^^^ support.type.python
# ^ keyword.operator.logical.union.python
# ^ keyword.operator.arithmetic.python
# ^^^^ constant.language.null.python
# ^ keyword.operator.assignment.python
# ^^^^ constant.language.null.python
Expand All @@ -3002,11 +3002,36 @@ def __init__(self):
# ^^^^^^^ - meta.type
# ^ punctuation.separator.annotation.python
# ^^^ support.type.python
# ^ keyword.operator.logical.union.python
# ^ keyword.operator.arithmetic.python
# ^^^^ constant.language.null.python
# ^ keyword.operator.assignment.python
# ^^^ string.quoted.single.python
bar: (str | None) = 'b'
# ^^ - meta.type
# ^^^^^^^^^^^^ meta.type.python meta.group.python
# ^^^^^^^ - meta.type
# ^ punctuation.separator.annotation.python
# ^^^ support.type.python
# ^ keyword.operator.arithmetic.python
# ^^^^ constant.language.null.python
# ^ keyword.operator.assignment.python
# ^^^ string.quoted.single.python
bar: list[str | None] = 'b'
# ^^ - meta.type
# ^^^^ meta.type.python meta.path.python support.type.python
# ^^^^^^^^^^^^ meta.type.python meta.brackets.python
# ^^^^^^^ - meta.type
# ^ punctuation.separator.annotation.python
# ^ punctuation.section.brackets.begin.python
# ^^^ support.type.python
# ^ keyword.operator.arithmetic.python
# ^^^^ constant.language.null.python
# ^ punctuation.section.brackets.end.python
# ^ keyword.operator.assignment.python
# ^^^ string.quoted.single.python
foo: int := 0
# ^ punctuation.separator.annotation.python
# ^^^ meta.type.python meta.path.python support.type.python
Expand Down Expand Up @@ -3172,7 +3197,7 @@ class Starship:
# ^^^ support.type.python
# ^ punctuation.separator.sequence.python
# ^^^ support.type.python
# ^ keyword.operator.logical.union.python
# ^ keyword.operator.arithmetic.python
# ^^^ support.type.python
# ^ punctuation.section.brackets.end.python
Expand Down

0 comments on commit 3e550f4

Please sign in to comment.