Skip to content

Commit

Permalink
add Forward references
Browse files Browse the repository at this point in the history
  • Loading branch information
crazyguitar committed Aug 13, 2018
1 parent 8a23e66 commit 3c211c9
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions docs/notes/python-typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,50 @@ output:
# mypy will not detect type errors
$ mypy --strict foo.py
Forward references
-------------------
Based on PEP 484, if we want to reference a type before it has been declared, we
have to use **string literal** to imply that there is a type of that name later on
in the file.
.. code-block:: python
from typing import Optional
class Tree:
def __init__(
self, data: int,
left: Optional["Tree"], # Forward references.
right: Optional["Tree"]
) -> None:
self.data = data
self.left = left
self.right = right
.. note::
There are some issues that mypy does not complain about Forward References.
Get further information from `Issue#948`_.
.. _Issue\#948: https://github.com/python/mypy/issues/948
.. code-block:: python
class A:
def __init__(self, a: A) -> None: # should fail
self.a = a
output:
.. code-block:: bash
$ mypy --strict type.py
$ echo $?
0
Type alias
----------
Expand Down

0 comments on commit 3c211c9

Please sign in to comment.