Skip to content

Commit 72c6a76

Browse files
friedfacebook-github-bot
authored andcommitted
fix all usage of pkgutil.walk_packages where finder is used wrong
Summary: find_loader().load_module() is deprecated lets just use importlib where possible. Reviewed By: itamaro Differential Revision: D70993911 fbshipit-source-id: 87c37cfc0827afbafd7412d98c0b95041cc9a542
1 parent 65de6bc commit 72c6a76

File tree

2 files changed

+6
-41
lines changed

2 files changed

+6
-41
lines changed

flowtorch/docs.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from collections.abc import Callable, Mapping, Sequence
1010
from inspect import isclass, isfunction, signature
1111
from types import ModuleType
12-
from typing import Any, Dict, Optional, Tuple
12+
from typing import Any
1313

1414

1515
def get_decorators(function: Callable) -> Sequence[str]:
@@ -259,35 +259,16 @@ def walk_packages(
259259

260260
# The followings line uncovered a bug that hasn't been fixed in mypy:
261261
# https://github.com/python/mypy/issues/1422
262-
for importer, this_modname, _ in pkgutil.walk_packages(
262+
for _, this_modname, _ in pkgutil.walk_packages(
263263
path=path, # type: ignore # mypy issue #1422
264264
prefix=f"{module.__name__}.",
265265
onerror=lambda x: None,
266266
):
267-
# Conditions required for mypy
268-
if importer is not None:
269-
if isinstance(importer, importlib.abc.MetaPathFinder):
270-
finder = importer.find_module(this_modname, None)
271-
elif isinstance(importer, importlib.abc.PathEntryFinder):
272-
finder = importer.find_module(this_modname)
273-
else:
274-
finder = None
275-
276-
# pyre-fixme[61]: `finder` is undefined, or not always defined.
277-
if finder is not None:
278-
module = finder.load_module(this_modname)
279-
280-
else:
281-
raise Exception("Finder is none")
267+
module = importlib.import_module(this_modname)
282268

283269
if module is not None:
284270
# Get all classes and functions imported/defined in module
285271
modules[this_modname] = (module, documentable_symbols(module))
286-
287-
del module
288-
# pyre-fixme[61]: `finder` is undefined, or not always defined.
289-
del finder
290-
291272
else:
292273
raise Exception("Module is none")
293274

flowtorch/utils.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pkgutil
99
from collections.abc import Callable, Sequence
1010
from functools import partial
11-
from typing import Any, Optional, Tuple
11+
from typing import Any
1212

1313
import flowtorch
1414
from flowtorch.bijectors.base import Bijector
@@ -67,34 +67,18 @@ def _walk_packages(
6767

6868
# The followings line uncovered a bug that hasn't been fixed in mypy:
6969
# https://github.com/python/mypy/issues/1422
70-
for importer, this_modname, _ in pkgutil.walk_packages(
70+
for _, this_modname, _ in pkgutil.walk_packages(
7171
path=path, # type: ignore # mypy issue #1422
7272
prefix=f"{flowtorch.__name__}.{modname}.",
7373
onerror=lambda x: None,
7474
):
75-
# Conditions required for mypy
76-
if importer is not None:
77-
if isinstance(importer, importlib.abc.MetaPathFinder):
78-
finder = importer.find_module(this_modname, None)
79-
elif isinstance(importer, importlib.abc.PathEntryFinder):
80-
finder = importer.find_module(this_modname)
81-
else:
82-
finder = None
83-
84-
# pyre-fixme[61]: `finder` is undefined, or not always defined.
85-
if finder is not None:
86-
module = finder.load_module(this_modname)
87-
88-
else:
89-
raise Exception("Finder is none")
75+
module = importlib.import_module(this_modname)
9076

9177
if module is not None:
9278
this_classes = inspect.getmembers(module, filter)
9379
classes.extend(this_classes)
9480

9581
del module
96-
# pyre-fixme[61]: `finder` is undefined, or not always defined.
97-
del finder
9882

9983
else:
10084
raise Exception("Module is none")

0 commit comments

Comments
 (0)