Error message for dunder methods

current scenario -

class A:
  def __init(self, x):
    self.x = x

a = A(1)

TypeError: A() takes no arguments

expected scenario -

  1. it gives an error message, Did you mean __init__?

How does the interpreter know that __init is a typo for __init__? You can have a method called __init which is perfectly valid, and it is not mandatory for classes to have an __init__ method.

Due to “name mangling”, your __init method is renamed _A__init, which makes it even more difficult for the interpreter to guess what you really wanted.

Its not impossible for AttributeError and TypeError to offer suggestions, as NameError already does. But the scope for bad suggestions and mistakes will, I think, be much greater.

I think this is probably better suited for a linter like PyLint, PyChecker, or Flake8. But if you really think this should be considered, you should raise a feature request on the bug tracker.

if we do a search on github for the number of times __init__( vs __init( has been used, then, the results are,

__init__( -> 43,702,579
__init( -> 60,646

it appears that it is pretty rare to create a method called __init, so I think this would be a good error message suggestion if it is possible to do.

You should use your IDE’s autocomplete features to prevent such errors

that argument would hold true for a lot of error messages that have been added in newer versions of python, for example,

expected = {1: "jack", 2: "john", 3: "james" ,

SyntaxError: '{' was never closed

the IDE would suggest to close the bracket

>>> import math
AttributeError: module 'math' has no attribute 'py'. Did you mean: 'pi'?

here also, IDE would suggest pi

No, because your points above instantly produce errors. An unclosed bracket is syntactically invalid while __init is valid syntax.