monkey_patch_sqlalchemy()   F
last analyzed

Complexity

Conditions 12

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 43.104

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 12
c 3
b 0
f 0
dl 0
loc 37
ccs 12
cts 30
cp 0.4
crap 43.104
rs 2.7855

3 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 9 4
A _unwrap_target_assoc_proxy() 0 5 2
A any_() 0 9 4

How to fix   Complexity   

Complexity

Complex classes like monkey_patch_sqlalchemy() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# ~*~ coding: utf-8 ~*~
0 ignored issues
show
Coding Style introduced by
This module should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
2
#-
3
# OSMAlchemy - OpenStreetMap to SQLAlchemy bridge
4
# Copyright (c) 2016 Michael Bayer <[email protected]>
5
# Copyright (c) 2016 Dominik George <[email protected]>
6
#
7
# Permission is hereby granted, free of charge, to any person obtaining a copy
8
# of this software and associated documentation files (the "Software"), to deal
9
# in the Software without restriction, including without limitation the rights
10
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
# copies of the Software, and to permit persons to whom the Software is
12
# furnished to do so, subject to the following conditions:
13
#
14
# The above copyright notice and this permission notice shall be included in all
15
# copies or substantial portions of the Software.
16
#
17
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
# SOFTWARE.
24
25 1
def monkey_patch_sqlalchemy():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
26 1
    from sqlalchemy.ext.associationproxy import AssociationProxy
27 1
    from sqlalchemy.util import memoized_property
28
29
    # Monkey patch support for chained association proxy queries into SQLAlchemy
30
    # https://bitbucket.org/zzzeek/sqlalchemy/issues/3769/chained-any-has-with-association-proxy
31 1
    if not hasattr(AssociationProxy, "_unwrap_target_assoc_proxy"):
32 1
        def _unwrap_target_assoc_proxy(self):
33
            attr = getattr(self.target_class, self.value_attr)
34
            if isinstance(attr, AssociationProxy):
35
                return attr, getattr(self.target_class, attr.target_collection)
36
            return None, None
37 1
        AssociationProxy._unwrap_target_assoc_proxy = memoized_property(_unwrap_target_assoc_proxy)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _unwrap_target_assoc_proxy 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...
38
39 1
        orig_any = AssociationProxy.any
40 1
        def any_(self, criterion=None, **kwargs):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
41
            target_assoc, inner = self._unwrap_target_assoc_proxy
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _unwrap_target_assoc_proxy 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...
42
            if target_assoc is not None:
43
                if target_assoc._target_is_object and target_assoc._uselist:
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _target_is_object 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...
Coding Style Best Practice introduced by
It seems like _uselist 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
                    inner = inner.any(criterion=criterion, **kwargs)
45
                else:
46
                    inner = inner.has(criterion=criterion, **kwargs)
47
                return self._comparator.any(inner)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _comparator 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...
48
            orig_any(self, criterion, **kwargs)
49 1
        AssociationProxy.any = any_
50
51 1
        orig_has = AssociationProxy.has
52 1
        def has(self, criterion=None, **kwargs):
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
53
            target_assoc, inner = self._unwrap_target_assoc_proxy
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _unwrap_target_assoc_proxy 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...
54
            if target_assoc is not None:
55
                if target_assoc._target_is_object and target_assoc._uselist:
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _target_is_object 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...
Coding Style Best Practice introduced by
It seems like _uselist 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...
56
                    inner = inner.any(criterion=criterion, **kwargs)
57
                else:
58
                    inner = inner.has(criterion=criterion, **kwargs)
59
                return self._comparator.has(inner)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like _comparator 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...
60
            orig_has(self, criterion, **kwargs)
61 1
        AssociationProxy.has = has
62
63
def monkey_patch_flask_restless():
0 ignored issues
show
Coding Style introduced by
This function should have a docstring.

The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:

class SomeClass:
    def some_method(self):
        """Do x and return foo."""

If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.

Loading history...
64
    try:
65
        import flask_restless.helpers
0 ignored issues
show
introduced by
Unable to import 'flask_restless.helpers'
Loading history...
66
    except:
0 ignored issues
show
Coding Style Best Practice introduced by
General except handlers without types should be used sparingly.

Typically, you would use general except handlers when you intend to specifically handle all types of errors, f.e. when logging. Otherwise, such general error handlers can mask errors in your application that you want to know of.

Loading history...
67
        return
68
69
    # Monkey patch support for chained association proxy into Flask-Restless
70
    # https://github.com/jfinkels/flask-restless/issues/578
71
    if hasattr(flask_restless.helpers, "get_related_association_proxy_model"):
72
        from sqlalchemy.ext.associationproxy import AssociationProxy
73
74
        def _get_related_association_proxy_model(attr):
0 ignored issues
show
Coding Style Naming introduced by
The name _get_related_association_proxy_model does not conform to the function naming conventions ([a-z_][a-z0-9_]{2,30}$).

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...
75
            if isinstance(attr.remote_attr, AssociationProxy):
76
                return _get_related_association_proxy_model(attr.remote_attr)
77
            else:
78
                prop = attr.remote_attr.property
79
                for attribute in ('mapper', 'parent'):
80
                    if hasattr(prop, attribute):
81
                        return getattr(prop, attribute).class_
82
                return None
83
84
        flask_restless.helpers.get_related_association_proxy_model = _get_related_association_proxy_model
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (105/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
85