# [译]PEP 484 -- 类型提示

## Abstract

type system 支持 unions，generic 类型， Any类型兼容所有的类型。his latter feature is taken from the idea of gradual typing. Gradual typing and the full type system are explained in PEP 483.

## Acceptable type hints

annotation 仅仅是一些特殊的注释，或者一个 stub file。除此之外并无特殊之处

annotation 应该足够的简单，否则的话静态分析工具并不能很好的解析类型。比如说动态计算的类型是不会被允许的。

In addition to the above, the following special constructs defined below may be used: None, Any, Union, Tuple, Callable, all ABCs and stand-ins for concrete classes exported from typing (e.g. Sequence and Dict), type variables, and type aliases.

## Type aliases

Url尽量首字母大写，这样体现了这是用户自定义的类型。

## Callable

Callable[[Arg1Type, Arg2Type], ReturnType] 前面的是接受参数的类型，后面那个是返回的类型。

typing.callable将重复的进行 collections.abc.Callable 的工作，isinstance(x, typing.Callable)会被解析成isinstance(x, collections.abc.Callable),但是isinstance(x, typing.Callable[...])不会被支持。

## 泛型 Generics

Any被认为是任意类型，比如下面例子中的泛型被认为是elements: List

## instantiating generic classes and type erasure， 展示泛型类以及类型擦除

Using generic classes (parameterized or not) to access attributes will result in type check failure. Outside the class definition body, a class attribute cannot be assigned, and can only be looked up by accessing it through a class instance that does not have an instance attribute with the same name:

## Arbitrary generic types as base classes

SymbolTable is a subclass of dict and a subtype of Dict[str, List[Node]].

## Compatibility with other uses of function annotations

• a # type: ignore comment;
• a @no_type_check decorator on a class or function;
• a custom class or function decorator marked with @no_type_check_decorator.

## NewType helper function

NewType("unique_name", base_class)

## Stub files

• Extension modules
• Third-party modules whose authors have not yet added type hints
• Standard library modules for which type hints have not yet been written
• Modules that must be compatible with Python 2 and 3
• Modules that use annotations for other purposes