Skip to content

Commit 62585f1

Browse files
Agony5757claude
andcommitted
fix: lazy-load torch-dependent modules in __init__.py
torch is an optional dependency but was being imported at module load time, causing ImportError in CI environments without torch installed. Use __getattr__ at module level for lazy on-demand import of VLOGMahjong and base_modules classes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c1b2233 commit 62585f1

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

pymahjong/__init__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,15 @@
4444
get_response_action_index,
4545
)
4646

47-
# RL models
48-
from pymahjong.models import VLOGMahjong
49-
from pymahjong.base_modules import (
50-
MinusOneModule,
51-
MahjongNet,
52-
DiscreteActionQNetwork,
53-
DiscreteActionPolicyNetwork,
54-
)
47+
# RL models (lazy-loaded, torch is an optional dependency)
48+
def __getattr__(name):
49+
if name == "VLOGMahjong":
50+
from pymahjong.models import VLOGMahjong
51+
return VLOGMahjong
52+
if name in ("MinusOneModule", "MahjongNet", "DiscreteActionQNetwork", "DiscreteActionPolicyNetwork"):
53+
from pymahjong import base_modules
54+
return getattr(base_modules, name)
55+
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
5556

5657
# Utilities
5758
from pymahjong.tenhou_paipu_check import paipu_replay, paipu_replay_summary

0 commit comments

Comments
 (0)