summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog10
-rw-r--r--astroid/brain/brain_collections.py8
-rw-r--r--tests/unittest_brain.py11
3 files changed, 26 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 02852c69..9ff42a6e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,14 +2,18 @@
astroid's ChangeLog
===================
+What's New in astroid 2.5.0?
+============================
+Release Date: TBA
+
+* Enrich the ``brain_collection`` module so that ``__class_getitem__`` method is added to `deque` for
+ ``python`` version above 3.9.
+
* The ``context.path`` is now a ``dict`` and the ``context.push`` method
returns ``True`` if the node has been visited a certain amount of times.
Close #669
-What's New in astroid 2.5.0?
-============================
-Release Date: TBA
* Adds a brain for type object so that it is possible to write `type[int]` in annotation.
Fixes PyCQA/pylint#4001
diff --git a/astroid/brain/brain_collections.py b/astroid/brain/brain_collections.py
index 6594e0c7..229969c5 100644
--- a/astroid/brain/brain_collections.py
+++ b/astroid/brain/brain_collections.py
@@ -4,6 +4,7 @@
# Copyright (c) 2017 Derek Gustafson <degustaf@gmail.com>
# Copyright (c) 2018 Ioana Tagirta <ioana.tagirta@gmail.com>
# Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
+# Copyright (c) 2021 Julien Palard <julien@palard.fr>
# 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
@@ -12,6 +13,9 @@ import sys
import astroid
+PY39 = sys.version_info >= (3, 9)
+
+
def _collections_transform():
return astroid.parse(
"""
@@ -61,6 +65,10 @@ def _deque_mock():
def __mul__(self, other): pass
def __imul__(self, other): pass
def __rmul__(self, other): pass"""
+ if PY39:
+ base_deque_class += """
+ @classmethod
+ def __class_getitem__(self, item): pass"""
return base_deque_class
diff --git a/tests/unittest_brain.py b/tests/unittest_brain.py
index e6ff6928..1c004a06 100644
--- a/tests/unittest_brain.py
+++ b/tests/unittest_brain.py
@@ -141,6 +141,17 @@ class CollectionsDequeTests(unittest.TestCase):
self.assertIn("insert", inferred.locals)
self.assertIn("index", inferred.locals)
+ @test_utils.require_version(maxver="3.8")
+ def test_deque_not_py39methods(self):
+ inferred = self._inferred_queue_instance()
+ with self.assertRaises(astroid.exceptions.AttributeInferenceError):
+ inferred.getattr("__class_getitem__")
+
+ @test_utils.require_version(minver="3.9")
+ def test_deque_py39methods(self):
+ inferred = self._inferred_queue_instance()
+ self.assertTrue(inferred.getattr("__class_getitem__"))
+
class OrderedDictTest(unittest.TestCase):
def _inferred_ordered_dict_instance(self):