Skip to content

A __new__ method should be considered a staticmethod, not a classmethod. #13154

@cake-monotone

Description

@cake-monotone

This is a subtle tweak: The __new__ method is currently causing classmethod warnings (e.g., ARG003). However, it should actually be causing staticmethod warnings (e.g., ARG004).

https://play.ruff.rs/204c57b4-a35e-48d5-8f92-ca2e109dd2cd

class A:
    def __new__(cls, unused_static_method_arguments):
        return 42


A.__new__(A, 1)  # It works!!  If __new__ is treated as a class method, it should be `A.__new__(1)`

NOTE: According to the official Python documentation, the __new__ method is a staticmethod that takes the class itself (cls) as its first argument.

Reference

Called to create a new instance of class cls. __new__() is a static method (special-cased so you need not declare it as such) that takes the class of which an instance was requested as its first argument.

https://docs.python.org/3/reference/datamodel.html#object.__new__

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions