Syntax Highlighting for Type Hints

已回答

PyCharm (pycharm 2016.3.2, python 3.5) implements syntax highlighting for Type Hints (PEP 484). So the following...

def my_func(x: float, y: float) -> float:
    ...

will result in the word "float" being colored blue (at least in default settings).

However, if you define your own types this highlighting feature is lost. Example:

import numpy
from typing import Union

my_type = Union[float, numpy.float64]

def my_func(x: my_type, y: my_type) -> my_type:
    ...

PyCharm performs its type checking like a champ! But, now "my_type" loses its syntax highlighting and defaults to regular text color/font. If you have lots of function arguments it can get hard to read. Is there a way to turn on syntax highlighting for custom types? Or a better implementation of the above?

Thanks!

1

Hi Joe,

It's highlighted blue not because it's some special kind of type hints, but because type annotations are processed as normal Python expressions, and "int" is a built-in name so the settings for Settings | Editor | Colors & Fonts | Python | Built-in name apply here. There is a feature request about adding a dedicated category for type annotations in syntax highlighting preferences, however. We're still discussing it, but don't hesitate to leave your vote there if you're interested.

1

请先登录再写评论。