Does a "with" statement support type hinting?

Reactgular :

Can you define the type hint for a variable defined with the with syntax?

with example() as x:
    print(x)

I would like to type hint the above to say that x is a str (as an example).

The only work around that I've found is to use an intermediate variable, but this feels hacky.

with example() as x:
    y: str = x
    print(y)

I can't find an example in the typing documentation.

pschill :

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
    [...]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=5149&siteId=1