Skip to content

Commit 93ca875

Browse files
Fix crash in ExplicitNamespacePackageFinder (#1714)
* Add skip if no `six` * `urllib3` does appear to be required * Check `submodule_search_locations` Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 4551b57 commit 93ca875

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in astroid 2.12.3?
1212
=============================
1313
Release date: TBA
1414

15+
* Fixed crash in ``ExplicitNamespacePackageFinder`` involving ``_SixMetaPathImporter``.
16+
17+
Closes #1708
18+
1519
* Fix unhandled `FutureWarning` from pandas import in cython modules
1620

1721
Closes #1717

astroid/interpreter/_import/util.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ def is_namespace(modname: str) -> bool:
7272
if found_spec and found_spec.submodule_search_locations:
7373
last_submodule_search_locations = found_spec.submodule_search_locations
7474

75-
if found_spec is None:
76-
return False
77-
78-
return found_spec.origin is None
75+
return (
76+
found_spec is not None
77+
and found_spec.submodule_search_locations is not None
78+
and found_spec.origin is None
79+
)

requirements_test_brain.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ PyQt6
77
types-python-dateutil
88
six
99
types-six
10+
urllib3

tests/unittest_modutils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from xml import etree
1818
from xml.etree import ElementTree
1919

20+
import pytest
2021
from pytest import CaptureFixture, LogCaptureFixture
2122

2223
import astroid
@@ -25,6 +26,13 @@
2526

2627
from . import resources
2728

29+
try:
30+
import urllib3 # pylint: disable=unused-import
31+
32+
HAS_URLLIB3 = True
33+
except ImportError:
34+
HAS_URLLIB3 = False
35+
2836

2937
def _get_file_from_object(obj) -> str:
3038
return modutils._path_from_filename(obj.__file__)
@@ -439,5 +447,14 @@ def test_is_module_name_part_of_extension_package_whitelist_success(self) -> Non
439447
)
440448

441449

450+
@pytest.mark.skipif(not HAS_URLLIB3, reason="This test requires urllib3.")
451+
def test_file_info_from_modpath__SixMetaPathImporter() -> None:
452+
pytest.raises(
453+
ImportError,
454+
modutils.file_info_from_modpath,
455+
["urllib3.packages.six.moves.http_client"],
456+
)
457+
458+
442459
if __name__ == "__main__":
443460
unittest.main()

0 commit comments

Comments
 (0)