Correct Type annotation for __init__

PythonPython 2.7Typing

Python Problem Overview


What is the correct type annotation for a __init__ function in python?

class MyClass:
    ...

Which of the following would make more sense?

def __init__(self):
    # type: (None) -> None

def __init__(self):
    # type: (MyClass) -> MyClass

def __init__(self):
    # type: (None) -> MyClass

Since we would normally instantiate as myclass = MyClass(), but the __init__ function itself has no return value.

Python Solutions


Solution 1 - Python

self should be omitted from the annotation when it is given as a comment, and __init__() should be marked as -> None. This is all specified explicitly in PEP-0484.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhangcView Question on Stackoverflow
Solution 1 - PythonremramView Answer on Stackoverflow