Skip to content

__new__ should be a static method #1230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Lynskylate opened this issue Aug 11, 2019 · 3 comments
Closed

__new__ should be a static method #1230

Lynskylate opened this issue Aug 11, 2019 · 3 comments
Labels
C-compat A discrepancy between RustPython and CPython

Comments

@Lynskylate
Copy link
Contributor

Lynskylate commented Aug 11, 2019

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

@Lynskylate Lynskylate added the C-compat A discrepancy between RustPython and CPython label Aug 11, 2019
@cthulahoops
Copy link
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.

@cthulahoops
Copy link
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'>

@Lynskylate
Copy link
Contributor Author

Lynskylate commented Aug 16, 2019

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
Labels
C-compat A discrepancy between RustPython and CPython
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants