MagicPython icon indicating copy to clipboard operation
MagicPython copied to clipboard

Highlight typing hints in a different color than ordinary code.

Open vistatest opened this issue 5 years ago • 10 comments

Typing hints are displayed in the same color as python variables and statements. That looks somewhat confusing and makes it harder to see at once where is a typing hint, where a variable name is defined and where is an actual code.

E.g., the line below up to 'origin' is in the same color.

  remote: pygit2.Remote = gitrepo.remotes['origin']

vistatest avatar May 16 '19 02:05 vistatest

I agree. Would be nice to be able to apply a different color to type hints. I get the impression a lot of people like them to be just regular code. I'm fine with that as the default, but would be nice to add an extra label so some of us can for example make all type hints semi-transparent.

shadow-light avatar Jul 05 '19 22:07 shadow-light

There are also the type hints on function defs. And they very much deserve some highlighting.

def fun_func(
    big_param: Set[Tuple[int, int]],
    generic_param: List[T],
) -> Mapping[str, T]: ...

cardoso-neto avatar Oct 06 '19 20:10 cardoso-neto

Type annotations of the attributes of a dataclass need proper highlighting as well:

Number = Union[int, float]
@dataclass(frozen=True)
class SomeClass:
    foo: Generator[AnyStr, Number, None] = field(hash=False)
    bar: Iterable[T] = field(compare=False)
    amount: Number = -1

cardoso-neto avatar Oct 19 '19 20:10 cardoso-neto

When using generic types (typevar) on classes (inheriting from it), no highlighting appears.

V = TypeVar("V", FirstType, SomeOtherType)
E = TypeVar("E")

class StopIgnoringMe(Generic[V, E], ParentClass):
    pass

class NormalInheritance(ParentClass):
    pass

cardoso-neto avatar Oct 27 '19 21:10 cardoso-neto

I'll see what I can do to highlight type hints better. I guess the starting point would be to at least give them all a scope of their own like meta.typehint.python.

vpetrovykh avatar Oct 28 '19 15:10 vpetrovykh

@vpetrovykh do you need help with the issue?

dkruk avatar Feb 14 '20 13:02 dkruk

It appears Github's highlighting decided that everything that starts with a capital letter deserves that same color. I'm still inclined towards having separate scopes for typehints and for normal classes, so we can choose different colors.

cardoso-neto avatar Jun 05 '20 04:06 cardoso-neto

I'd also like to have a separate scope for type hints. I was writing some type hints in Django Models (so the fields show up as the real type and not the orm field type), but it made the code look ugly since the variable and the type hint and the field class name where all white, you couldn't easily see where the type hint ends and where the orm field class starts! For now I settled for using # type comments instead since these do have their own scope and look better at the end of the line (I made the semi-transparent).

hassanselim0 avatar Jul 09 '20 04:07 hassanselim0

For having the same scope support.type.python, it's not possible to get distinct syntax highlighting for type hints of basic data types like int or str and casting methods like int() and str()

hierr avatar Jul 12 '21 22:07 hierr

To change color of type hints in VSCode you can use editor settings

...
"editor.semanticTokenColorCustomizations": {
  "[Visual Studio Light]": {
    // Apply to this theme only
    "enabled": true,
    "rules": {
      "*.typeHint:python": "#e4c8c8"
    }
  }
}
...

dkruk avatar Jul 13 '21 10:07 dkruk