-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type of the same class inside the class #3661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In such cases you should put the name of class in quotes (this is called forward reference), like this: class TcpFlow(object):
def __enter__(self) -> 'TcpFlow':
return self |
As of Python 3.7, you can now do: from __future__ import annotations
class TcpFlow(object):
def __enter__(self) -> TcpFlow:
return self (notice the quotes around the return type have been removed) |
As of Python 3.11, you can now use the from typing import Self
class TcpFlow:
def __enter__(self) -> Self:
return self This new pattern can be applied to previous versions of Python using Note that this removed the need to use: from __future__ import annotations which is important as the SC has tentatively accepted PEP 649, which indicates the future will be to move away from |
I have a method that returns itself but it doesn't know the type of the class inside
I got this error
The text was updated successfully, but these errors were encountered: