-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 highlighting could be improved to cover new type alias syntax (PEP 695) #10164
Comments
Excuse my ignorance, but I was confused about the last two cases: _ = type
_ = type(None) As I understand it, in both cases, Edit: Just noticed that GitHub highlights them differently too (so it must assign different token types as well). |
@7ombie While all functions are variables it's not the case that all variables are functions, so when used as a variable you can't/shouldn't highlight it like a function, because you would be inconsistently highlighting variables differently depending on their type. [I think] this is the case for all programming languages that have "first-class functions" or higher order functions. |
@kas2020-commits - I see. I'm sure you're right (about how it works), but that makes no sense to me. I thought it would use the type of the variable, so each variable would be consistently highlighted throughout, indicating what it is. Instead, we're highlighting the same variable differently, based on whether it occurs in a terminal expression (just the name) or a compound expression (like an invocation), which only indicates what I'm doing with it at that time, which isn't informative (I can already see what I'm doing). It's just noise. |
@7ombie It's not quite the same:
The example above, therefore, produces the following:
|
As per PEP 695, starting in version 3.12 Python has new syntax for defining type parameters and type aliases.
To be clear, the tree sitter parser works just fine, it's the highlighting that could be improved.
You can now define type aliases in Python like so:
where
type
should be defined as a@keyword
in/runtime/queries/python/highlights.scm
same asclass
in class definitions. The Tree-sitter sub-tree for the provided code is this:An important caveat is that the literal
type
is not always a keyword - only in this context is it a keyword. By default,type
is the symbol name for a built-in callable class definition, but it can also be considered a type hint.Basically, these are all the major contexts of how
type
should be identified for highlighting purposes:The text was updated successfully, but these errors were encountered: