Skip to content
This repository has been archived by the owner on Apr 14, 2022. It is now read-only.

Auto-completion of __init__(self, ) should not automatically include a "return" statement #1082

Closed
karrtikr opened this issue May 14, 2019 · 3 comments · Fixed by #1435
Closed
Assignees

Comments

@karrtikr
Copy link

@PowerSnail commented on Mon May 06 2019

Environment data

  • VS Code version: 1.33.1
  • Extension version (available under the Extensions sidebar): 2019.4.12954
  • OS and version: Ubuntu 19.04
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.3 (system)
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): any
  • Relevant/affected Python packages and their versions: NA

Expected behaviour

Auto-completion for __init__ method should not generate return super().__init__().

__init__ should always return None. And, explicit return in __init__ is probably not appropriate anyway.

Maybe it should just generate an empty method with a pass, and let the user fill in the the necessary initialization. First, many classes don't inherit anything (other than object) and super().__init__() should not be there. Second, some libraries recommend initializing super by super(MyClass, self).__init__(). Users have to rewrite this anyway.

Actual behaviour

Auto-completion generates something like this when click "tab" when typing __init__:

class NewClass(object):
    
    def __init__(self, *args, **kwargs):
        return super().__init__(*args, **kwargs)

Steps to reproduce:

  1. Make a new class
  2. Type def __ini
  3. Press <tab>

Thanks.


@Jonas1312 commented on Wed May 08 2019

Same issue here

@jakebailey
Copy link
Member

jakebailey commented May 14, 2019

Regarding "Second, some libraries recommend initializing super by super(MyClass, self).__init__(). Users have to rewrite this anyway", this is a Python 2 vs 3 thing, and the LS will generate code based on the Python version in use (#236):

if (context.Ast.LanguageVersion.Is3x()) {
sb.AppendFormat("return super().{0}({1})",
overload.Name,
parameterString);
} else if (!string.IsNullOrEmpty(className)) {
sb.AppendFormat("return super({0}, {1}).{2}({3})",
className,
first?.Name ?? string.Empty,
overload.Name,
parameterString);
} else {
sb.Append("pass");
}

This should be special cased for __init__/__new__/etc, though; the normal override code is being run here unconditionally.

@PowerSnail
Copy link

I'm not sure that using Python version is sufficient to decide which boilerplate should be used. Users may be using a Python3 environment, but writing Python2-compatible code. There are also cases where there is no need to call super in any form.

But that is not the main point anyway. The real hard error is the return.

@michaelkolber
Copy link

@jakebailey I'm facing this issue as well.

VS Code version: 1.37.0 (user setup)
Extension version (available under the Extensions sidebar): 2019.8.29288
OS and version: Windows 10, but currently running on WSL Ubuntu 18 via the "Remote - WSL" extension
Python version (& distribution if applicable, e.g. Anaconda): Python 3.6.8
Type of virtual environment used N/A
Relevant/affected Python packages and their versions: NA

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants