Commit 2e52e963 authored by Yanghan Wang's avatar Yanghan Wang Committed by Facebook GitHub Bot
Browse files

support inner class with multiple inheritance

Summary:
Pull Request resolved: https://github.com/facebookresearch/d2go/pull/400

Before the diff, the bootstrap can't handle the following gramma:

https://www.internalfb.com/code/fbsource/[010f09214704]/fbcode/mobile-vision/d2go/tests/modeling/test_modeling_distillation.py?lines=231-236

The fix is recursively applying the truncating trick.

Reviewed By: itomatik

Differential Revision: D40701375

fbshipit-source-id: 946b6be47aa4b879e2e247b879a0d8b9ac13822b
parent ec12252f
......@@ -278,11 +278,15 @@ def _bootstrap_file(
# HACK: convert multiple inheritance to single inheritance, this is needed
# because current implementation of MoreMagicMock can't handle this well.
# eg. `class MyClass(MyMixin, nn.Module)` -> `class MyClass(MyMixin)`
for stmt in tree.body:
if isinstance(stmt, ast.ClassDef):
if len(stmt.bases) > 1:
stmt.bases = stmt.bases[:1]
stmt.keywords.clear()
def _truncate_multiple_inheritance(ast_tree):
for stmt in ast_tree.body:
if isinstance(stmt, ast.ClassDef):
if len(stmt.bases) > 1:
stmt.bases = stmt.bases[:1]
stmt.keywords.clear()
_truncate_multiple_inheritance(stmt)
_truncate_multiple_inheritance(tree)
_log(2, f"Parsing AST takes {t.time} sec")
......
......@@ -47,10 +47,10 @@ class TestRegistryBootstrap(unittest.TestCase):
def test_bootstrap_core_lib(self):
self.assertFalse(bootstrap._IS_BOOTSTRAPPED)
bootstrap.bootstrap_registries(enable_cache=False, catch_exception=True)
bootstrap.bootstrap_registries(enable_cache=False, catch_exception=False)
self.assertTrue(bootstrap._IS_BOOTSTRAPPED)
def test_bootstrap_with_cache(self):
self.assertFalse(bootstrap._IS_BOOTSTRAPPED)
bootstrap.bootstrap_registries(enable_cache=True, catch_exception=True)
bootstrap.bootstrap_registries(enable_cache=True, catch_exception=False)
self.assertTrue(bootstrap._IS_BOOTSTRAPPED)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment