| 1 |
# Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html |
| 2 |
# For details: https://github.com/pylint-dev/astroid/blob/main/LICENSE |
| 3 |
# Copyright (c) https://github.com/pylint-dev/astroid/blob/main/CONTRIBUTORS.txt |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
import unittest |
| 8 |
|
| 9 |
import astroid |
| 10 |
from astroid import builder |
| 11 |
from astroid.bases import Instance |
| 12 |
|
| 13 |
|
| 14 |
class ThreadingBrainTest(unittest.TestCase): |
| 15 |
def test_lock(self) -> None: |
| 16 |
lock_instance = builder.extract_node(""" |
| 17 |
import threading |
| 18 |
threading.Lock() |
| 19 |
""") |
| 20 |
inferred = next(lock_instance.infer()) |
| 21 |
self.assert_is_valid_lock(inferred) |
| 22 |
|
| 23 |
acquire_method = inferred.getattr("acquire")[0] |
| 24 |
parameters = [param.name for param in acquire_method.args.args[1:]] |
| 25 |
assert parameters == ["blocking", "timeout"] |
| 26 |
|
| 27 |
assert inferred.getattr("locked") |
| 28 |
|
| 29 |
def test_rlock(self) -> None: |
| 30 |
self._test_lock_object("RLock") |
| 31 |
|
| 32 |
def test_semaphore(self) -> None: |
| 33 |
self._test_lock_object("Semaphore") |
| 34 |
|
| 35 |
def test_boundedsemaphore(self) -> None: |
| 36 |
self._test_lock_object("BoundedSemaphore") |
| 37 |
|
| 38 |
def _test_lock_object(self, object_name: str) -> None: |
| 39 |
lock_instance = builder.extract_node(f""" |
| 40 |
import threading |
| 41 |
threading.{object_name}() |
| 42 |
""") |
| 43 |
inferred = next(lock_instance.infer()) |
| 44 |
self.assert_is_valid_lock(inferred) |
| 45 |
|
| 46 |
def assert_is_valid_lock(self, inferred: Instance) -> None: |
| 47 |
self.assertIsInstance(inferred, astroid.Instance) |
| 48 |
self.assertEqual(inferred.root().name, "threading") |
| 49 |
for method in ("acquire", "release", "__enter__", "__exit__"): |
| 50 |
self.assertIsInstance(next(inferred.igetattr(method)), astroid.BoundMethod) |