diff options
author | Ceridwen <ceridwenv@gmail.com> | 2016-05-16 18:54:01 -0400 |
---|---|---|
committer | Ceridwen <ceridwenv@gmail.com> | 2016-05-16 18:54:01 -0400 |
commit | 54556be81068cc38e37fb4fcccc126d50de5d405 (patch) | |
tree | 4a931b2c3f08061500394d9e82eb7f4883e2f724 | |
parent | 71ad069f79a1a2fddcd89606c9236249d9b5e2e1 (diff) | |
download | astroid-git-54556be81068cc38e37fb4fcccc126d50de5d405.tar.gz |
Convert all files to new license header
56 files changed, 1354 insertions, 1849 deletions
diff --git a/astroid/__init__.py b/astroid/__init__.py index be85b59c..754a078b 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Python Abstract Syntax Tree New Generation The aim of this module is to provide a common base representation of diff --git a/astroid/__pkginfo__.py b/astroid/__pkginfo__.py index 64540bf2..b36d3915 100644 --- a/astroid/__pkginfo__.py +++ b/astroid/__pkginfo__.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """astroid packaging information""" import sys diff --git a/astroid/arguments.py b/astroid/arguments.py index 8f4ea2f7..882bbc3f 100644 --- a/astroid/arguments.py +++ b/astroid/arguments.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import six diff --git a/astroid/as_string.py b/astroid/as_string.py index c081ea56..6499b03a 100644 --- a/astroid/as_string.py +++ b/astroid/as_string.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """This module renders Astroid nodes as string: * :func:`to_code` function return equivalent (hopefuly valid) python string diff --git a/astroid/astpeephole.py b/astroid/astpeephole.py index c66dd5a7..9055a922 100644 --- a/astroid/astpeephole.py +++ b/astroid/astpeephole.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Small AST optimizations.""" import _ast diff --git a/astroid/bases.py b/astroid/bases.py index d5de3420..0f3579e9 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """This module contains base classes and functions for the nodes and some inference utils. """ diff --git a/astroid/brain/brain_builtin_inference.py b/astroid/brain/brain_builtin_inference.py index 47ef7f3c..82d10d9e 100644 --- a/astroid/brain/brain_builtin_inference.py +++ b/astroid/brain/brain_builtin_inference.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Astroid hooks for various builtins.""" from functools import partial diff --git a/astroid/brain/brain_dateutil.py b/astroid/brain/brain_dateutil.py index 0b45412c..2c385ecc 100644 --- a/astroid/brain/brain_dateutil.py +++ b/astroid/brain/brain_dateutil.py @@ -1,15 +1,18 @@ -"""Astroid hooks for dateutil"""
-
-import textwrap
-
-from astroid import MANAGER, register_module_extender
-from astroid.builder import AstroidBuilder
-
-def dateutil_transform():
- return AstroidBuilder(MANAGER).string_build(textwrap.dedent('''
- import datetime
- def parse(timestr, parserinfo=None, **kwargs):
- return datetime.datetime()
- '''))
-
-register_module_extender(MANAGER, 'dateutil.parser', dateutil_transform)
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + +"""Astroid hooks for dateutil""" + +import textwrap + +from astroid import MANAGER, register_module_extender +from astroid.builder import AstroidBuilder + +def dateutil_transform(): + return AstroidBuilder(MANAGER).string_build(textwrap.dedent(''' + import datetime + def parse(timestr, parserinfo=None, **kwargs): + return datetime.datetime() + ''')) + +register_module_extender(MANAGER, 'dateutil.parser', dateutil_transform) diff --git a/astroid/brain/brain_gi.py b/astroid/brain/brain_gi.py index 3c24248f..9b65d204 100644 --- a/astroid/brain/brain_gi.py +++ b/astroid/brain/brain_gi.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Astroid hooks for the Python 2 GObject introspection bindings. Helps with understanding everything imported from 'gi.repository' diff --git a/astroid/brain/brain_mechanize.py b/astroid/brain/brain_mechanize.py index 20a253a4..2026d3cd 100644 --- a/astroid/brain/brain_mechanize.py +++ b/astroid/brain/brain_mechanize.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + from astroid import MANAGER, register_module_extender from astroid.builder import AstroidBuilder diff --git a/astroid/brain/brain_nose.py b/astroid/brain/brain_nose.py index 9a37f4e1..7c04c9b3 100644 --- a/astroid/brain/brain_nose.py +++ b/astroid/brain/brain_nose.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Hooks for nose library.""" diff --git a/astroid/brain/brain_numpy.py b/astroid/brain/brain_numpy.py index 75f4f18f..26d7abb5 100644 --- a/astroid/brain/brain_numpy.py +++ b/astroid/brain/brain_numpy.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # copyright 2003-2015 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # diff --git a/astroid/brain/brain_pkg_resources.py b/astroid/brain/brain_pkg_resources.py index 7777fb71..9180f0d4 100644 --- a/astroid/brain/brain_pkg_resources.py +++ b/astroid/brain/brain_pkg_resources.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # copyright 2003-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # diff --git a/astroid/brain/brain_pytest.py b/astroid/brain/brain_pytest.py index 4f615c14..f20605f0 100644 --- a/astroid/brain/brain_pytest.py +++ b/astroid/brain/brain_pytest.py @@ -1,31 +1,34 @@ -"""Astroid hooks for pytest."""
-from __future__ import absolute_import
-from astroid import MANAGER, register_module_extender
-from astroid.builder import AstroidBuilder
-
-
-def pytest_transform():
- return AstroidBuilder(MANAGER).string_build('''
-
-try:
- import _pytest.mark
- import _pytest.recwarn
- import _pytest.runner
- import _pytest.python
-except ImportError:
- pass
-else:
- deprecated_call = _pytest.recwarn.deprecated_call
- exit = _pytest.runner.exit
- fail = _pytest.runner.fail
- fixture = _pytest.python.fixture
- importorskip = _pytest.runner.importorskip
- mark = _pytest.mark.MarkGenerator()
- raises = _pytest.python.raises
- skip = _pytest.runner.skip
- yield_fixture = _pytest.python.yield_fixture
-
-''')
-
-register_module_extender(MANAGER, 'pytest', pytest_transform)
-register_module_extender(MANAGER, 'py.test', pytest_transform)
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + +"""Astroid hooks for pytest.""" +from __future__ import absolute_import +from astroid import MANAGER, register_module_extender +from astroid.builder import AstroidBuilder + + +def pytest_transform(): + return AstroidBuilder(MANAGER).string_build(''' + +try: + import _pytest.mark + import _pytest.recwarn + import _pytest.runner + import _pytest.python +except ImportError: + pass +else: + deprecated_call = _pytest.recwarn.deprecated_call + exit = _pytest.runner.exit + fail = _pytest.runner.fail + fixture = _pytest.python.fixture + importorskip = _pytest.runner.importorskip + mark = _pytest.mark.MarkGenerator() + raises = _pytest.python.raises + skip = _pytest.runner.skip + yield_fixture = _pytest.python.yield_fixture + +''') + +register_module_extender(MANAGER, 'pytest', pytest_transform) +register_module_extender(MANAGER, 'py.test', pytest_transform) diff --git a/astroid/brain/brain_qt.py b/astroid/brain/brain_qt.py index f568b65f..30d8de75 100644 --- a/astroid/brain/brain_qt.py +++ b/astroid/brain/brain_qt.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Astroid hooks for the PyQT library.""" from astroid import MANAGER, register_module_extender diff --git a/astroid/brain/brain_six.py b/astroid/brain/brain_six.py index 1c0ddf63..e991aeaa 100644 --- a/astroid/brain/brain_six.py +++ b/astroid/brain/brain_six.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # diff --git a/astroid/brain/brain_ssl.py b/astroid/brain/brain_ssl.py index 1cf8d1b8..5f33d014 100644 --- a/astroid/brain/brain_ssl.py +++ b/astroid/brain/brain_ssl.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Astroid hooks for the ssl library.""" from astroid import MANAGER, register_module_extender diff --git a/astroid/brain/brain_stdlib.py b/astroid/brain/brain_stdlib.py index 3cf5fd18..0f82f6d8 100644 --- a/astroid/brain/brain_stdlib.py +++ b/astroid/brain/brain_stdlib.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Astroid hooks for the Python standard library.""" import functools diff --git a/astroid/builder.py b/astroid/builder.py index e26d0aeb..e08af401 100644 --- a/astroid/builder.py +++ b/astroid/builder.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """The AstroidBuilder makes astroid from living object and / or from _ast The builder is not thread safe and can't be used to parse different sources diff --git a/astroid/context.py b/astroid/context.py index 6497e7d8..95f1d9f5 100644 --- a/astroid/context.py +++ b/astroid/context.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Various context related utilities, including inference and call contexts.""" import contextlib diff --git a/astroid/decorators.py b/astroid/decorators.py index 3e9f409f..e86ef67f 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # # The code in this file was originally part of logilab-common, licensed under # the same license. diff --git a/astroid/exceptions.py b/astroid/exceptions.py index 98a0f881..0a4d5d87 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """this module contains exceptions used in the astroid library """ from astroid import util diff --git a/astroid/helpers.py b/astroid/helpers.py index 6d48b0da..76261730 100644 --- a/astroid/helpers.py +++ b/astroid/helpers.py @@ -1,184 +1,170 @@ -# copyright 2003-2015 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/>.
-
-"""
-Various helper utilities.
-"""
-
-import six
-
-from astroid import bases
-from astroid import context as contextmod
-from astroid import exceptions
-from astroid import manager
-from astroid import nodes
-from astroid import raw_building
-from astroid import scoped_nodes
-from astroid import util
-
-
-BUILTINS = six.moves.builtins.__name__
-
-
-def _build_proxy_class(cls_name, builtins):
- proxy = raw_building.build_class(cls_name)
- proxy.parent = builtins
- return proxy
-
-
-def _function_type(function, builtins):
- if isinstance(function, scoped_nodes.Lambda):
- if function.root().name == BUILTINS:
- cls_name = 'builtin_function_or_method'
- else:
- cls_name = 'function'
- elif isinstance(function, bases.BoundMethod):
- if six.PY2:
- cls_name = 'instancemethod'
- else:
- cls_name = 'method'
- elif isinstance(function, bases.UnboundMethod):
- if six.PY2:
- cls_name = 'instancemethod'
- else:
- cls_name = 'function'
- return _build_proxy_class(cls_name, builtins)
-
-
-def _object_type(node, context=None):
- astroid_manager = manager.AstroidManager()
- builtins = astroid_manager.astroid_cache[BUILTINS]
- context = context or contextmod.InferenceContext()
-
- for inferred in node.infer(context=context):
- if isinstance(inferred, scoped_nodes.ClassDef):
- if inferred.newstyle:
- metaclass = inferred.metaclass()
- if metaclass:
- yield metaclass
- continue
- yield builtins.getattr('type')[0]
- elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)):
- yield _function_type(inferred, builtins)
- elif isinstance(inferred, scoped_nodes.Module):
- yield _build_proxy_class('module', builtins)
- else:
- yield inferred._proxied
-
-
-def object_type(node, context=None):
- """Obtain the type of the given node
-
- This is used to implement the ``type`` builtin, which means that it's
- used for inferring type calls, as well as used in a couple of other places
- in the inference.
- The node will be inferred first, so this function can support all
- sorts of objects, as long as they support inference.
- """
-
- try:
- types = set(_object_type(node, context))
- except exceptions.InferenceError:
- return util.Uninferable
- if len(types) > 1 or not types:
- return util.Uninferable
- return list(types)[0]
-
-
-def safe_infer(node, context=None):
- """Return the inferred value for the given node.
-
- Return None if inference failed or if there is some ambiguity (more than
- one node has been inferred).
- """
- try:
- inferit = node.infer(context=context)
- value = next(inferit)
- except exceptions.InferenceError:
- return
- try:
- next(inferit)
- return # None if there is ambiguity on the inferred node
- except exceptions.InferenceError:
- return # there is some kind of ambiguity
- except StopIteration:
- return value
-
-
-def has_known_bases(klass, context=None):
- """Return true if all base classes of a class could be inferred."""
- try:
- return klass._all_bases_known
- except AttributeError:
- pass
- for base in klass.bases:
- result = safe_infer(base, context=context)
- # TODO: check for A->B->A->B pattern in class structure too?
- if (not isinstance(result, scoped_nodes.ClassDef) or
- result is klass or
- not has_known_bases(result, context=context)):
- klass._all_bases_known = False
- return False
- klass._all_bases_known = True
- return True
-
-
-def _type_check(type1, type2):
- if not all(map(has_known_bases, (type1, type2))):
- return util.Uninferable
-
- if not all([type1.newstyle, type2.newstyle]):
- return False
- try:
- return type1 in type2.mro()[:-1]
- except exceptions.MroError:
- # The MRO is invalid.
- return util.Uninferable
-
-
-def is_subtype(type1, type2):
- """Check if *type1* is a subtype of *typ2*."""
- return _type_check(type2, type1)
-
-
-def is_supertype(type1, type2):
- """Check if *type2* is a supertype of *type1*."""
- return _type_check(type1, type2)
-
-
-def class_instance_as_index(node):
- """Get the value as an index for the given instance.
-
- If an instance provides an __index__ method, then it can
- be used in some scenarios where an integer is expected,
- for instance when multiplying or subscripting a list.
- """
- context = contextmod.InferenceContext()
- context.callcontext = contextmod.CallContext(args=[node])
-
- try:
- for inferred in node.igetattr('__index__', context=context):
- if not isinstance(inferred, bases.BoundMethod):
- continue
-
- for result in inferred.infer_call_result(node, context=context):
- if (isinstance(result, nodes.Const)
- and isinstance(result.value, int)):
- return result
- except exceptions.InferenceError:
- pass
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + + +""" +Various helper utilities. +""" + +import six + +from astroid import bases +from astroid import context as contextmod +from astroid import exceptions +from astroid import manager +from astroid import nodes +from astroid import raw_building +from astroid import scoped_nodes +from astroid import util + + +BUILTINS = six.moves.builtins.__name__ + + +def _build_proxy_class(cls_name, builtins): + proxy = raw_building.build_class(cls_name) + proxy.parent = builtins + return proxy + + +def _function_type(function, builtins): + if isinstance(function, scoped_nodes.Lambda): + if function.root().name == BUILTINS: + cls_name = 'builtin_function_or_method' + else: + cls_name = 'function' + elif isinstance(function, bases.BoundMethod): + if six.PY2: + cls_name = 'instancemethod' + else: + cls_name = 'method' + elif isinstance(function, bases.UnboundMethod): + if six.PY2: + cls_name = 'instancemethod' + else: + cls_name = 'function' + return _build_proxy_class(cls_name, builtins) + + +def _object_type(node, context=None): + astroid_manager = manager.AstroidManager() + builtins = astroid_manager.astroid_cache[BUILTINS] + context = context or contextmod.InferenceContext() + + for inferred in node.infer(context=context): + if isinstance(inferred, scoped_nodes.ClassDef): + if inferred.newstyle: + metaclass = inferred.metaclass() + if metaclass: + yield metaclass + continue + yield builtins.getattr('type')[0] + elif isinstance(inferred, (scoped_nodes.Lambda, bases.UnboundMethod)): + yield _function_type(inferred, builtins) + elif isinstance(inferred, scoped_nodes.Module): + yield _build_proxy_class('module', builtins) + else: + yield inferred._proxied + + +def object_type(node, context=None): + """Obtain the type of the given node + + This is used to implement the ``type`` builtin, which means that it's + used for inferring type calls, as well as used in a couple of other places + in the inference. + The node will be inferred first, so this function can support all + sorts of objects, as long as they support inference. + """ + + try: + types = set(_object_type(node, context)) + except exceptions.InferenceError: + return util.Uninferable + if len(types) > 1 or not types: + return util.Uninferable + return list(types)[0] + + +def safe_infer(node, context=None): + """Return the inferred value for the given node. + + Return None if inference failed or if there is some ambiguity (more than + one node has been inferred). + """ + try: + inferit = node.infer(context=context) + value = next(inferit) + except exceptions.InferenceError: + return + try: + next(inferit) + return # None if there is ambiguity on the inferred node + except exceptions.InferenceError: + return # there is some kind of ambiguity + except StopIteration: + return value + + +def has_known_bases(klass, context=None): + """Return true if all base classes of a class could be inferred.""" + try: + return klass._all_bases_known + except AttributeError: + pass + for base in klass.bases: + result = safe_infer(base, context=context) + # TODO: check for A->B->A->B pattern in class structure too? + if (not isinstance(result, scoped_nodes.ClassDef) or + result is klass or + not has_known_bases(result, context=context)): + klass._all_bases_known = False + return False + klass._all_bases_known = True + return True + + +def _type_check(type1, type2): + if not all(map(has_known_bases, (type1, type2))): + return util.Uninferable + + if not all([type1.newstyle, type2.newstyle]): + return False + try: + return type1 in type2.mro()[:-1] + except exceptions.MroError: + # The MRO is invalid. + return util.Uninferable + + +def is_subtype(type1, type2): + """Check if *type1* is a subtype of *typ2*.""" + return _type_check(type2, type1) + + +def is_supertype(type1, type2): + """Check if *type2* is a supertype of *type1*.""" + return _type_check(type1, type2) + + +def class_instance_as_index(node): + """Get the value as an index for the given instance. + + If an instance provides an __index__ method, then it can + be used in some scenarios where an integer is expected, + for instance when multiplying or subscripting a list. + """ + context = contextmod.InferenceContext() + context.callcontext = contextmod.CallContext(args=[node]) + + try: + for inferred in node.igetattr('__index__', context=context): + if not isinstance(inferred, bases.BoundMethod): + continue + + for result in inferred.infer_call_result(node, context=context): + if (isinstance(result, nodes.Const) + and isinstance(result.value, int)): + return result + except exceptions.InferenceError: + pass diff --git a/astroid/inference.py b/astroid/inference.py index f9912f6c..a6c16cad 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """this module contains a set of functions to handle inference on astroid trees """ diff --git a/astroid/manager.py b/astroid/manager.py index 2a80714f..6e314639 100644 --- a/astroid/manager.py +++ b/astroid/manager.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """astroid manager: avoid multiple astroid build of a same module when possible by providing a class responsible to get astroid representation from various source and using a cache of built modules) diff --git a/astroid/mixins.py b/astroid/mixins.py index 4092fe35..2241fb44 100644 --- a/astroid/mixins.py +++ b/astroid/mixins.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """This module contains some mixins for the different nodes. """ diff --git a/astroid/modutils.py b/astroid/modutils.py index 6a25f4dc..c5c2188c 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # diff --git a/astroid/node_classes.py b/astroid/node_classes.py index f0b2d16e..4aacde61 100644 --- a/astroid/node_classes.py +++ b/astroid/node_classes.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Module for some node classes. More nodes in scoped_nodes.py """ diff --git a/astroid/nodes.py b/astroid/nodes.py index 2fd6cb65..8e89a013 100644 --- a/astroid/nodes.py +++ b/astroid/nodes.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """ on all nodes : .is_statement, returning true if the node should be considered as a diff --git a/astroid/objects.py b/astroid/objects.py index 35c9b1a9..1adc29ef 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """ Inference objects are a way to represent composite AST nodes, diff --git a/astroid/protocols.py b/astroid/protocols.py index cc6a5906..9efbad30 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """this module contains a set of functions to handle python protocols for nodes where it makes sense. """ diff --git a/astroid/raw_building.py b/astroid/raw_building.py index 220f48fb..24127d49 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """this module contains a set of functions to create astroid trees from scratch (build_* functions) or from living object (object_build_* functions) """ diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 903b3cc3..7bfa2434 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """this module contains utilities for rebuilding a _ast tree in order to get a single Astroid representation """ diff --git a/astroid/scoped_nodes.py b/astroid/scoped_nodes.py index 9baa92ac..e520a664 100644 --- a/astroid/scoped_nodes.py +++ b/astroid/scoped_nodes.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """ This module contains the classes for "scoped" node, i.e. which are opening a diff --git a/astroid/test_utils.py b/astroid/test_utils.py index 82dc9501..a67576a0 100644 --- a/astroid/test_utils.py +++ b/astroid/test_utils.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Utility functions for test code that uses astroid ASTs as input.""" import contextlib import functools diff --git a/astroid/tests/__init__.py b/astroid/tests/__init__.py index e69de29b..7a8e4fce 100644 --- a/astroid/tests/__init__.py +++ b/astroid/tests/__init__.py @@ -0,0 +1,3 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + diff --git a/astroid/tests/resources.py b/astroid/tests/resources.py index 7988d053..acf5a222 100644 --- a/astroid/tests/resources.py +++ b/astroid/tests/resources.py @@ -1,20 +1,6 @@ -# Copyright 2014 Google, Inc. 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import os import sys diff --git a/astroid/tests/unittest_brain.py b/astroid/tests/unittest_brain.py index 6d971474..7b230e26 100644 --- a/astroid/tests/unittest_brain.py +++ b/astroid/tests/unittest_brain.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # Copyright 2013 Google Inc. All Rights Reserved. # # This file is part of astroid. diff --git a/astroid/tests/unittest_builder.py b/astroid/tests/unittest_builder.py index bcd3c4bf..471a1fe7 100644 --- a/astroid/tests/unittest_builder.py +++ b/astroid/tests/unittest_builder.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """tests for the astroid builder and rebuilder module""" import os diff --git a/astroid/tests/unittest_helpers.py b/astroid/tests/unittest_helpers.py index fc95d639..828b566b 100644 --- a/astroid/tests/unittest_helpers.py +++ b/astroid/tests/unittest_helpers.py @@ -1,262 +1,248 @@ -# copyright 2003-2015 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/>.
-
-import unittest
-
-import six
-from six.moves import builtins
-
-from astroid import builder
-from astroid import helpers
-from astroid import manager
-from astroid import raw_building
-from astroid import test_utils
-from astroid import util
-
-
-class TestHelpers(unittest.TestCase):
-
- def setUp(self):
- builtins_name = builtins.__name__
- astroid_manager = manager.AstroidManager()
- self.builtins = astroid_manager.astroid_cache[builtins_name]
- self.manager = manager.AstroidManager()
-
- def _extract(self, obj_name):
- return self.builtins.getattr(obj_name)[0]
-
- def _build_custom_builtin(self, obj_name):
- proxy = raw_building.build_class(obj_name)
- proxy.parent = self.builtins
- return proxy
-
- def assert_classes_equal(self, cls, other):
- self.assertEqual(cls.name, other.name)
- self.assertEqual(cls.parent, other.parent)
- self.assertEqual(cls.qname(), other.qname())
-
- def test_object_type(self):
- pairs = [
- ('1', self._extract('int')),
- ('[]', self._extract('list')),
- ('{1, 2, 3}', self._extract('set')),
- ('{1:2, 4:3}', self._extract('dict')),
- ('type', self._extract('type')),
- ('object', self._extract('type')),
- ('object()', self._extract('object')),
- ('lambda: None', self._build_custom_builtin('function')),
- ('len', self._build_custom_builtin('builtin_function_or_method')),
- ('None', self._build_custom_builtin('NoneType')),
- ('import sys\nsys#@', self._build_custom_builtin('module')),
- ]
- for code, expected in pairs:
- node = test_utils.extract_node(code)
- objtype = helpers.object_type(node)
- self.assert_classes_equal(objtype, expected)
-
- def test_object_type_classes_and_functions(self):
- ast_nodes = test_utils.extract_node('''
- def generator():
- yield
-
- class A(object):
- def test(self):
- self #@
- @classmethod
- def cls_method(cls): pass
- @staticmethod
- def static_method(): pass
- A #@
- A() #@
- A.test #@
- A().test #@
- A.cls_method #@
- A().cls_method #@
- A.static_method #@
- A().static_method #@
- generator() #@
- ''')
- from_self = helpers.object_type(ast_nodes[0])
- cls = next(ast_nodes[1].infer())
- self.assert_classes_equal(from_self, cls)
-
- cls_type = helpers.object_type(ast_nodes[1])
- self.assert_classes_equal(cls_type, self._extract('type'))
-
- instance_type = helpers.object_type(ast_nodes[2])
- cls = next(ast_nodes[2].infer())._proxied
- self.assert_classes_equal(instance_type, cls)
-
- expected_method_types = [
- (ast_nodes[3], 'instancemethod' if six.PY2 else 'function'),
- (ast_nodes[4], 'instancemethod' if six.PY2 else 'method'),
- (ast_nodes[5], 'instancemethod' if six.PY2 else 'method'),
- (ast_nodes[6], 'instancemethod' if six.PY2 else 'method'),
- (ast_nodes[7], 'function'),
- (ast_nodes[8], 'function'),
- (ast_nodes[9], 'generator'),
- ]
- for node, expected in expected_method_types:
- node_type = helpers.object_type(node)
- expected_type = self._build_custom_builtin(expected)
- self.assert_classes_equal(node_type, expected_type)
-
- @test_utils.require_version(minver='3.0')
- def test_object_type_metaclasses(self):
- module = builder.parse('''
- import abc
- class Meta(metaclass=abc.ABCMeta):
- pass
- meta_instance = Meta()
- ''')
- meta_type = helpers.object_type(module['Meta'])
- self.assert_classes_equal(meta_type, module['Meta'].metaclass())
-
- meta_instance = next(module['meta_instance'].infer())
- instance_type = helpers.object_type(meta_instance)
- self.assert_classes_equal(instance_type, module['Meta'])
-
- @test_utils.require_version(minver='3.0')
- def test_object_type_most_derived(self):
- node = test_utils.extract_node('''
- class A(type):
- def __new__(*args, **kwargs):
- return type.__new__(*args, **kwargs)
- class B(object): pass
- class C(object, metaclass=A): pass
-
- # The most derived metaclass of D is A rather than type.
- class D(B , C): #@
- pass
- ''')
- metaclass = node.metaclass()
- self.assertEqual(metaclass.name, 'A')
- obj_type = helpers.object_type(node)
- self.assertEqual(metaclass, obj_type)
-
- def test_inference_errors(self):
- node = test_utils.extract_node('''
- from unknown import Unknown
- u = Unknown #@
- ''')
- self.assertEqual(helpers.object_type(node), util.Uninferable)
-
- def test_object_type_too_many_types(self):
- node = test_utils.extract_node('''
- from unknown import Unknown
- def test(x):
- if x:
- return lambda: None
- else:
- return 1
- test(Unknown) #@
- ''')
- self.assertEqual(helpers.object_type(node), util.Uninferable)
-
- def test_is_subtype(self):
- ast_nodes = test_utils.extract_node('''
- class int_subclass(int):
- pass
- class A(object): pass #@
- class B(A): pass #@
- class C(A): pass #@
- int_subclass() #@
- ''')
- cls_a = ast_nodes[0]
- cls_b = ast_nodes[1]
- cls_c = ast_nodes[2]
- int_subclass = ast_nodes[3]
- int_subclass = helpers.object_type(next(int_subclass.infer()))
- base_int = self._extract('int')
- self.assertTrue(helpers.is_subtype(int_subclass, base_int))
- self.assertTrue(helpers.is_supertype(base_int, int_subclass))
-
- self.assertTrue(helpers.is_supertype(cls_a, cls_b))
- self.assertTrue(helpers.is_supertype(cls_a, cls_c))
- self.assertTrue(helpers.is_subtype(cls_b, cls_a))
- self.assertTrue(helpers.is_subtype(cls_c, cls_a))
- self.assertFalse(helpers.is_subtype(cls_a, cls_b))
- self.assertFalse(helpers.is_subtype(cls_a, cls_b))
-
- @test_utils.require_version(maxver='3.0')
- def test_is_subtype_supertype_old_style_classes(self):
- cls_a, cls_b = test_utils.extract_node('''
- class A: #@
- pass
- class B(A): #@
- pass
- ''')
- self.assertFalse(helpers.is_subtype(cls_a, cls_b))
- self.assertFalse(helpers.is_subtype(cls_b, cls_a))
- self.assertFalse(helpers.is_supertype(cls_a, cls_b))
- self.assertFalse(helpers.is_supertype(cls_b, cls_a))
-
- def test_is_subtype_supertype_mro_error(self):
- cls_e, cls_f = test_utils.extract_node('''
- class A(object): pass
- class B(A): pass
- class C(A): pass
- class D(B, C): pass
- class E(C, B): pass #@
- class F(D, E): pass #@
- ''')
- self.assertFalse(helpers.is_subtype(cls_e, cls_f))
- self.assertEqual(helpers.is_subtype(cls_f, cls_e), util.Uninferable)
- self.assertEqual(helpers.is_supertype(cls_e, cls_f), util.Uninferable)
- self.assertFalse(helpers.is_supertype(cls_f, cls_e))
-
- def test_is_subtype_supertype_unknown_bases(self):
- cls_a, cls_b = test_utils.extract_node('''
- from unknown import Unknown
- class A(Unknown): pass #@
- class B(A): pass #@
- ''')
- self.assertTrue(helpers.is_subtype(cls_b, cls_a))
- self.assertTrue(helpers.is_supertype(cls_a, cls_b))
-
- def test_is_subtype_supertype_unrelated_classes(self):
- cls_a, cls_b = test_utils.extract_node('''
- class A(object): pass #@
- class B(object): pass #@
- ''')
- self.assertFalse(helpers.is_subtype(cls_a, cls_b))
- self.assertFalse(helpers.is_subtype(cls_b, cls_a))
- self.assertFalse(helpers.is_supertype(cls_a, cls_b))
- self.assertFalse(helpers.is_supertype(cls_b, cls_a))
-
- def test_is_subtype_supertype_classes_no_type_ancestor(self):
- cls_a = test_utils.extract_node('''
- class A(object): #@
- pass
- ''')
- builtin_type = self._extract('type')
- self.assertFalse(helpers.is_supertype(builtin_type, cls_a))
- self.assertFalse(helpers.is_subtype(cls_a, builtin_type))
-
- def test_is_subtype_supertype_classes_metaclasses(self):
- cls_a = test_utils.extract_node('''
- class A(type): #@
- pass
- ''')
- builtin_type = self._extract('type')
- self.assertTrue(helpers.is_supertype(builtin_type, cls_a))
- self.assertTrue(helpers.is_subtype(cls_a, builtin_type))
-
-
-if __name__ == '__main__':
- unittest.main()
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + + +import unittest + +import six +from six.moves import builtins + +from astroid import builder +from astroid import helpers +from astroid import manager +from astroid import raw_building +from astroid import test_utils +from astroid import util + + +class TestHelpers(unittest.TestCase): + + def setUp(self): + builtins_name = builtins.__name__ + astroid_manager = manager.AstroidManager() + self.builtins = astroid_manager.astroid_cache[builtins_name] + self.manager = manager.AstroidManager() + + def _extract(self, obj_name): + return self.builtins.getattr(obj_name)[0] + + def _build_custom_builtin(self, obj_name): + proxy = raw_building.build_class(obj_name) + proxy.parent = self.builtins + return proxy + + def assert_classes_equal(self, cls, other): + self.assertEqual(cls.name, other.name) + self.assertEqual(cls.parent, other.parent) + self.assertEqual(cls.qname(), other.qname()) + + def test_object_type(self): + pairs = [ + ('1', self._extract('int')), + ('[]', self._extract('list')), + ('{1, 2, 3}', self._extract('set')), + ('{1:2, 4:3}', self._extract('dict')), + ('type', self._extract('type')), + ('object', self._extract('type')), + ('object()', self._extract('object')), + ('lambda: None', self._build_custom_builtin('function')), + ('len', self._build_custom_builtin('builtin_function_or_method')), + ('None', self._build_custom_builtin('NoneType')), + ('import sys\nsys#@', self._build_custom_builtin('module')), + ] + for code, expected in pairs: + node = test_utils.extract_node(code) + objtype = helpers.object_type(node) + self.assert_classes_equal(objtype, expected) + + def test_object_type_classes_and_functions(self): + ast_nodes = test_utils.extract_node(''' + def generator(): + yield + + class A(object): + def test(self): + self #@ + @classmethod + def cls_method(cls): pass + @staticmethod + def static_method(): pass + A #@ + A() #@ + A.test #@ + A().test #@ + A.cls_method #@ + A().cls_method #@ + A.static_method #@ + A().static_method #@ + generator() #@ + ''') + from_self = helpers.object_type(ast_nodes[0]) + cls = next(ast_nodes[1].infer()) + self.assert_classes_equal(from_self, cls) + + cls_type = helpers.object_type(ast_nodes[1]) + self.assert_classes_equal(cls_type, self._extract('type')) + + instance_type = helpers.object_type(ast_nodes[2]) + cls = next(ast_nodes[2].infer())._proxied + self.assert_classes_equal(instance_type, cls) + + expected_method_types = [ + (ast_nodes[3], 'instancemethod' if six.PY2 else 'function'), + (ast_nodes[4], 'instancemethod' if six.PY2 else 'method'), + (ast_nodes[5], 'instancemethod' if six.PY2 else 'method'), + (ast_nodes[6], 'instancemethod' if six.PY2 else 'method'), + (ast_nodes[7], 'function'), + (ast_nodes[8], 'function'), + (ast_nodes[9], 'generator'), + ] + for node, expected in expected_method_types: + node_type = helpers.object_type(node) + expected_type = self._build_custom_builtin(expected) + self.assert_classes_equal(node_type, expected_type) + + @test_utils.require_version(minver='3.0') + def test_object_type_metaclasses(self): + module = builder.parse(''' + import abc + class Meta(metaclass=abc.ABCMeta): + pass + meta_instance = Meta() + ''') + meta_type = helpers.object_type(module['Meta']) + self.assert_classes_equal(meta_type, module['Meta'].metaclass()) + + meta_instance = next(module['meta_instance'].infer()) + instance_type = helpers.object_type(meta_instance) + self.assert_classes_equal(instance_type, module['Meta']) + + @test_utils.require_version(minver='3.0') + def test_object_type_most_derived(self): + node = test_utils.extract_node(''' + class A(type): + def __new__(*args, **kwargs): + return type.__new__(*args, **kwargs) + class B(object): pass + class C(object, metaclass=A): pass + + # The most derived metaclass of D is A rather than type. + class D(B , C): #@ + pass + ''') + metaclass = node.metaclass() + self.assertEqual(metaclass.name, 'A') + obj_type = helpers.object_type(node) + self.assertEqual(metaclass, obj_type) + + def test_inference_errors(self): + node = test_utils.extract_node(''' + from unknown import Unknown + u = Unknown #@ + ''') + self.assertEqual(helpers.object_type(node), util.Uninferable) + + def test_object_type_too_many_types(self): + node = test_utils.extract_node(''' + from unknown import Unknown + def test(x): + if x: + return lambda: None + else: + return 1 + test(Unknown) #@ + ''') + self.assertEqual(helpers.object_type(node), util.Uninferable) + + def test_is_subtype(self): + ast_nodes = test_utils.extract_node(''' + class int_subclass(int): + pass + class A(object): pass #@ + class B(A): pass #@ + class C(A): pass #@ + int_subclass() #@ + ''') + cls_a = ast_nodes[0] + cls_b = ast_nodes[1] + cls_c = ast_nodes[2] + int_subclass = ast_nodes[3] + int_subclass = helpers.object_type(next(int_subclass.infer())) + base_int = self._extract('int') + self.assertTrue(helpers.is_subtype(int_subclass, base_int)) + self.assertTrue(helpers.is_supertype(base_int, int_subclass)) + + self.assertTrue(helpers.is_supertype(cls_a, cls_b)) + self.assertTrue(helpers.is_supertype(cls_a, cls_c)) + self.assertTrue(helpers.is_subtype(cls_b, cls_a)) + self.assertTrue(helpers.is_subtype(cls_c, cls_a)) + self.assertFalse(helpers.is_subtype(cls_a, cls_b)) + self.assertFalse(helpers.is_subtype(cls_a, cls_b)) + + @test_utils.require_version(maxver='3.0') + def test_is_subtype_supertype_old_style_classes(self): + cls_a, cls_b = test_utils.extract_node(''' + class A: #@ + pass + class B(A): #@ + pass + ''') + self.assertFalse(helpers.is_subtype(cls_a, cls_b)) + self.assertFalse(helpers.is_subtype(cls_b, cls_a)) + self.assertFalse(helpers.is_supertype(cls_a, cls_b)) + self.assertFalse(helpers.is_supertype(cls_b, cls_a)) + + def test_is_subtype_supertype_mro_error(self): + cls_e, cls_f = test_utils.extract_node(''' + class A(object): pass + class B(A): pass + class C(A): pass + class D(B, C): pass + class E(C, B): pass #@ + class F(D, E): pass #@ + ''') + self.assertFalse(helpers.is_subtype(cls_e, cls_f)) + self.assertEqual(helpers.is_subtype(cls_f, cls_e), util.Uninferable) + self.assertEqual(helpers.is_supertype(cls_e, cls_f), util.Uninferable) + self.assertFalse(helpers.is_supertype(cls_f, cls_e)) + + def test_is_subtype_supertype_unknown_bases(self): + cls_a, cls_b = test_utils.extract_node(''' + from unknown import Unknown + class A(Unknown): pass #@ + class B(A): pass #@ + ''') + self.assertTrue(helpers.is_subtype(cls_b, cls_a)) + self.assertTrue(helpers.is_supertype(cls_a, cls_b)) + + def test_is_subtype_supertype_unrelated_classes(self): + cls_a, cls_b = test_utils.extract_node(''' + class A(object): pass #@ + class B(object): pass #@ + ''') + self.assertFalse(helpers.is_subtype(cls_a, cls_b)) + self.assertFalse(helpers.is_subtype(cls_b, cls_a)) + self.assertFalse(helpers.is_supertype(cls_a, cls_b)) + self.assertFalse(helpers.is_supertype(cls_b, cls_a)) + + def test_is_subtype_supertype_classes_no_type_ancestor(self): + cls_a = test_utils.extract_node(''' + class A(object): #@ + pass + ''') + builtin_type = self._extract('type') + self.assertFalse(helpers.is_supertype(builtin_type, cls_a)) + self.assertFalse(helpers.is_subtype(cls_a, builtin_type)) + + def test_is_subtype_supertype_classes_metaclasses(self): + cls_a = test_utils.extract_node(''' + class A(type): #@ + pass + ''') + builtin_type = self._extract('type') + self.assertTrue(helpers.is_supertype(builtin_type, cls_a)) + self.assertTrue(helpers.is_subtype(cls_a, builtin_type)) + + +if __name__ == '__main__': + unittest.main() diff --git a/astroid/tests/unittest_inference.py b/astroid/tests/unittest_inference.py index d484a121..ae076b97 100644 --- a/astroid/tests/unittest_inference.py +++ b/astroid/tests/unittest_inference.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """tests for the astroid inference capabilities """ # pylint: disable=too-many-lines diff --git a/astroid/tests/unittest_lookup.py b/astroid/tests/unittest_lookup.py index ed031d6c..b40592fe 100644 --- a/astroid/tests/unittest_lookup.py +++ b/astroid/tests/unittest_lookup.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """tests for the astroid variable lookup capabilities """ import functools diff --git a/astroid/tests/unittest_manager.py b/astroid/tests/unittest_manager.py index 1373aa77..65d64192 100644 --- a/astroid/tests/unittest_manager.py +++ b/astroid/tests/unittest_manager.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import os import platform import sys diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index 9041f5fd..e1d0ec6f 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # copyright 2003-2014 LOGILAB S.A. (Paris, FRANCE), all rights reserved. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr # diff --git a/astroid/tests/unittest_nodes.py b/astroid/tests/unittest_nodes.py index 106ca53f..fb54fabf 100644 --- a/astroid/tests/unittest_nodes.py +++ b/astroid/tests/unittest_nodes.py @@ -1,20 +1,6 @@ -# 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """tests for specific behaviour of astroid nodes """ import os diff --git a/astroid/tests/unittest_objects.py b/astroid/tests/unittest_objects.py index db30771f..0681b1b4 100644 --- a/astroid/tests/unittest_objects.py +++ b/astroid/tests/unittest_objects.py @@ -1,517 +1,503 @@ -# copyright 2003-2015 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/>.
-
-import unittest
-
-from astroid import bases
-from astroid import exceptions
-from astroid import nodes
-from astroid import objects
-from astroid import test_utils
-
-class ObjectsTest(unittest.TestCase):
-
- def test_frozenset(self):
- node = test_utils.extract_node("""
- frozenset({1: 2, 2: 3}) #@
- """)
- inferred = next(node.infer())
- self.assertIsInstance(inferred, objects.FrozenSet)
-
- self.assertEqual(inferred.pytype(), "%s.frozenset" % bases.BUILTINS)
-
- itered = inferred.itered()
- self.assertEqual(len(itered), 2)
- self.assertIsInstance(itered[0], nodes.Const)
- self.assertEqual([const.value for const in itered], [1, 2])
-
- proxied = inferred._proxied
- self.assertEqual(inferred.qname(), "%s.frozenset" % bases.BUILTINS)
- self.assertIsInstance(proxied, nodes.ClassDef)
-
-
-class SuperTests(unittest.TestCase):
-
- def test_inferring_super_outside_methods(self):
- ast_nodes = test_utils.extract_node('''
- class Module(object):
- pass
- class StaticMethod(object):
- @staticmethod
- def static():
- # valid, but we don't bother with it.
- return super(StaticMethod, StaticMethod) #@
- # super outside methods aren't inferred
- super(Module, Module) #@
- # no argument super is not recognised outside methods as well.
- super() #@
- ''')
- in_static = next(ast_nodes[0].value.infer())
- self.assertIsInstance(in_static, bases.Instance)
- self.assertEqual(in_static.qname(), "%s.super" % bases.BUILTINS)
-
- module_level = next(ast_nodes[1].infer())
- self.assertIsInstance(module_level, bases.Instance)
- self.assertEqual(in_static.qname(), "%s.super" % bases.BUILTINS)
-
- no_arguments = next(ast_nodes[2].infer())
- self.assertIsInstance(no_arguments, bases.Instance)
- self.assertEqual(no_arguments.qname(), "%s.super" % bases.BUILTINS)
-
- def test_inferring_unbound_super_doesnt_work(self):
- node = test_utils.extract_node('''
- class Test(object):
- def __init__(self):
- super(Test) #@
- ''')
- unbounded = next(node.infer())
- self.assertIsInstance(unbounded, bases.Instance)
- self.assertEqual(unbounded.qname(), "%s.super" % bases.BUILTINS)
-
- def test_use_default_inference_on_not_inferring_args(self):
- ast_nodes = test_utils.extract_node('''
- class Test(object):
- def __init__(self):
- super(Lala, self) #@
- super(Test, lala) #@
- ''')
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, bases.Instance)
- self.assertEqual(first.qname(), "%s.super" % bases.BUILTINS)
-
- second = next(ast_nodes[1].infer())
- self.assertIsInstance(second, bases.Instance)
- self.assertEqual(second.qname(), "%s.super" % bases.BUILTINS)
-
- @test_utils.require_version(maxver='3.0')
- def test_super_on_old_style_class(self):
- # super doesn't work on old style class, but leave
- # that as an error for pylint. We'll infer Super objects,
- # but every call will result in a failure at some point.
- node = test_utils.extract_node('''
- class OldStyle:
- def __init__(self):
- super(OldStyle, self) #@
- ''')
- old = next(node.infer())
- self.assertIsInstance(old, objects.Super)
- self.assertIsInstance(old.mro_pointer, nodes.ClassDef)
- self.assertEqual(old.mro_pointer.name, 'OldStyle')
- with self.assertRaises(exceptions.SuperError) as cm:
- old.super_mro()
- self.assertEqual(str(cm.exception),
- "Unable to call super on old-style classes.")
-
- @test_utils.require_version(minver='3.0')
- def test_no_arguments_super(self):
- ast_nodes = test_utils.extract_node('''
- class First(object): pass
- class Second(First):
- def test(self):
- super() #@
- @classmethod
- def test_classmethod(cls):
- super() #@
- ''')
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, objects.Super)
- self.assertIsInstance(first.type, bases.Instance)
- self.assertEqual(first.type.name, 'Second')
- self.assertIsInstance(first.mro_pointer, nodes.ClassDef)
- self.assertEqual(first.mro_pointer.name, 'Second')
-
- second = next(ast_nodes[1].infer())
- self.assertIsInstance(second, objects.Super)
- self.assertIsInstance(second.type, nodes.ClassDef)
- self.assertEqual(second.type.name, 'Second')
- self.assertIsInstance(second.mro_pointer, nodes.ClassDef)
- self.assertEqual(second.mro_pointer.name, 'Second')
-
- def test_super_simple_cases(self):
- ast_nodes = test_utils.extract_node('''
- class First(object): pass
- class Second(First): pass
- class Third(First):
- def test(self):
- super(Third, self) #@
- super(Second, self) #@
-
- # mro position and the type
- super(Third, Third) #@
- super(Third, Second) #@
- super(Fourth, Fourth) #@
-
- class Fourth(Third):
- pass
- ''')
-
- # .type is the object which provides the mro.
- # .mro_pointer is the position in the mro from where
- # the lookup should be done.
-
- # super(Third, self)
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, objects.Super)
- self.assertIsInstance(first.type, bases.Instance)
- self.assertEqual(first.type.name, 'Third')
- self.assertIsInstance(first.mro_pointer, nodes.ClassDef)
- self.assertEqual(first.mro_pointer.name, 'Third')
-
- # super(Second, self)
- second = next(ast_nodes[1].infer())
- self.assertIsInstance(second, objects.Super)
- self.assertIsInstance(second.type, bases.Instance)
- self.assertEqual(second.type.name, 'Third')
- self.assertIsInstance(first.mro_pointer, nodes.ClassDef)
- self.assertEqual(second.mro_pointer.name, 'Second')
-
- # super(Third, Third)
- third = next(ast_nodes[2].infer())
- self.assertIsInstance(third, objects.Super)
- self.assertIsInstance(third.type, nodes.ClassDef)
- self.assertEqual(third.type.name, 'Third')
- self.assertIsInstance(third.mro_pointer, nodes.ClassDef)
- self.assertEqual(third.mro_pointer.name, 'Third')
-
- # super(Third, second)
- fourth = next(ast_nodes[3].infer())
- self.assertIsInstance(fourth, objects.Super)
- self.assertIsInstance(fourth.type, nodes.ClassDef)
- self.assertEqual(fourth.type.name, 'Second')
- self.assertIsInstance(fourth.mro_pointer, nodes.ClassDef)
- self.assertEqual(fourth.mro_pointer.name, 'Third')
-
- # Super(Fourth, Fourth)
- fifth = next(ast_nodes[4].infer())
- self.assertIsInstance(fifth, objects.Super)
- self.assertIsInstance(fifth.type, nodes.ClassDef)
- self.assertEqual(fifth.type.name, 'Fourth')
- self.assertIsInstance(fifth.mro_pointer, nodes.ClassDef)
- self.assertEqual(fifth.mro_pointer.name, 'Fourth')
-
- def test_super_infer(self):
- node = test_utils.extract_node('''
- class Super(object):
- def __init__(self):
- super(Super, self) #@
- ''')
- inferred = next(node.infer())
- self.assertIsInstance(inferred, objects.Super)
- reinferred = next(inferred.infer())
- self.assertIsInstance(reinferred, objects.Super)
- self.assertIs(inferred, reinferred)
-
- def test_inferring_invalid_supers(self):
- ast_nodes = test_utils.extract_node('''
- class Super(object):
- def __init__(self):
- # MRO pointer is not a type
- super(1, self) #@
- # MRO type is not a subtype
- super(Super, 1) #@
- # self is not a subtype of Bupper
- super(Bupper, self) #@
- class Bupper(Super):
- pass
- ''')
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, objects.Super)
- with self.assertRaises(exceptions.SuperError) as cm:
- first.super_mro()
- self.assertIsInstance(cm.exception.super_.mro_pointer, nodes.Const)
- self.assertEqual(cm.exception.super_.mro_pointer.value, 1)
- for node, invalid_type in zip(ast_nodes[1:],
- (nodes.Const, bases.Instance)):
- inferred = next(node.infer())
- self.assertIsInstance(inferred, objects.Super, node)
- with self.assertRaises(exceptions.SuperError) as cm:
- inferred.super_mro()
- self.assertIsInstance(cm.exception.super_.type, invalid_type)
-
- def test_proxied(self):
- node = test_utils.extract_node('''
- class Super(object):
- def __init__(self):
- super(Super, self) #@
- ''')
- inferred = next(node.infer())
- proxied = inferred._proxied
- self.assertEqual(proxied.qname(), "%s.super" % bases.BUILTINS)
- self.assertIsInstance(proxied, nodes.ClassDef)
-
- def test_super_bound_model(self):
- ast_nodes = test_utils.extract_node('''
- class First(object):
- def method(self):
- pass
- @classmethod
- def class_method(cls):
- pass
- class Super_Type_Type(First):
- def method(self):
- super(Super_Type_Type, Super_Type_Type).method #@
- super(Super_Type_Type, Super_Type_Type).class_method #@
- @classmethod
- def class_method(cls):
- super(Super_Type_Type, Super_Type_Type).method #@
- super(Super_Type_Type, Super_Type_Type).class_method #@
-
- class Super_Type_Object(First):
- def method(self):
- super(Super_Type_Object, self).method #@
- super(Super_Type_Object, self).class_method #@
- ''')
- # Super(type, type) is the same for both functions and classmethods.
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, nodes.FunctionDef)
- self.assertEqual(first.name, 'method')
-
- second = next(ast_nodes[1].infer())
- self.assertIsInstance(second, bases.BoundMethod)
- self.assertEqual(second.bound.name, 'First')
- self.assertEqual(second.type, 'classmethod')
-
- third = next(ast_nodes[2].infer())
- self.assertIsInstance(third, nodes.FunctionDef)
- self.assertEqual(third.name, 'method')
-
- fourth = next(ast_nodes[3].infer())
- self.assertIsInstance(fourth, bases.BoundMethod)
- self.assertEqual(fourth.bound.name, 'First')
- self.assertEqual(fourth.type, 'classmethod')
-
- # Super(type, obj) can lead to different attribute bindings
- # depending on the type of the place where super was called.
- fifth = next(ast_nodes[4].infer())
- self.assertIsInstance(fifth, bases.BoundMethod)
- self.assertEqual(fifth.bound.name, 'First')
- self.assertEqual(fifth.type, 'method')
-
- sixth = next(ast_nodes[5].infer())
- self.assertIsInstance(sixth, bases.BoundMethod)
- self.assertEqual(sixth.bound.name, 'First')
- self.assertEqual(sixth.type, 'classmethod')
-
- def test_super_getattr_single_inheritance(self):
- ast_nodes = test_utils.extract_node('''
- class First(object):
- def test(self): pass
- class Second(First):
- def test2(self): pass
- class Third(Second):
- test3 = 42
- def __init__(self):
- super(Third, self).test2 #@
- super(Third, self).test #@
- # test3 is local, no MRO lookup is done.
- super(Third, self).test3 #@
- super(Third, self) #@
-
- # Unbounds.
- super(Third, Third).test2 #@
- super(Third, Third).test #@
-
- ''')
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, bases.BoundMethod)
- self.assertEqual(first.bound.name, 'Second')
-
- second = next(ast_nodes[1].infer())
- self.assertIsInstance(second, bases.BoundMethod)
- self.assertEqual(second.bound.name, 'First')
-
- with self.assertRaises(exceptions.InferenceError):
- next(ast_nodes[2].infer())
- fourth = next(ast_nodes[3].infer())
- with self.assertRaises(exceptions.AttributeInferenceError):
- fourth.getattr('test3')
- with self.assertRaises(exceptions.AttributeInferenceError):
- next(fourth.igetattr('test3'))
-
- first_unbound = next(ast_nodes[4].infer())
- self.assertIsInstance(first_unbound, nodes.FunctionDef)
- self.assertEqual(first_unbound.name, 'test2')
- self.assertEqual(first_unbound.parent.name, 'Second')
-
- second_unbound = next(ast_nodes[5].infer())
- self.assertIsInstance(second_unbound, nodes.FunctionDef)
- self.assertEqual(second_unbound.name, 'test')
- self.assertEqual(second_unbound.parent.name, 'First')
-
- def test_super_invalid_mro(self):
- node = test_utils.extract_node('''
- class A(object):
- test = 42
- class Super(A, A):
- def __init__(self):
- super(Super, self) #@
- ''')
- inferred = next(node.infer())
- with self.assertRaises(exceptions.AttributeInferenceError):
- next(inferred.getattr('test'))
-
- def test_super_complex_mro(self):
- ast_nodes = test_utils.extract_node('''
- class A(object):
- def spam(self): return "A"
- def foo(self): return "A"
- @staticmethod
- def static(self): pass
- class B(A):
- def boo(self): return "B"
- def spam(self): return "B"
- class C(A):
- def boo(self): return "C"
- class E(C, B):
- def __init__(self):
- super(E, self).boo #@
- super(C, self).boo #@
- super(E, self).spam #@
- super(E, self).foo #@
- super(E, self).static #@
- ''')
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, bases.BoundMethod)
- self.assertEqual(first.bound.name, 'C')
- second = next(ast_nodes[1].infer())
- self.assertIsInstance(second, bases.BoundMethod)
- self.assertEqual(second.bound.name, 'B')
- third = next(ast_nodes[2].infer())
- self.assertIsInstance(third, bases.BoundMethod)
- self.assertEqual(third.bound.name, 'B')
- fourth = next(ast_nodes[3].infer())
- self.assertEqual(fourth.bound.name, 'A')
- static = next(ast_nodes[4].infer())
- self.assertIsInstance(static, nodes.FunctionDef)
- self.assertEqual(static.parent.scope().name, 'A')
-
- def test_super_data_model(self):
- ast_nodes = test_utils.extract_node('''
- class X(object): pass
- class A(X):
- def __init__(self):
- super(A, self) #@
- super(A, A) #@
- super(X, A) #@
- ''')
- first = next(ast_nodes[0].infer())
- thisclass = first.getattr('__thisclass__')[0]
- self.assertIsInstance(thisclass, nodes.ClassDef)
- self.assertEqual(thisclass.name, 'A')
- selfclass = first.getattr('__self_class__')[0]
- self.assertIsInstance(selfclass, nodes.ClassDef)
- self.assertEqual(selfclass.name, 'A')
- self_ = first.getattr('__self__')[0]
- self.assertIsInstance(self_, bases.Instance)
- self.assertEqual(self_.name, 'A')
- cls = first.getattr('__class__')[0]
- self.assertEqual(cls, first._proxied)
-
- second = next(ast_nodes[1].infer())
- thisclass = second.getattr('__thisclass__')[0]
- self.assertEqual(thisclass.name, 'A')
- self_ = second.getattr('__self__')[0]
- self.assertIsInstance(self_, nodes.ClassDef)
- self.assertEqual(self_.name, 'A')
-
- third = next(ast_nodes[2].infer())
- thisclass = third.getattr('__thisclass__')[0]
- self.assertEqual(thisclass.name, 'X')
- selfclass = third.getattr('__self_class__')[0]
- self.assertEqual(selfclass.name, 'A')
-
- def assertEqualMro(self, klass, expected_mro):
- self.assertEqual(
- [member.name for member in klass.super_mro()],
- expected_mro)
-
- def test_super_mro(self):
- ast_nodes = test_utils.extract_node('''
- class A(object): pass
- class B(A): pass
- class C(A): pass
- class E(C, B):
- def __init__(self):
- super(E, self) #@
- super(C, self) #@
- super(B, self) #@
-
- super(B, 1) #@
- super(1, B) #@
- ''')
- first = next(ast_nodes[0].infer())
- self.assertEqualMro(first, ['C', 'B', 'A', 'object'])
- second = next(ast_nodes[1].infer())
- self.assertEqualMro(second, ['B', 'A', 'object'])
- third = next(ast_nodes[2].infer())
- self.assertEqualMro(third, ['A', 'object'])
-
- fourth = next(ast_nodes[3].infer())
- with self.assertRaises(exceptions.SuperError):
- fourth.super_mro()
- fifth = next(ast_nodes[4].infer())
- with self.assertRaises(exceptions.SuperError):
- fifth.super_mro()
-
- def test_super_yes_objects(self):
- ast_nodes = test_utils.extract_node('''
- from collections import Missing
- class A(object):
- def __init__(self):
- super(Missing, self) #@
- super(A, Missing) #@
- ''')
- first = next(ast_nodes[0].infer())
- self.assertIsInstance(first, bases.Instance)
- second = next(ast_nodes[1].infer())
- self.assertIsInstance(second, bases.Instance)
-
- def test_super_invalid_types(self):
- node = test_utils.extract_node('''
- import collections
- class A(object):
- def __init__(self):
- super(A, collections) #@
- ''')
- inferred = next(node.infer())
- with self.assertRaises(exceptions.SuperError):
- inferred.super_mro()
- with self.assertRaises(exceptions.SuperError):
- inferred.super_mro()
-
- def test_super_properties(self):
- node = test_utils.extract_node('''
- class Foo(object):
- @property
- def dict(self):
- return 42
-
- class Bar(Foo):
- @property
- def dict(self):
- return super(Bar, self).dict
-
- Bar().dict
- ''')
- inferred = next(node.infer())
- self.assertIsInstance(inferred, nodes.Const)
- self.assertEqual(inferred.value, 42)
-
-
-if __name__ == '__main__':
- unittest.main()
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + + +import unittest + +from astroid import bases +from astroid import exceptions +from astroid import nodes +from astroid import objects +from astroid import test_utils + +class ObjectsTest(unittest.TestCase): + + def test_frozenset(self): + node = test_utils.extract_node(""" + frozenset({1: 2, 2: 3}) #@ + """) + inferred = next(node.infer()) + self.assertIsInstance(inferred, objects.FrozenSet) + + self.assertEqual(inferred.pytype(), "%s.frozenset" % bases.BUILTINS) + + itered = inferred.itered() + self.assertEqual(len(itered), 2) + self.assertIsInstance(itered[0], nodes.Const) + self.assertEqual([const.value for const in itered], [1, 2]) + + proxied = inferred._proxied + self.assertEqual(inferred.qname(), "%s.frozenset" % bases.BUILTINS) + self.assertIsInstance(proxied, nodes.ClassDef) + + +class SuperTests(unittest.TestCase): + + def test_inferring_super_outside_methods(self): + ast_nodes = test_utils.extract_node(''' + class Module(object): + pass + class StaticMethod(object): + @staticmethod + def static(): + # valid, but we don't bother with it. + return super(StaticMethod, StaticMethod) #@ + # super outside methods aren't inferred + super(Module, Module) #@ + # no argument super is not recognised outside methods as well. + super() #@ + ''') + in_static = next(ast_nodes[0].value.infer()) + self.assertIsInstance(in_static, bases.Instance) + self.assertEqual(in_static.qname(), "%s.super" % bases.BUILTINS) + + module_level = next(ast_nodes[1].infer()) + self.assertIsInstance(module_level, bases.Instance) + self.assertEqual(in_static.qname(), "%s.super" % bases.BUILTINS) + + no_arguments = next(ast_nodes[2].infer()) + self.assertIsInstance(no_arguments, bases.Instance) + self.assertEqual(no_arguments.qname(), "%s.super" % bases.BUILTINS) + + def test_inferring_unbound_super_doesnt_work(self): + node = test_utils.extract_node(''' + class Test(object): + def __init__(self): + super(Test) #@ + ''') + unbounded = next(node.infer()) + self.assertIsInstance(unbounded, bases.Instance) + self.assertEqual(unbounded.qname(), "%s.super" % bases.BUILTINS) + + def test_use_default_inference_on_not_inferring_args(self): + ast_nodes = test_utils.extract_node(''' + class Test(object): + def __init__(self): + super(Lala, self) #@ + super(Test, lala) #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, bases.Instance) + self.assertEqual(first.qname(), "%s.super" % bases.BUILTINS) + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, bases.Instance) + self.assertEqual(second.qname(), "%s.super" % bases.BUILTINS) + + @test_utils.require_version(maxver='3.0') + def test_super_on_old_style_class(self): + # super doesn't work on old style class, but leave + # that as an error for pylint. We'll infer Super objects, + # but every call will result in a failure at some point. + node = test_utils.extract_node(''' + class OldStyle: + def __init__(self): + super(OldStyle, self) #@ + ''') + old = next(node.infer()) + self.assertIsInstance(old, objects.Super) + self.assertIsInstance(old.mro_pointer, nodes.ClassDef) + self.assertEqual(old.mro_pointer.name, 'OldStyle') + with self.assertRaises(exceptions.SuperError) as cm: + old.super_mro() + self.assertEqual(str(cm.exception), + "Unable to call super on old-style classes.") + + @test_utils.require_version(minver='3.0') + def test_no_arguments_super(self): + ast_nodes = test_utils.extract_node(''' + class First(object): pass + class Second(First): + def test(self): + super() #@ + @classmethod + def test_classmethod(cls): + super() #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Super) + self.assertIsInstance(first.type, bases.Instance) + self.assertEqual(first.type.name, 'Second') + self.assertIsInstance(first.mro_pointer, nodes.ClassDef) + self.assertEqual(first.mro_pointer.name, 'Second') + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.Super) + self.assertIsInstance(second.type, nodes.ClassDef) + self.assertEqual(second.type.name, 'Second') + self.assertIsInstance(second.mro_pointer, nodes.ClassDef) + self.assertEqual(second.mro_pointer.name, 'Second') + + def test_super_simple_cases(self): + ast_nodes = test_utils.extract_node(''' + class First(object): pass + class Second(First): pass + class Third(First): + def test(self): + super(Third, self) #@ + super(Second, self) #@ + + # mro position and the type + super(Third, Third) #@ + super(Third, Second) #@ + super(Fourth, Fourth) #@ + + class Fourth(Third): + pass + ''') + + # .type is the object which provides the mro. + # .mro_pointer is the position in the mro from where + # the lookup should be done. + + # super(Third, self) + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Super) + self.assertIsInstance(first.type, bases.Instance) + self.assertEqual(first.type.name, 'Third') + self.assertIsInstance(first.mro_pointer, nodes.ClassDef) + self.assertEqual(first.mro_pointer.name, 'Third') + + # super(Second, self) + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, objects.Super) + self.assertIsInstance(second.type, bases.Instance) + self.assertEqual(second.type.name, 'Third') + self.assertIsInstance(first.mro_pointer, nodes.ClassDef) + self.assertEqual(second.mro_pointer.name, 'Second') + + # super(Third, Third) + third = next(ast_nodes[2].infer()) + self.assertIsInstance(third, objects.Super) + self.assertIsInstance(third.type, nodes.ClassDef) + self.assertEqual(third.type.name, 'Third') + self.assertIsInstance(third.mro_pointer, nodes.ClassDef) + self.assertEqual(third.mro_pointer.name, 'Third') + + # super(Third, second) + fourth = next(ast_nodes[3].infer()) + self.assertIsInstance(fourth, objects.Super) + self.assertIsInstance(fourth.type, nodes.ClassDef) + self.assertEqual(fourth.type.name, 'Second') + self.assertIsInstance(fourth.mro_pointer, nodes.ClassDef) + self.assertEqual(fourth.mro_pointer.name, 'Third') + + # Super(Fourth, Fourth) + fifth = next(ast_nodes[4].infer()) + self.assertIsInstance(fifth, objects.Super) + self.assertIsInstance(fifth.type, nodes.ClassDef) + self.assertEqual(fifth.type.name, 'Fourth') + self.assertIsInstance(fifth.mro_pointer, nodes.ClassDef) + self.assertEqual(fifth.mro_pointer.name, 'Fourth') + + def test_super_infer(self): + node = test_utils.extract_node(''' + class Super(object): + def __init__(self): + super(Super, self) #@ + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, objects.Super) + reinferred = next(inferred.infer()) + self.assertIsInstance(reinferred, objects.Super) + self.assertIs(inferred, reinferred) + + def test_inferring_invalid_supers(self): + ast_nodes = test_utils.extract_node(''' + class Super(object): + def __init__(self): + # MRO pointer is not a type + super(1, self) #@ + # MRO type is not a subtype + super(Super, 1) #@ + # self is not a subtype of Bupper + super(Bupper, self) #@ + class Bupper(Super): + pass + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, objects.Super) + with self.assertRaises(exceptions.SuperError) as cm: + first.super_mro() + self.assertIsInstance(cm.exception.super_.mro_pointer, nodes.Const) + self.assertEqual(cm.exception.super_.mro_pointer.value, 1) + for node, invalid_type in zip(ast_nodes[1:], + (nodes.Const, bases.Instance)): + inferred = next(node.infer()) + self.assertIsInstance(inferred, objects.Super, node) + with self.assertRaises(exceptions.SuperError) as cm: + inferred.super_mro() + self.assertIsInstance(cm.exception.super_.type, invalid_type) + + def test_proxied(self): + node = test_utils.extract_node(''' + class Super(object): + def __init__(self): + super(Super, self) #@ + ''') + inferred = next(node.infer()) + proxied = inferred._proxied + self.assertEqual(proxied.qname(), "%s.super" % bases.BUILTINS) + self.assertIsInstance(proxied, nodes.ClassDef) + + def test_super_bound_model(self): + ast_nodes = test_utils.extract_node(''' + class First(object): + def method(self): + pass + @classmethod + def class_method(cls): + pass + class Super_Type_Type(First): + def method(self): + super(Super_Type_Type, Super_Type_Type).method #@ + super(Super_Type_Type, Super_Type_Type).class_method #@ + @classmethod + def class_method(cls): + super(Super_Type_Type, Super_Type_Type).method #@ + super(Super_Type_Type, Super_Type_Type).class_method #@ + + class Super_Type_Object(First): + def method(self): + super(Super_Type_Object, self).method #@ + super(Super_Type_Object, self).class_method #@ + ''') + # Super(type, type) is the same for both functions and classmethods. + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, nodes.FunctionDef) + self.assertEqual(first.name, 'method') + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, bases.BoundMethod) + self.assertEqual(second.bound.name, 'First') + self.assertEqual(second.type, 'classmethod') + + third = next(ast_nodes[2].infer()) + self.assertIsInstance(third, nodes.FunctionDef) + self.assertEqual(third.name, 'method') + + fourth = next(ast_nodes[3].infer()) + self.assertIsInstance(fourth, bases.BoundMethod) + self.assertEqual(fourth.bound.name, 'First') + self.assertEqual(fourth.type, 'classmethod') + + # Super(type, obj) can lead to different attribute bindings + # depending on the type of the place where super was called. + fifth = next(ast_nodes[4].infer()) + self.assertIsInstance(fifth, bases.BoundMethod) + self.assertEqual(fifth.bound.name, 'First') + self.assertEqual(fifth.type, 'method') + + sixth = next(ast_nodes[5].infer()) + self.assertIsInstance(sixth, bases.BoundMethod) + self.assertEqual(sixth.bound.name, 'First') + self.assertEqual(sixth.type, 'classmethod') + + def test_super_getattr_single_inheritance(self): + ast_nodes = test_utils.extract_node(''' + class First(object): + def test(self): pass + class Second(First): + def test2(self): pass + class Third(Second): + test3 = 42 + def __init__(self): + super(Third, self).test2 #@ + super(Third, self).test #@ + # test3 is local, no MRO lookup is done. + super(Third, self).test3 #@ + super(Third, self) #@ + + # Unbounds. + super(Third, Third).test2 #@ + super(Third, Third).test #@ + + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, bases.BoundMethod) + self.assertEqual(first.bound.name, 'Second') + + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, bases.BoundMethod) + self.assertEqual(second.bound.name, 'First') + + with self.assertRaises(exceptions.InferenceError): + next(ast_nodes[2].infer()) + fourth = next(ast_nodes[3].infer()) + with self.assertRaises(exceptions.AttributeInferenceError): + fourth.getattr('test3') + with self.assertRaises(exceptions.AttributeInferenceError): + next(fourth.igetattr('test3')) + + first_unbound = next(ast_nodes[4].infer()) + self.assertIsInstance(first_unbound, nodes.FunctionDef) + self.assertEqual(first_unbound.name, 'test2') + self.assertEqual(first_unbound.parent.name, 'Second') + + second_unbound = next(ast_nodes[5].infer()) + self.assertIsInstance(second_unbound, nodes.FunctionDef) + self.assertEqual(second_unbound.name, 'test') + self.assertEqual(second_unbound.parent.name, 'First') + + def test_super_invalid_mro(self): + node = test_utils.extract_node(''' + class A(object): + test = 42 + class Super(A, A): + def __init__(self): + super(Super, self) #@ + ''') + inferred = next(node.infer()) + with self.assertRaises(exceptions.AttributeInferenceError): + next(inferred.getattr('test')) + + def test_super_complex_mro(self): + ast_nodes = test_utils.extract_node(''' + class A(object): + def spam(self): return "A" + def foo(self): return "A" + @staticmethod + def static(self): pass + class B(A): + def boo(self): return "B" + def spam(self): return "B" + class C(A): + def boo(self): return "C" + class E(C, B): + def __init__(self): + super(E, self).boo #@ + super(C, self).boo #@ + super(E, self).spam #@ + super(E, self).foo #@ + super(E, self).static #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, bases.BoundMethod) + self.assertEqual(first.bound.name, 'C') + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, bases.BoundMethod) + self.assertEqual(second.bound.name, 'B') + third = next(ast_nodes[2].infer()) + self.assertIsInstance(third, bases.BoundMethod) + self.assertEqual(third.bound.name, 'B') + fourth = next(ast_nodes[3].infer()) + self.assertEqual(fourth.bound.name, 'A') + static = next(ast_nodes[4].infer()) + self.assertIsInstance(static, nodes.FunctionDef) + self.assertEqual(static.parent.scope().name, 'A') + + def test_super_data_model(self): + ast_nodes = test_utils.extract_node(''' + class X(object): pass + class A(X): + def __init__(self): + super(A, self) #@ + super(A, A) #@ + super(X, A) #@ + ''') + first = next(ast_nodes[0].infer()) + thisclass = first.getattr('__thisclass__')[0] + self.assertIsInstance(thisclass, nodes.ClassDef) + self.assertEqual(thisclass.name, 'A') + selfclass = first.getattr('__self_class__')[0] + self.assertIsInstance(selfclass, nodes.ClassDef) + self.assertEqual(selfclass.name, 'A') + self_ = first.getattr('__self__')[0] + self.assertIsInstance(self_, bases.Instance) + self.assertEqual(self_.name, 'A') + cls = first.getattr('__class__')[0] + self.assertEqual(cls, first._proxied) + + second = next(ast_nodes[1].infer()) + thisclass = second.getattr('__thisclass__')[0] + self.assertEqual(thisclass.name, 'A') + self_ = second.getattr('__self__')[0] + self.assertIsInstance(self_, nodes.ClassDef) + self.assertEqual(self_.name, 'A') + + third = next(ast_nodes[2].infer()) + thisclass = third.getattr('__thisclass__')[0] + self.assertEqual(thisclass.name, 'X') + selfclass = third.getattr('__self_class__')[0] + self.assertEqual(selfclass.name, 'A') + + def assertEqualMro(self, klass, expected_mro): + self.assertEqual( + [member.name for member in klass.super_mro()], + expected_mro) + + def test_super_mro(self): + ast_nodes = test_utils.extract_node(''' + class A(object): pass + class B(A): pass + class C(A): pass + class E(C, B): + def __init__(self): + super(E, self) #@ + super(C, self) #@ + super(B, self) #@ + + super(B, 1) #@ + super(1, B) #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertEqualMro(first, ['C', 'B', 'A', 'object']) + second = next(ast_nodes[1].infer()) + self.assertEqualMro(second, ['B', 'A', 'object']) + third = next(ast_nodes[2].infer()) + self.assertEqualMro(third, ['A', 'object']) + + fourth = next(ast_nodes[3].infer()) + with self.assertRaises(exceptions.SuperError): + fourth.super_mro() + fifth = next(ast_nodes[4].infer()) + with self.assertRaises(exceptions.SuperError): + fifth.super_mro() + + def test_super_yes_objects(self): + ast_nodes = test_utils.extract_node(''' + from collections import Missing + class A(object): + def __init__(self): + super(Missing, self) #@ + super(A, Missing) #@ + ''') + first = next(ast_nodes[0].infer()) + self.assertIsInstance(first, bases.Instance) + second = next(ast_nodes[1].infer()) + self.assertIsInstance(second, bases.Instance) + + def test_super_invalid_types(self): + node = test_utils.extract_node(''' + import collections + class A(object): + def __init__(self): + super(A, collections) #@ + ''') + inferred = next(node.infer()) + with self.assertRaises(exceptions.SuperError): + inferred.super_mro() + with self.assertRaises(exceptions.SuperError): + inferred.super_mro() + + def test_super_properties(self): + node = test_utils.extract_node(''' + class Foo(object): + @property + def dict(self): + return 42 + + class Bar(Foo): + @property + def dict(self): + return super(Bar, self).dict + + Bar().dict + ''') + inferred = next(node.infer()) + self.assertIsInstance(inferred, nodes.Const) + self.assertEqual(inferred.value, 42) + + +if __name__ == '__main__': + unittest.main() diff --git a/astroid/tests/unittest_peephole.py b/astroid/tests/unittest_peephole.py index 78349898..3117faca 100644 --- a/astroid/tests/unittest_peephole.py +++ b/astroid/tests/unittest_peephole.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """Tests for the astroid AST peephole optimizer.""" diff --git a/astroid/tests/unittest_protocols.py b/astroid/tests/unittest_protocols.py index c0045c77..2ba9ef3b 100644 --- a/astroid/tests/unittest_protocols.py +++ b/astroid/tests/unittest_protocols.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import contextlib import unittest diff --git a/astroid/tests/unittest_python3.py b/astroid/tests/unittest_python3.py index 87010571..b37c5a4b 100644 --- a/astroid/tests/unittest_python3.py +++ b/astroid/tests/unittest_python3.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + from textwrap import dedent import unittest diff --git a/astroid/tests/unittest_raw_building.py b/astroid/tests/unittest_raw_building.py index 5933d4bf..5484eb6b 100644 --- a/astroid/tests/unittest_raw_building.py +++ b/astroid/tests/unittest_raw_building.py @@ -1,3 +1,6 @@ +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import inspect import os import unittest diff --git a/astroid/tests/unittest_regrtest.py b/astroid/tests/unittest_regrtest.py index 1b043b90..7f5270ee 100644 --- a/astroid/tests/unittest_regrtest.py +++ b/astroid/tests/unittest_regrtest.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import sys import unittest import textwrap diff --git a/astroid/tests/unittest_scoped_nodes.py b/astroid/tests/unittest_scoped_nodes.py index 39e39991..6882f9bc 100644 --- a/astroid/tests/unittest_scoped_nodes.py +++ b/astroid/tests/unittest_scoped_nodes.py @@ -1,20 +1,6 @@ -# copyright 2003-2014 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + """tests for specific behaviour of astroid scoped nodes (i.e. module, class and function) """ diff --git a/astroid/tests/unittest_transforms.py b/astroid/tests/unittest_transforms.py index 29a8b8db..6960c4b1 100644 --- a/astroid/tests/unittest_transforms.py +++ b/astroid/tests/unittest_transforms.py @@ -1,245 +1,231 @@ -# copyright 2003-2015 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/>.
-
-from __future__ import print_function
-
-import contextlib
-import time
-import unittest
-
-from astroid import builder
-from astroid import nodes
-from astroid import parse
-from astroid import transforms
-
-
-@contextlib.contextmanager
-def add_transform(manager, node, transform, predicate=None):
- manager.register_transform(node, transform, predicate)
- try:
- yield
- finally:
- manager.unregister_transform(node, transform, predicate)
-
-
-class TestTransforms(unittest.TestCase):
-
- def setUp(self):
- self.transformer = transforms.TransformVisitor()
-
- def parse_transform(self, code):
- module = parse(code, apply_transforms=False)
- return self.transformer.visit(module)
-
- def test_function_inlining_transform(self):
- def transform_call(node):
- # Let's do some function inlining
- inferred = next(node.infer())
- return inferred
-
- self.transformer.register_transform(nodes.Call,
- transform_call)
-
- module = self.parse_transform('''
- def test(): return 42
- test() #@
- ''')
-
- self.assertIsInstance(module.body[1], nodes.Expr)
- self.assertIsInstance(module.body[1].value, nodes.Const)
- self.assertEqual(module.body[1].value.value, 42)
-
- def test_recursive_transforms_into_astroid_fields(self):
- # Test that the transformer walks properly the tree
- # by going recursively into the _astroid_fields per each node.
- def transform_compare(node):
- # Let's check the values of the ops
- _, right = node.ops[0]
- # Assume they are Consts and they were transformed before
- # us.
- return nodes.const_factory(node.left.value < right.value)
-
- def transform_name(node):
- # Should be Consts
- return next(node.infer())
-
- self.transformer.register_transform(nodes.Compare, transform_compare)
- self.transformer.register_transform(nodes.Name, transform_name)
-
- module = self.parse_transform('''
- a = 42
- b = 24
- a < b
- ''')
-
- self.assertIsInstance(module.body[2], nodes.Expr)
- self.assertIsInstance(module.body[2].value, nodes.Const)
- self.assertFalse(module.body[2].value.value)
-
- def test_transform_patches_locals(self):
- def transform_function(node):
- assign = nodes.Assign()
- name = nodes.AssignName()
- name.name = 'value'
- assign.targets = [name]
- assign.value = nodes.const_factory(42)
- node.body.append(assign)
-
- self.transformer.register_transform(nodes.FunctionDef,
- transform_function)
-
- module = self.parse_transform('''
- def test():
- pass
- ''')
-
- func = module.body[0]
- self.assertEqual(len(func.body), 2)
- self.assertIsInstance(func.body[1], nodes.Assign)
- self.assertEqual(func.body[1].as_string(), 'value = 42')
-
- def test_predicates(self):
- def transform_call(node):
- inferred = next(node.infer())
- return inferred
-
- def should_inline(node):
- return node.func.name.startswith('inlineme')
-
- self.transformer.register_transform(nodes.Call,
- transform_call,
- should_inline)
-
- module = self.parse_transform('''
- def inlineme_1():
- return 24
- def dont_inline_me():
- return 42
- def inlineme_2():
- return 2
- inlineme_1()
- dont_inline_me()
- inlineme_2()
- ''')
- values = module.body[-3:]
- self.assertIsInstance(values[0], nodes.Expr)
- self.assertIsInstance(values[0].value, nodes.Const)
- self.assertEqual(values[0].value.value, 24)
- self.assertIsInstance(values[1], nodes.Expr)
- self.assertIsInstance(values[1].value, nodes.Call)
- self.assertIsInstance(values[2], nodes.Expr)
- self.assertIsInstance(values[2].value, nodes.Const)
- self.assertEqual(values[2].value.value, 2)
-
- def test_transforms_are_separated(self):
- # Test that the transforming is done at a separate
- # step, which means that we are not doing inference
- # on a partially constructred tree anymore, which was the
- # source of crashes in the past when certain inference rules
- # were used in a transform.
- def transform_function(node):
- if node.decorators:
- for decorator in node.decorators.nodes:
- inferred = next(decorator.infer())
- if inferred.qname() == 'abc.abstractmethod':
- return next(node.infer_call_result(node))
-
- manager = builder.MANAGER
- with add_transform(manager, nodes.FunctionDef, transform_function):
- module = builder.parse('''
- import abc
- from abc import abstractmethod
-
- class A(object):
- @abc.abstractmethod
- def ala(self):
- return 24
-
- @abstractmethod
- def bala(self):
- return 42
- ''')
-
- cls = module['A']
- ala = cls.body[0]
- bala = cls.body[1]
- self.assertIsInstance(ala, nodes.Const)
- self.assertEqual(ala.value, 24)
- self.assertIsInstance(bala, nodes.Const)
- self.assertEqual(bala.value, 42)
-
- def test_transforms_are_called_for_builtin_modules(self):
- # Test that transforms are called for builtin modules.
- def transform_function(node):
- name = nodes.AssignName()
- name.name = 'value'
- node.args.args = [name]
- return node
-
- manager = builder.MANAGER
- predicate = lambda node: node.root().name == 'time'
- with add_transform(manager, nodes.FunctionDef,
- transform_function, predicate):
- builder_instance = builder.AstroidBuilder()
- module = builder_instance.module_build(time)
-
- asctime = module['asctime']
- self.assertEqual(len(asctime.args.args), 1)
- self.assertIsInstance(asctime.args.args[0], nodes.AssignName)
- self.assertEqual(asctime.args.args[0].name, 'value')
-
- def test_builder_apply_transforms(self):
- def transform_function(node):
- return nodes.const_factory(42)
-
- manager = builder.MANAGER
- with add_transform(manager, nodes.FunctionDef, transform_function):
- astroid_builder = builder.AstroidBuilder(apply_transforms=False)
- module = astroid_builder.string_build('''def test(): pass''')
-
- # The transform wasn't applied.
- self.assertIsInstance(module.body[0], nodes.FunctionDef)
-
- def test_transform_crashes_on_is_subtype_of(self):
- # Test that we don't crash when having is_subtype_of
- # in a transform, as per issue #188. This happened
- # before, when the transforms weren't in their own step.
- def transform_class(cls):
- if cls.is_subtype_of('django.db.models.base.Model'):
- return cls
- return cls
-
- self.transformer.register_transform(nodes.ClassDef,
- transform_class)
-
- self.parse_transform('''
- # Change environ to automatically call putenv() if it exists
- import os
- putenv = os.putenv
- try:
- # This will fail if there's no putenv
- putenv
- except NameError:
- pass
- else:
- import UserDict
- ''')
-
-
-if __name__ == '__main__':
- unittest.main()
+# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + + +from __future__ import print_function + +import contextlib +import time +import unittest + +from astroid import builder +from astroid import nodes +from astroid import parse +from astroid import transforms + + +@contextlib.contextmanager +def add_transform(manager, node, transform, predicate=None): + manager.register_transform(node, transform, predicate) + try: + yield + finally: + manager.unregister_transform(node, transform, predicate) + + +class TestTransforms(unittest.TestCase): + + def setUp(self): + self.transformer = transforms.TransformVisitor() + + def parse_transform(self, code): + module = parse(code, apply_transforms=False) + return self.transformer.visit(module) + + def test_function_inlining_transform(self): + def transform_call(node): + # Let's do some function inlining + inferred = next(node.infer()) + return inferred + + self.transformer.register_transform(nodes.Call, + transform_call) + + module = self.parse_transform(''' + def test(): return 42 + test() #@ + ''') + + self.assertIsInstance(module.body[1], nodes.Expr) + self.assertIsInstance(module.body[1].value, nodes.Const) + self.assertEqual(module.body[1].value.value, 42) + + def test_recursive_transforms_into_astroid_fields(self): + # Test that the transformer walks properly the tree + # by going recursively into the _astroid_fields per each node. + def transform_compare(node): + # Let's check the values of the ops + _, right = node.ops[0] + # Assume they are Consts and they were transformed before + # us. + return nodes.const_factory(node.left.value < right.value) + + def transform_name(node): + # Should be Consts + return next(node.infer()) + + self.transformer.register_transform(nodes.Compare, transform_compare) + self.transformer.register_transform(nodes.Name, transform_name) + + module = self.parse_transform(''' + a = 42 + b = 24 + a < b + ''') + + self.assertIsInstance(module.body[2], nodes.Expr) + self.assertIsInstance(module.body[2].value, nodes.Const) + self.assertFalse(module.body[2].value.value) + + def test_transform_patches_locals(self): + def transform_function(node): + assign = nodes.Assign() + name = nodes.AssignName() + name.name = 'value' + assign.targets = [name] + assign.value = nodes.const_factory(42) + node.body.append(assign) + + self.transformer.register_transform(nodes.FunctionDef, + transform_function) + + module = self.parse_transform(''' + def test(): + pass + ''') + + func = module.body[0] + self.assertEqual(len(func.body), 2) + self.assertIsInstance(func.body[1], nodes.Assign) + self.assertEqual(func.body[1].as_string(), 'value = 42') + + def test_predicates(self): + def transform_call(node): + inferred = next(node.infer()) + return inferred + + def should_inline(node): + return node.func.name.startswith('inlineme') + + self.transformer.register_transform(nodes.Call, + transform_call, + should_inline) + + module = self.parse_transform(''' + def inlineme_1(): + return 24 + def dont_inline_me(): + return 42 + def inlineme_2(): + return 2 + inlineme_1() + dont_inline_me() + inlineme_2() + ''') + values = module.body[-3:] + self.assertIsInstance(values[0], nodes.Expr) + self.assertIsInstance(values[0].value, nodes.Const) + self.assertEqual(values[0].value.value, 24) + self.assertIsInstance(values[1], nodes.Expr) + self.assertIsInstance(values[1].value, nodes.Call) + self.assertIsInstance(values[2], nodes.Expr) + self.assertIsInstance(values[2].value, nodes.Const) + self.assertEqual(values[2].value.value, 2) + + def test_transforms_are_separated(self): + # Test that the transforming is done at a separate + # step, which means that we are not doing inference + # on a partially constructred tree anymore, which was the + # source of crashes in the past when certain inference rules + # were used in a transform. + def transform_function(node): + if node.decorators: + for decorator in node.decorators.nodes: + inferred = next(decorator.infer()) + if inferred.qname() == 'abc.abstractmethod': + return next(node.infer_call_result(node)) + + manager = builder.MANAGER + with add_transform(manager, nodes.FunctionDef, transform_function): + module = builder.parse(''' + import abc + from abc import abstractmethod + + class A(object): + @abc.abstractmethod + def ala(self): + return 24 + + @abstractmethod + def bala(self): + return 42 + ''') + + cls = module['A'] + ala = cls.body[0] + bala = cls.body[1] + self.assertIsInstance(ala, nodes.Const) + self.assertEqual(ala.value, 24) + self.assertIsInstance(bala, nodes.Const) + self.assertEqual(bala.value, 42) + + def test_transforms_are_called_for_builtin_modules(self): + # Test that transforms are called for builtin modules. + def transform_function(node): + name = nodes.AssignName() + name.name = 'value' + node.args.args = [name] + return node + + manager = builder.MANAGER + predicate = lambda node: node.root().name == 'time' + with add_transform(manager, nodes.FunctionDef, + transform_function, predicate): + builder_instance = builder.AstroidBuilder() + module = builder_instance.module_build(time) + + asctime = module['asctime'] + self.assertEqual(len(asctime.args.args), 1) + self.assertIsInstance(asctime.args.args[0], nodes.AssignName) + self.assertEqual(asctime.args.args[0].name, 'value') + + def test_builder_apply_transforms(self): + def transform_function(node): + return nodes.const_factory(42) + + manager = builder.MANAGER + with add_transform(manager, nodes.FunctionDef, transform_function): + astroid_builder = builder.AstroidBuilder(apply_transforms=False) + module = astroid_builder.string_build('''def test(): pass''') + + # The transform wasn't applied. + self.assertIsInstance(module.body[0], nodes.FunctionDef) + + def test_transform_crashes_on_is_subtype_of(self): + # Test that we don't crash when having is_subtype_of + # in a transform, as per issue #188. This happened + # before, when the transforms weren't in their own step. + def transform_class(cls): + if cls.is_subtype_of('django.db.models.base.Model'): + return cls + return cls + + self.transformer.register_transform(nodes.ClassDef, + transform_class) + + self.parse_transform(''' + # Change environ to automatically call putenv() if it exists + import os + putenv = os.putenv + try: + # This will fail if there's no putenv + putenv + except NameError: + pass + else: + import UserDict + ''') + + +if __name__ == '__main__': + unittest.main() diff --git a/astroid/tests/unittest_utils.py b/astroid/tests/unittest_utils.py index 52e1e35d..e1054c4b 100644 --- a/astroid/tests/unittest_utils.py +++ b/astroid/tests/unittest_utils.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import unittest from astroid import builder diff --git a/astroid/transforms.py b/astroid/transforms.py index 5d8fc91b..83243ad0 100644 --- a/astroid/transforms.py +++ b/astroid/transforms.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + import collections import warnings diff --git a/astroid/util.py b/astroid/util.py index 40c0fb25..5c506b59 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -1,20 +1,6 @@ -# copyright 2003-2015 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/>. +# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html +# For details: https://github.com/PyCQA/astroid/blob/master/COPYING.LESSER + # # The code in this file was originally part of logilab-common, licensed under # the same license. |