|
16 | 16 | except ImportError: |
17 | 17 | HAS_NUMPY = False |
18 | 18 |
|
19 | | -from astroid import builder, nodes |
| 19 | +from astroid import Uninferable, builder, nodes |
| 20 | +from astroid.brain.brain_numpy_utils import ( |
| 21 | + NUMPY_VERSION_TYPE_HINTS_SUPPORT, |
| 22 | + _get_numpy_version, |
| 23 | + numpy_supports_type_hints, |
| 24 | +) |
20 | 25 |
|
21 | 26 |
|
22 | 27 | @unittest.skipUnless(HAS_NUMPY, "This test requires the numpy library.") |
@@ -341,6 +346,88 @@ def test_datetime_astype_return(self): |
341 | 346 | ), |
342 | 347 | ) |
343 | 348 |
|
| 349 | + @unittest.skipUnless( |
| 350 | + HAS_NUMPY and numpy_supports_type_hints(), |
| 351 | + f"This test requires the numpy library with a version above {NUMPY_VERSION_TYPE_HINTS_SUPPORT}", |
| 352 | + ) |
| 353 | + def test_generic_types_are_subscriptables(self): |
| 354 | + """ |
| 355 | + Test that all types deriving from generic are subscriptables |
| 356 | + """ |
| 357 | + for type_ in ( |
| 358 | + "bool_", |
| 359 | + "bytes_", |
| 360 | + "character", |
| 361 | + "complex128", |
| 362 | + "complex192", |
| 363 | + "complex64", |
| 364 | + "complexfloating", |
| 365 | + "datetime64", |
| 366 | + "flexible", |
| 367 | + "float16", |
| 368 | + "float32", |
| 369 | + "float64", |
| 370 | + "float96", |
| 371 | + "floating", |
| 372 | + "generic", |
| 373 | + "inexact", |
| 374 | + "int16", |
| 375 | + "int32", |
| 376 | + "int32", |
| 377 | + "int64", |
| 378 | + "int8", |
| 379 | + "integer", |
| 380 | + "number", |
| 381 | + "signedinteger", |
| 382 | + "str_", |
| 383 | + "timedelta64", |
| 384 | + "uint16", |
| 385 | + "uint32", |
| 386 | + "uint32", |
| 387 | + "uint64", |
| 388 | + "uint8", |
| 389 | + "unsignedinteger", |
| 390 | + "void", |
| 391 | + ): |
| 392 | + with self.subTest(type_=type_): |
| 393 | + src = f""" |
| 394 | + import numpy as np |
| 395 | + np.{type_}[int] |
| 396 | + """ |
| 397 | + node = builder.extract_node(src) |
| 398 | + cls_node = node.inferred()[0] |
| 399 | + self.assertIsInstance(cls_node, nodes.ClassDef) |
| 400 | + self.assertEqual(cls_node.name, type_) |
| 401 | + |
| 402 | + |
| 403 | +@unittest.skipIf( |
| 404 | + HAS_NUMPY, "Those tests check that astroid does not crash if numpy is not available" |
| 405 | +) |
| 406 | +class NumpyBrainUtilsTest(unittest.TestCase): |
| 407 | + """ |
| 408 | + This class is dedicated to test that astroid does not crash |
| 409 | + if numpy module is not available |
| 410 | + """ |
| 411 | + |
| 412 | + def test_get_numpy_version_do_not_crash(self): |
| 413 | + """ |
| 414 | + Test that the function _get_numpy_version doesn't crash even if numpy is not installed |
| 415 | + """ |
| 416 | + self.assertEqual(_get_numpy_version(), ("0", "0", "0")) |
| 417 | + |
| 418 | + def test_numpy_object_uninferable(self): |
| 419 | + """ |
| 420 | + Test that in case numpy is not available, then a numpy object is uninferable |
| 421 | + but the inference doesn't lead to a crash |
| 422 | + """ |
| 423 | + src = """ |
| 424 | + import numpy as np |
| 425 | + np.number[int] |
| 426 | + """ |
| 427 | + node = builder.extract_node(src) |
| 428 | + cls_node = node.inferred()[0] |
| 429 | + self.assertIs(cls_node, Uninferable) |
| 430 | + |
344 | 431 |
|
345 | 432 | if __name__ == "__main__": |
346 | 433 | unittest.main() |
0 commit comments