<type>documentation</type>
<domain>edit</domain>
<id>analysis-helping-type-hints</id>
<title>Adding Type Hints</title>
<date>$Date: 2007-02-09 12:04:39 -0500 (Fri, 09 Feb 2007) $</date>
<version>$Revision: 14785 $</version>
<product>all</product>

Wing can understand several different kinds of type hints added to Python code.

**PEP484 and PEP 526 Type Annotations**

Adding type hints in the styles standardized by `PEP 484
<https://www.python.org/dev/peps/pep-0484/>`__ (Python 3.5+) and `PEP 526
<https://www.python.org/dev/peps/pep-0526/>`__ (Python 3.6+) is another way to
help Wing understand difficult-to-analyze code.

For example, the following indicates to Wing the argument and return types of the function
``myFunction``:

.. python::

  from typing import Dict, List
  
  def myFunction(arg1: str, arg2: Dict) -> List:
    return arg2.get(arg1, [])
    
The type of variables can be indicated by a comment that follows an assignment:

.. python::

  x = Something() # type: int
  
Or in Python 3.6+ the type can instead be specified inline:

.. python::

  x:int = Something()
  
The types that Wing can recognize include basic types like ``str`` and ``int`` and also
the following from the ``typing`` module: ``List``, ``Tuple``, ``Dict``, ``Set``,
``FrozenSet``, ``Optional``, and ``Union``.

**Type Hinting with isinstance()**

Another way to inform Wing of the type of a variable is to add an ``isinstance`` call to
your code. For example ``isinstance(obj, CMyClass)``. This is useful in older Python
versions, or when combined with debug-only runtime type checking like ``assert
isinstance(obj, CMyClass)``.

In cases where doing this introduces a circular import or other problems, use a 
conditional:

.. python::

  if 0:
    import othermodule
    isinstance(obj, othermodule.CMyClass)

The source code analysis engine will still pick up on the type hint, even though
it is never executed.
