Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecated importlib methods #869

Merged
merged 10 commits into from
Dec 30, 2020
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ astroid's ChangeLog
What's New in astroid 2.5.0?
============================
Release Date: TBA
* Fix deprecated importlib methods

Closes #703

* Add `python 3.9` support.

* The flat attribute of ``numpy.ndarray`` is now inferred as an ``numpy.ndarray`` itself.
Expand Down
13 changes: 12 additions & 1 deletion astroid/interpreter/_import/spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Copyright (c) 2018 Nick Drozd <nicholasdrozd@gmail.com>
# Copyright (c) 2019 Hugo van Kemenade <hugovk@users.noreply.github.com>
# Copyright (c) 2019 Ashley Whetter <ashley@awhetter.co.uk>
# Copyright (c) 2020 Gergely Kalmar <GergelyKalmar@users.noreply.github.com>

import abc
import collections
Expand Down Expand Up @@ -270,6 +271,16 @@ def _cached_set_diff(left, right):


def _precache_zipimporters(path=None):
"""
For each path that has not been already cached
in the sys.path_importer_cache, create a new zipimporter
instance and store it into the cache.
hippo91 marked this conversation as resolved.
Show resolved Hide resolved
Return a dict associating all paths, stored into the cache, to corresponding
hippo91 marked this conversation as resolved.
Show resolved Hide resolved
zipimporter instances
hippo91 marked this conversation as resolved.
Show resolved Hide resolved

:param path: paths that has to be added into the cache
:return: association between paths stored into the cache and zipimporter instances
hippo91 marked this conversation as resolved.
Show resolved Hide resolved
"""
pic = sys.path_importer_cache

# When measured, despite having the same complexity (O(n)),
Expand All @@ -285,7 +296,7 @@ def _precache_zipimporters(path=None):
pic[entry_path] = zipimport.zipimporter(entry_path)
except zipimport.ZipImportError:
continue
return pic
return {key: value for key, value in pic.items() if isinstance(value, zipimport.zipimporter)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if that's as intended, but this simply filters out everything from pic that is not a zipimport.zipimporter class (i.e. the other types of importers). This may be related to the failing test too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GergelyKalmar it is exactly as intended. The goal is to have a container with only one type of objects. The failing tests (dealing with pypy) are not due to this PR.



def _search_zip(modpath, pic):
Expand Down