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

pincer.commands.interactable   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 30
dl 0
loc 55
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A Interactable.__init__() 0 4 3
A Interactable.__del__() 0 2 1
B Interactable.unassign() 0 20 8
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
from typing import List
8
9
from .. import client as _client
10
from .chat_command_handler import ChatCommandHandler, _hash_interactable_structure
0 ignored issues
show
Unused Code introduced by
Unused _hash_interactable_structure imported from chat_command_handler
Loading history...
11
from .components.component_handler import ComponentHandler
12
from ..objects.app.command import AppCommand, InteractableStructure
13
14
15
INTERACTION_REGISTERS = ChainMap(ChatCommandHandler.register, ComponentHandler.register)
16
17
18
class Interactable:
19
    """
20
    Class that can register :class:`~pincer.commands.interactable.PartialInteractable`
21
    objects. Any class that subclasses this class can register Application Commands and
22
    Message Components.
23
    PartialInteractable objects are registered by running the register function and
24
    setting an attribute of the client to the result.
25
    """
26
27
    def __init__(self):
28
        for value in vars(type(self)).values():
29
            if isinstance(value, InteractableStructure):
30
                value.manager = self
31
32
    def __del__(self):
33
        self.unassign()
34
35
    def unassign(self):
36
        """
37
        Removes this objects loaded commands from ChatCommandHandler and
38
        ComponentHandler and removes loaded events from the client.
39
        """
40
        for value in vars(type(self)).values():
41
            if isinstance(value, InteractableStructure):
42
                if isinstance(value.metadata, AppCommand):
43
                    for key, _value in INTERACTION_REGISTERS.items():
44
                        if value is _value:
45
                            INTERACTION_REGISTERS.pop(key)
46
47
                key = value.call.__name__.lower()
48
49
                event_or_list = _client._events.get(key)
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...
50
                if isinstance(event_or_list, List):
0 ignored issues
show
introduced by
Second argument of isinstance is not a type
Loading history...
51
                    if value in event_or_list:
52
                        event_or_list.remove(value)
53
                else:
54
                    _client._events.pop(key, None)
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...
55