[issue31506] Improve the error message logic for object_new & object_init

Nick Coghlan report at bugs.python.org
Wed Sep 20 02:30:01 EDT 2017


Nick Coghlan added the comment:

Aye, the "C.__new__" example omitting the first arg was just an error in that example.

And that's a good point about the current "object.__init__()" error message actually being incorrect, since the *methods* each take exactly one argument - it's only the "object(*args, **kwds)" form that genuinely expects zero arguments.

If we were to correct that error as well, we'd end up with the following:

    # Without any method overrides
    class C:
        pass

    C(42) -> "TypeError: C() takes no arguments"
    C.__new__(C, 42) -> "TypeError: C() takes no arguments"
    C().__init__(42) -> "TypeError: C.__init__() takes exactly one argument"
    # These next two quirks are the price we pay for the nicer errors above
    object.__new__(C, 42) -> "TypeError: C() takes no arguments"
    object.__init__(C(), 42) -> "TypeError: C.__init__() takes exactly one argument"

    # With method overrides
    class D:
        def __new__(cls, *args, **kwds):
            super().__new__(cls, *args, **kwds)
        def __init__(self, *args, **kwds):
            super().__init__(*args, **kwds)

    D(42) -> "TypeError: object.__new__() takes exactly one argument"
    D.__new__(D, 42) -> "TypeError: object.__new__() takes exactly one argument"
    D().__init__(42) -> "TypeError: object.__init__() takes exactly one argument"
    object.__new__(C, 42) -> "TypeError: object.__new__() takes exactly one argument"
    object.__init__(C(), 42) -> "TypeError: object.__init__() takes exactly one argument"

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue31506>
_______________________________________


More information about the Python-bugs-list mailing list