glideinWMS / glideinwms

The glideinWMS Project
http://tinyurl.com/glideinwms
Apache License 2.0
16 stars 45 forks source link

DeprecationWarning for `imp` module in matchPolicy.py #300

Closed namrathaurs closed 1 year ago

namrathaurs commented 1 year ago

During the CI workflow run for Python 3.8/3.9 unit tests for HEPCloud Decision Engine, it was observed that matchPolicy.py was reporting a DeprecationWarning as follows (although not urgent but needs to be fixed):

../glideinwms/creation/lib/matchPolicy.py:17
../glideinwms/creation/lib/matchPolicy.py:17
../glideinwms/creation/lib/matchPolicy.py:17
../glideinwms/creation/lib/matchPolicy.py:17
/home/runner/work/decisionengine_modules/decisionengine_modules/glideinwms/creation/lib/matchPolicy.py:17: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
    import imp

Per documentation available here, the imp module is deprecated since version 3.4 and will be removed in version 3.12. Additionally, this module is deprecated in favor of importlib.

During the PR review, the suggestion given was to check the use of imp and to examine if there is a one-to-one replacement in importlib. During the investigation, came across a couple of SO discussions which seemed to convey that this would be a 1:1 replacement (meaning just changing all references of imp to importlib module). Upon testing, however, turns out that there's a bit more that needs to be changed as pylint ended up with a couple of errors with just the 1:1 replacement:

************* Module creation.lib.matchPolicy
creation/lib/matchPolicy.py:86:32: E1101: Module 'importlib' has no 'find_module' member (no-member)
creation/lib/matchPolicy.py:88:32: E1101: Module 'importlib' has no 'load_module' member (no-member)
************* Module creation.lib.matchPolicy
creation/lib/matchPolicy.py:86:32: E1101: Module 'importlib' has no 'find_module' member (no-member)
creation/lib/matchPolicy.py:88:32: E1101: Module 'importlib' has no 'load_module' member (no-member)

From the documentation for find_module and load_module, these two methods have been deprecated since Python 3.3. Unless compatibility with Python 3.3 is desired, these would need to be replaced with importlib.util.find_spec() and importlib.util.spec_from_file_location(), importlib.util.module_from_spec() respectively.