summaryrefslogtreecommitdiff
path: root/astroid/exceptions.py
blob: 9f49753b873c31961674b099bef73d7132a663d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
#
# This file is part of astroid.
#
# astroid is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 2.1 of the License, or (at your
# option) any later version.
#
# astroid is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
# for more details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with astroid. If not, see <http://www.gnu.org/licenses/>.
"""this module contains exceptions used in the astroid library
"""
from astroid import util


class AstroidError(Exception):
    """base exception class for all astroid related exceptions

    AstroidError and its subclasses are structured, intended to hold
    objects representing state when the exception is thrown.  Field
    values are passed to the constructor as keyword-only arguments.
    Each subclass has its own set of standard fields, but use your
    best judgment to decide whether a specific exception instance
    needs more or fewer fields for debugging.  Field values may be
    used to lazily generate the error message: self.message.format()
    will be called with the field names and values supplied as keyword
    arguments.
    """
    def __init__(self, message='', **kws):
        self.message = message
        for key, value in kws.items():
            setattr(self, key, value)

    def __str__(self):
        return self.message.format(**vars(self))


class AstroidBuildingException(AstroidError):
    """exception class when we are unable to build an astroid representation

    Standard attributes:
        modname: Name of the module that AST construction failed for.
        error: Exception raised during construction.
    """

    def __init__(self, message='Failed to import module {modname}.', **kws):
        super(AstroidBuildingException, self).__init__(message, **kws)


class NoDefault(AstroidError):
    """raised by function's `default_value` method when an argument has
    no default value
    
    Standard attributes:
        func: Function node.
        name: Name of argument without a default.
    """
    func = None
    name = None
    
    def __init__(self, message='{func!r} has no default for {name!r}.', **kws):
        super(NoDefault, self).__init__(message, **kws)


class DefaultStop(AstroidError):
    '''This is a special error that's only meant to be raised in
    generators wrapped with raise_if_nothing_inferred and
    yes_if_nothing_inferred.  It does nothing other than carry a set
    of attributes to be used in raising in InferenceError.

    '''
    
    # def __init__(self, message='{func!r} has no default for {name!r}.', **kws):
    #     super(NoDefault, self).__init__(message, **kws)


class ResolveError(AstroidError):
    """Base class of astroid resolution/inference error.

    ResolveError is not intended to be raised.

    Standard attributes:
        context: InferenceContext object.
    """
    context = None


class MroError(ResolveError):
    """Error raised when there is a problem with method resolution of a class.

    Standard attributes:
        mros: A sequence of sequences containing ClassDef nodes.
        cls: ClassDef node whose MRO resolution failed.
        context: InferenceContext object.
    """
    mros = ()
    cls = None

    def __str__(self):
        mro_names = ", ".join("({})".format(", ".join(b.name for b in m))
                              for m in self.mros)
        return self.message.format(mros=mro_names, cls=self.cls)

class DuplicateBasesError(MroError):
    """Error raised when there are duplicate bases in the same class bases."""

class InconsistentMroError(MroError):
    """Error raised when a class's MRO is inconsistent."""


class SuperError(ResolveError):

    """Error raised when there is a problem with a super call.

    Standard attributes:
        super_: The Super instance that raised the exception.
        context: InferenceContext object.
    """
    super_ = None

    def __str__(self):
        return self.message.format(**vars(self.super_))


class InferenceError(ResolveError):
    """raised when we are unable to infer a node

    Standard attributes:
        node: The node inference was called on.
        context: InferenceContext object.
    """
    node = None
    context= None

    def __init__(self, message='Inference failed for {node!r}.', **kws):
        super(InferenceError, self).__init__(message, **kws)


# Why does this inherit from InferenceError rather than ResolveError?
# Changing it causes some inference tests to fail.
class NameInferenceError(InferenceError):
    """Raised when a name lookup fails, corresponds to NameError.

    Standard attributes:
        name: The name for which lookup failed, as a string.
        scope: The node representing the scope in which the lookup occurred.
        context: InferenceContext object.
    """
    name = None
    scope = None

    def __init__(self, message='{name!r} not found in {scope!r}.', **kws):
        super(NameInferenceError, self).__init__(message, **kws)


class AttributeInferenceError(ResolveError):
    """Raised when an attribute lookup fails, corresponds to AttributeError.

    Standard attributes:
        target: The node for which lookup failed.
        attribute: The attribute for which lookup failed, as a string.
        context: InferenceContext object.
    """
    target = None
    attribute = None

    def __init__(self, message='{attribute!r} not found on {target!r}.', **kws):
        super(AttributeInferenceError, self).__init__(message, **kws)


class UseInferenceDefault(Exception):
    """exception to be raised in custom inference function to indicate that it
    should go back to the default behaviour
    """


# Backwards-compatibility aliases
OperationError = util.BadOperationMessage
UnaryOperationError = util.BadUnaryOperationMessage
BinaryOperationError = util.BadBinaryOperationMessage

SuperArgumentTypeError = SuperError
UnresolvableName = NameInferenceError
NotFoundError = AttributeInferenceError