Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Python] Improve type alias definitions #3917

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions Python/Python.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ variables:
| getattr | globals | hasattr | hash | help | hex | id | input | isinstance
| issubclass | iter | len | locals | map | max | min | next | oct | open
| ord | pow | property | range | repr | reversed | round | setattr | sorted
| staticmethod | sum | super | type | vars | zip
| staticmethod | sum | super | type(?=\s*\() | vars | zip
# Python 2 functions
| apply | cmp | coerce | execfile | intern | raw_input | reduce | reload
| unichr | xrange
Expand Down Expand Up @@ -1042,10 +1042,18 @@ contexts:
###[ TYPE DEFINITIONS ]#######################################################

type-definitions:
- match: ^\s*(type)\b
- match: ^(?=\s*type\b)
branch_point: type-definitions
branch:
- type-definition
- type-definition-fallback

type-definition:
- meta_include_prototype: false
- match: \s*(type)\b
captures:
1: keyword.declaration.class.python
push:
set:
- type-definition-meta
- type-definition-value-assignment
- type-parameter-list
Expand All @@ -1061,14 +1069,25 @@ contexts:
- match: '{{identifier}}'
scope: entity.name.type.alias.python
pop: 1
- include: line-continuation-or-pop
- include: line-continuations
- match: (?=\S)
fail: type-definitions

type-definition-value-assignment:
- match: =
scope: keyword.operator.assignment.python
set: variable-annotation
- include: line-continuation-or-pop

type-definition-fallback:
# Fallback context, used, if `type` doesn't seem to start a type alias
- match: (?={{path}}\s*\()
set:
- function-call-argument-list
- function-call-wrapper
- qualified-name-until-leaf
- include: generic-name

###[ TYPE PARAMETERS ]########################################################

type-parameter-lists:
Expand Down
53 changes: 51 additions & 2 deletions Python/tests/syntax_test_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -2248,8 +2248,8 @@ def method(arg: T):
##################

type
# <- meta.type-alias.python keyword.declaration.class.python
#^^^ meta.type-alias.python keyword.declaration.class.python
# <- meta.generic-name - keyword
#^^^ meta.generic-name - keyword

type Alias # [T: int] = list[T]
# <- meta.type-alias.python keyword.declaration.class.python
Expand Down Expand Up @@ -2320,6 +2320,55 @@ def method(arg: T):
# ^ meta.generic-name.python
# ^ punctuation.section.brackets.end.python

type Alias
# <- meta.type-alias.python
#^^^^^^^^^^^ meta.type-alias.python
# ^^^^ keyword.declaration.class.python
# ^^^^^ entity.name.type.alias.python

type(data)
# <- meta.function-call.identifier.python support.function.builtin.python
#^^^ meta.function-call.identifier.python support.function.builtin.python
# ^^^^^^ meta.function-call.arguments.python
# ^ punctuation.section.arguments.begin.python
# ^^^^ meta.path.python meta.generic-name.python
# ^ punctuation.section.arguments.end.python

type: Alias
# <- meta.generic-name.python
#^^^ meta.generic-name.python
# ^ punctuation.separator.annotation.python
# ^^^^^ meta.type.python meta.path.python meta.generic-name.python

type = 10
# <- meta.generic-name.python
#^^^ meta.generic-name.python
# ^ keyword.operator.assignment.python
# ^^ meta.number.integer.decimal.python constant.numeric.value.python

class Foo:
type: Alias
# ^^^^ meta.generic-name.python
# ^ punctuation.separator.annotation.python
# ^^^^^ meta.type.python meta.path.python meta.generic-name.python

type = 10
# ^^^^ meta.generic-name.python
# ^ keyword.operator.assignment.python
# ^^ meta.number.integer.decimal.python constant.numeric.value.python

def __init__(self, type: int):
# ^^^^ variable.parameter.python
self.type = type
# ^^^^ meta.path.python meta.generic-name.python
# ^^^^ meta.path.python meta.generic-name.python

self.me = type(type)
# ^^^^ meta.function-call.identifier.python support.function.builtin.python
# ^^^^^^ meta.function-call.arguments.python
# ^ punctuation.section.arguments.begin.python
# ^^^^ meta.path.python meta.generic-name.python
# ^ punctuation.section.arguments.end.python

##################
# Decorators
Expand Down
Loading