Skip to content

Commit

Permalink
feat: 酷MA萌
Browse files Browse the repository at this point in the history
  • Loading branch information
LaoshuBaby committed Apr 5, 2024
1 parent 4852459 commit 81de168
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/yuheng/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@
from .method.transform import prefix_normalization


# 检查模块名称是否被更改
print(globals().get("__name__", ""))


def _check_alias():
import sys

# 检查所有已导入的模块,看是否有模块的名称不是 'yuheng' 但其实是这个包
for name, module in sys.modules.items():
if (
module is not None
and name != "yuheng"
and getattr(module, "__package__", "") == "yuheng"
):
raise ImportError("请不要使用别名导入 yuheng 包。直接使用 import yuheng。")


_check_alias()


class Carto:
def __init__(self):
self.version: str = "0.6"
Expand Down
30 changes: 30 additions & 0 deletions tests/cases/import_namespace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import sys
import unittest

current_dir = os.path.dirname(os.path.realpath(__file__))
src_dir = os.path.join(current_dir, "../../src")
sys.path.append(src_dir)


class TestImportNamespace(unittest.TestCase):
def setUp(self) -> None:
pass

def test_import_correct(self):
import yuheng

print(globals().get("__name__", ""))
world = yuheng.Carto()
del world

def test_import_wrong(self):
import yuheng as yh

print(globals().get("__name__", ""))
world = yh.Carto()
del world


if __name__ == "__main__":
unittest.main()

0 comments on commit 81de168

Please sign in to comment.