lazy_alchemy.lazy_alchemy.LazyDBProp.__set__()   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
from sqlalchemy import Table, MetaData
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
from sqlalchemy.engine import Engine
3
4
5
class CustomTable(Table):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
Bug introduced by
The method _all_selected_columns which was declared abstract in the super-class ReturnsRows
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method params which was declared abstract in the super-class Immutable
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
Bug introduced by
The method unique_params which was declared abstract in the super-class Immutable
was not overridden.

Methods which raise NotImplementedError should be overridden in concrete child classes.

Loading history...
best-practice introduced by
Too many ancestors (30/7)
Loading history...
6
    def __getattr__(self, attr):
7
        return getattr(self.c, attr)
8
9
    def __bool__(self) -> None:
10
        return self is not None
11
12
13
class LazyDBProp(object):
0 ignored issues
show
introduced by
Class 'LazyDBProp' inherits from object, can be safely removed from bases in python3
Loading history...
14
    """This descriptor returns sqlalchemy
15
    Table class which can be used to query
16
    table from the schema
17
    """
18
19
    def __init__(self) -> None:
20
        self._table = None
21
        self._name = None
22
23
    def __set_name__(self, _, name):
24
        self._name = name
25
26
    def __set__(self, instance, value):
27
        if isinstance(value, (CustomTable, Table)):
28
            self._table = value
29
30
    def __get__(self, instance, _):
31
        if self._table is None:
32
            self._table = CustomTable(
33
                self._name, instance.metadata, autoload=True)
34
        return self._table
35
36
37
def get_lazy_class(engine: Engine) -> object:
38
    """
39
    Function to create Lazy class for pulling table object
40
    using SQLalchemy metadata
41
    """
42
43
    def __init__(self, engine: Engine):
44
        self.metadata = MetaData(engine)
45
        self.engine = engine
46
47
    def __getattr__(self, attr):
48
        if attr not in self.__dict__:
49
            obj = self.__patch(attr)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like __patch 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
        return obj.__get__(self, type(self))
0 ignored issues
show
introduced by
The variable obj does not seem to be defined in case attr not in self.__dict__ on line 48 is False. Are you sure this can never be the case?
Loading history...
51
52
    def __patch(self, attribute):
53
        obj = LazyDBProp()
54
        obj.__set_name__(self, attribute)
55
        setattr(type(self), attribute, obj)
56
        return obj
57
58
    # naming classes uniquely for different schema's
59
    # to avoid cross referencing
60
    LazyClass = type(f"LazyClass_{engine.url.database}", (), {})
0 ignored issues
show
Coding Style Naming introduced by
Variable name "LazyClass" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
61
    LazyClass.__init__ = __init__
62
    LazyClass.__getattr__ = __getattr__
63
    LazyClass.__patch = __patch
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like __patch 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...
64
    return LazyClass(engine)
65