You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Traceback (most recent call last):
File "example/sample.py", line 2, in <module>
from tcpflow import TcpFlow
File "/Users/mrkaspa/code/py/tcpflow.py/tcpflow/__init__.py", line 1, in <module>
from .tcpflow import TcpFlow
File "/Users/mrkaspa/code/py/tcpflow.py/tcpflow/tcpflow.py", line 8, in <module>
class TcpFlow(object):
File "/Users/mrkaspa/code/py/tcpflow.py/tcpflow/tcpflow.py", line 16, in TcpFlow
def __enter__(self) -> TcpFlow:
NameError: name 'TcpFlow' is not defined
Reacted by Alexander Regueiro, Leo Gallucci, Danilo Batista de Queiroz, Felipe S. S. Schneider, Pranithan and Genaro Camele
from __future__ importannotationsclassTcpFlow(object):
def__enter__(self) ->TcpFlow:
returnself
(notice the quotes around the return type have been removed)
Reacted by asmith26, Francis Niu 牛飞龙, Khaïs COLIN, Miguel Toro Bonilla, Eero Ruohola, Kunal Mehta, Nikolai Zujev, Matt Stancliff, Rafael Setton, Josh Wedekind and 24 more
which is important as the SC has tentatively accepted PEP 649, which indicates the future will be to move away from from __future__ import annotations.
Reacted by Garrett Reynolds, Connor Adams, Ludal, Alejandro Martínez and Timor Gruber
Activity
ilevkivskyi commentedon Jul 5, 2017
In such cases you should put the name of class in quotes (this is called forward reference), like this:
Garrett-R commentedon Jun 23, 2020
As of Python 3.7, you can now do:
(notice the quotes around the return type have been removed)
johnthagen commentedon Dec 15, 2022
As of Python 3.11, you can now use the
Selftype to express the intent clearly and concisely:This new pattern can be applied to previous versions of Python using
typing_extensions.Selfbackport.Note that this removed the need to use:
which is important as the SC has tentatively accepted PEP 649, which indicates the future will be to move away from
from __future__ import annotations.