Passed
Pull Request — main (#389)
by
unknown
02:00
created

pincer.commands.interactable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 24
dl 0
loc 45
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Interactable.__init__() 0 4 3
A Interactable.unassign() 0 11 4
A Interactable.__del__() 0 2 1
1
# Copyright Pincer 2021-Present
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
# Full MIT License can be found in `LICENSE` at the project root.
3
4
from __future__ import annotations
5
6
from collections import ChainMap
7
8
from .. import client as _client
9
from .chat_command_handler import ChatCommandHandler, _hash_interactable_structure
10
from .components.component_handler import ComponentHandler
11
from ..objects.app.command import AppCommand, InteractableStructure
12
13
14
INTERACTION_REGISTERS = ChainMap(ChatCommandHandler.register, ComponentHandler.register)
15
16
17
class Interactable:
18
    """
19
    Class that can register :class:`~pincer.commands.interactable.PartialInteractable`
20
    objects. Any class that subclasses this class can register Application Commands and
21
    Message Components.
22
    PartialInteractable objects are registered by running the register function and
23
    setting an attribute of the client to the result.
24
    """
25
26
    def __init__(self):
27
        for value in vars(type(self)).values():
28
            if isinstance(value, InteractableStructure):
29
                value.manager = self
30
31
    def __del__(self):
32
        self.unassign()
33
34
    def unassign(self):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
35
        for value in vars(type(self)).values():
36
            if isinstance(value, InteractableStructure):
37
                if isinstance(value.metadata, AppCommand):
38
                    INTERACTION_REGISTERS.pop(
39
                        _hash_interactable_structure(value),
40
                        None
41
                    )
42
43
                _client._events.pop(
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _events was declared protected and should not be accessed from this context.

Prefixing a member variable _ is usually regarded as the equivalent of declaring it with protected visibility that exists in other languages. Consequentially, such a member should only be accessed from the same class or a child class:

class MyParent:
    def __init__(self):
        self._x = 1;
        self.y = 2;

class MyChild(MyParent):
    def some_method(self):
        return self._x    # Ok, since accessed from a child class

class AnotherClass:
    def some_method(self, instance_of_my_child):
        return instance_of_my_child._x   # Would be flagged as AnotherClass is not
                                         # a child class of MyParent
Loading history...
44
                    value.call.__name__.lower(), None
45
                )
46