2

I understand that __init__() is required to return None, but when Visual Studio autocompletes a derived class __init__() for me, it does so like this:

class Base:
    def __init__(self):
        print('Base')

class Derived(Base):
    def __init__(self):
        return super().__init__()  # This part is added by VS Code

It's obviously not a syntax issue, as the Base __init__ is returning None, which the derived class in turn returns as well.

But why even bother having that? What purpose does the return statement serve here?

Michael Kolber
  • 1,309
  • 1
  • 14
  • 23
  • 1
    Yea that's weird. – Neil Aug 09 '19 at 18:21
  • 2
    It might be doing that by default any time you call something in the parent class, regardless of whether it's `__init__` or a regular method? So it autocompletes it that way because that's how it generates a basic implementation of an inherited method. – Simeon Visser Aug 09 '19 at 18:22
  • 2
    Because VS Code is being stupid? `__init__` should never return a value. Add it to the 5000+ other bugs [here](https://github.com/microsoft/vscode/issues). – wim Aug 09 '19 at 18:26
  • @SimeonVisser You're correct, I just tried it with a non-dunder method and it exhibited the same result. – Michael Kolber Aug 09 '19 at 18:28
  • 1
    @wim It's still returning `None`, as long as `super().__init__` also correctly returns `None`. The explicit `return` is just redundant. (But yeah, VS Code is being stupid.) – chepner Aug 09 '19 at 18:31
  • 1
    But in VS Code's defense, it's a question of whether it's worth special-casing redundant but technically correct behavior. Since Python 3 *requires* `__init__` to return `None` now (in Python 2 the return value was simply ignored), I would say yes, it's worth special-casing. – chepner Aug 09 '19 at 18:32
  • 2
    @wim Did some digging, it's already been reported it seems: https://github.com/microsoft/python-language-server/issues/1082 – Michael Kolber Aug 09 '19 at 18:34
  • I'm not a VS code enthusiast or anything but I do think calling it stupid because OP found a special case where it is behaving stupid is a bit harsh. – d_kennetz Aug 09 '19 at 18:34
  • 1
    @d_kennetz Agreed, particularly because it's a bug with the Python extension, not with VS Code itself. – Michael Kolber Aug 09 '19 at 18:35
  • 3
    @d_kennetz I don't think anyone is calling the *developers* of VS Code stupid; it's an accurate description of the code, though, in the sense of "it hasn't been programmed to make a [smart] distinction between `__init__` and other functions". – chepner Aug 09 '19 at 18:35

1 Answers1

2

As per @Simeon Visser's comment, VS Code seems to do this for all inherited methods, and does not discriminate between __init__() and other methods. An issue has already been filed in the Python Language Server repo. The issue has been fixed.

Michael Kolber
  • 1,309
  • 1
  • 14
  • 23