Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
vision
Commits
f0f5ee04
Unverified
Commit
f0f5ee04
authored
Mar 02, 2021
by
Philip Meier
Committed by
GitHub
Mar 02, 2021
Browse files
Fix lazy importing for dataset tests (#3481)
parent
0ec5f31f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
11 deletions
+28
-11
test/datasets_utils.py
test/datasets_utils.py
+28
-11
No files found.
test/datasets_utils.py
View file @
f0f5ee04
...
...
@@ -10,6 +10,7 @@ import random
import
string
import
unittest
import
unittest.mock
from
collections
import
defaultdict
from
typing
import
Any
,
Callable
,
Dict
,
Iterator
,
List
,
Optional
,
Sequence
,
Tuple
,
Union
import
PIL
...
...
@@ -60,26 +61,42 @@ class LazyImporter:
)
def
__init__
(
self
):
cls
=
type
(
self
)
modules
=
defaultdict
(
list
)
for
module
in
self
.
MODULES
:
# We need the quirky 'module=module' argument to the lambda since otherwise the lookup for 'module' in this
# scope would happen at runtime rather than at definition. Thus, without it, every property would try to
# import the last 'module' in MODULES.
setattr
(
cls
,
module
.
split
(
"."
,
1
)[
0
],
property
(
lambda
self
,
module
=
module
:
LazyImporter
.
_import
(
module
)))
module
,
*
submodules
=
module
.
split
(
"."
,
1
)
if
submodules
:
modules
[
module
].
append
(
submodules
[
0
])
else
:
# This introduces the module so that it is known when we later iterate over the dictionary.
modules
.
__missing__
(
module
)
for
module
,
submodules
in
modules
.
items
():
# We need the quirky 'module=module' and submodules=submodules arguments to the lambda since otherwise the
# lookup for these would happen at runtime rather than at definition. Thus, without it, every property
# would try to import the last item in 'modules'
setattr
(
type
(
self
),
module
,
property
(
lambda
self
,
module
=
module
,
submodules
=
submodules
:
LazyImporter
.
_import
(
module
,
submodules
)),
)
@
staticmethod
def
_import
(
module
):
def
_import
(
package
,
subpackages
):
try
:
importlib
.
import_module
(
module
)
return
importlib
.
import_module
(
module
.
split
(
"."
,
1
)[
0
])
module
=
importlib
.
import_module
(
package
)
except
ImportError
as
error
:
raise
UsageError
(
f
"Failed to import module '
{
modul
e
}
'. "
f
"This probably means that the current test case needs '
{
modul
e
}
' installed, "
f
"Failed to import module '
{
packag
e
}
'. "
f
"This probably means that the current test case needs '
{
packag
e
}
' installed, "
f
"but it is not a dependency of torchvision. "
f
"You need to install it manually, for example 'pip install
{
modul
e
}
'."
f
"You need to install it manually, for example 'pip install
{
packag
e
}
'."
)
from
error
for
name
in
subpackages
:
importlib
.
import_module
(
f
".
{
name
}
"
,
package
=
package
)
return
module
lazy_importer
=
LazyImporter
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment