Adding Type Hints
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 (Python 3.5+) and PEP 526 (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:
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:
x = Something() # type: int
Or in Python 3.6+ the type can instead be specified inline:
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:
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.