ensure classes have an __init__ method

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.


ID: python-best-practices/init-method-required

Language: Python

Severity: Notice

Category: Best Practices


Ensure that a class has an __init__ method. This check is bypassed when the class is a data class (annotated with @dataclass).

Non-Compliant Code Examples

class Foo:  # need to define __init__
	def foo(bar):
	def bar(baz):

Compliant Code Examples

# dataclass do not require an init class
class Requests:
    cpu: float  # expressed in cpu cores
    memory: int  # expressed in bytes

    def from_pod(pod: V1Pod):
        cpu = 0.0
        memory = 0

        for container in pod.spec.containers:
            cpu += parse_cpu_string(container.resources.requests["cpu"])
            memory += parse_memory_string(container.resources.requests["memory"])

        return Requests(cpu, memory)

    def add(self, other):
        self.cpu += other.cpu
        self.memory += other.memory

class AnotherClass:
    cpu: float
    memory: int
    def add(self, other):
        self.cpu += other.cpu
        self.memory += other.memory
class Foo(Bar):

  def foo():
class UserLoginTest(TestCase):
    def setUp(self):
        self.username = 'testuser'
        self.password = 'testpassword'
        self.user = User.objects.create_user(username=self.username, password=self.password)

    def test_correct_credentials(self):
        user = authenticate(username=self.username, password=self.password)
        self.assertEqual(user, self.user)

    def test_incorrect_credentials(self):
        user = authenticate(username=self.username, password='wrongpassword')
from django.db import migrations, models
import django.db.models.deletion

class Migration(migrations.Migration):

    dependencies = [

    operations = [
class Foo:  # no __init__ required for dataclass
	value = 51
class Foo:
	def __init__(self):
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis