Does a "with" statement support type hinting?

Usually type annotations are placed at the API boundaries. In this case the type should be inferred from example.__enter__. In case that function doesn't declare any types, the solution is to create a corresponding stub file in order to help the type checker infer that type.

Specifically this means creating a .pyi file with the same stem as the module from which Example was imported. Then the following code can be added:

class Example:
    def __enter__(self) -> str: ...
    def __exit__(self, exc_type, exc_value, exc_traceback) -> None: ...

PEP 526, which has been implemented in Python 3.6, allows you to annotate variables. You can use, for example,

x: str
with example() as x:
    [...]

or

with example() as x:
    x: str
    [...]