check for cyclic deps

Summary: New test that each subpackage of pytorch3d imports cleanly.

Reviewed By: patricklabatut

Differential Revision: D30001632

fbshipit-source-id: ca8dcac94491fc22f33602b3bbef481cba927094
This commit is contained in:
Jeremy Reizenstein 2021-08-23 06:15:45 -07:00 committed by Facebook GitHub Bot
parent fadec970c9
commit 77fa5987b8
2 changed files with 23 additions and 1 deletions

View File

@ -35,4 +35,5 @@ from .utils import (
) )
from .vert_align import vert_align from .vert_align import vert_align
__all__ = [k for k in globals().keys() if not k.startswith("_")] __all__ = [k for k in globals().keys() if not k.startswith("_")]

View File

@ -4,9 +4,12 @@
# This source code is licensed under the BSD-style license found in the # This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree. # LICENSE file in the root directory of this source tree.
import importlib
import json import json
import os import os
import sys
import unittest import unittest
import unittest.mock
from collections import Counter from collections import Counter
from common_testing import get_pytorch3d_dir from common_testing import get_pytorch3d_dir
@ -18,7 +21,6 @@ in_re_worker = os.environ.get("INSIDE_RE_WORKER") is not None
class TestBuild(unittest.TestCase): class TestBuild(unittest.TestCase):
@unittest.skipIf(in_re_worker, "In RE worker")
def test_name_clash(self): def test_name_clash(self):
# For setup.py, all translation units need distinct names, so we # For setup.py, all translation units need distinct names, so we
# cannot have foo.cu and foo.cpp, even in different directories. # cannot have foo.cu and foo.cpp, even in different directories.
@ -111,3 +113,22 @@ class TestBuild(unittest.TestCase):
listed_in_json.extend(section) listed_in_json.extend(section)
self.assertListEqual(sorted(listed_in_json), notes_on_disk) self.assertListEqual(sorted(listed_in_json), notes_on_disk)
def test_no_import_cycles(self):
# Check each module of pytorch3d imports cleanly,
# which may fail if there are import cycles.
# First check the setup of the test. If any of pytorch3d
# was already imported the test would be pointless.
for module in sys.modules:
self.assertFalse(module.startswith("pytorch3d"), module)
root_dir = get_pytorch3d_dir() / "pytorch3d"
for module_file in root_dir.glob("**/*.py"):
if module_file.stem == "__init__":
continue
relative_module = str(module_file.relative_to(root_dir))[:-3]
module = "pytorch3d." + relative_module.replace("/", ".")
with self.subTest(name=module):
with unittest.mock.patch.dict(sys.modules):
importlib.import_module(module)