Colloquially, type hint and annotations are used interchangeably in Python. After reading PEP 484, I was unable to disambiguate the terms "type hint" vs "type annotations" from the text. Are they synonyms? Otherwise, do "type hint" refer to the typing system and "type annotations" refer to the written text itself (e.g., functional annotations)?
-
I found this sentence: "Type aliases may be as complex as type hints in annotations" which clearly implies that hints and annotations are two distinct things.Jörg W Mittag– Jörg W Mittag2022-07-18 23:34:27 +00:00Commented Jul 18, 2022 at 23:34
-
1Those terms are defined in the next section of the document: peps.python.org/pep-0484/#acceptable-type-hints Type hints may be built-in classes (including those defined in standard library or third-party extension modules), abstract base classes, types available in the types module, and user-defined classes (including those defined in the standard library or third-party modules). (...) Annotations must be valid expressions that evaluate without raising exceptions at the time the function is definedBen Cottrell– Ben Cottrell2022-07-19 06:18:25 +00:00Commented Jul 19, 2022 at 6:18
Add a comment
|
1 Answer
Type hints is a broader category than type annotations.
While annotations are normally the best format for type hints, there are times when it is more appropriate to represent them by a special comment, or in a separately distributed stub file. (See below for examples.)
It then has short sections describing type comments and stub files