Skip to content

__new__ should be a static method #1230

Closed

Description

@Lynskylate

In RustPython, __new__ is bound method but it should be a static method.

class A:
    def __new__(*args, **kwargs):
        print("Args Total: {}".format(len(args)+len(kwargs)))

class B(A):
    def __new__(cls, *args, **kwargs):
        return super().__new__(cls, *args, **kwargs)
"""
>>> B()
CPython output
Args Total: 1 
RustPython output
Args Total: 2
"""

super().new(cls, *args, **kwargs) will pass two same ype class(args[0] is same as cls).

object __new__ Python docuemtion

Activity

  1. cthulahoops commented on Aug 13, 2019

    @cthulahoops
    Collaborator

    I think this is probably a duplicate of #778 . Just trying to get this stuff fresh in my mind so I can review the work here.

  2. cthulahoops commented on Aug 14, 2019

    @cthulahoops
    Collaborator

    I've been considering this a problem with super as it works in the examples without super but finally got a simple example that shows the difference without using super:

    CPython:

    >>> (1).__new__(int, 5)
    5
    

    RustPython:

    >>>>> (1).__new__(int, 5)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: Expected type <class 'type'>, not <class 'int'>
    
  3. Lynskylate commented on Aug 16, 2019

    @Lynskylate
    ContributorAuthor

    Incompact example code for RustPython object's __new__.

    type(object.__new__)
    type(object().__new__)

    RustPython:

    <class 'builtin_function_or_method'>
    <class 'method'>
    

    CPython

    <class 'builtin_function_or_method'>
    <class 'builtin_function_or_method'>
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-compatA discrepancy between RustPython and CPython

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions