commit b2805edbffb8f8c09558338244e93bde74e3ec77
parent 04678357e0d95ba1569e65b7fc423c17f867a87f
Author: Árni Dagur <arni@dagur.eu>
Date: Tue, 22 Sep 2020 05:20:17 +0000
Avoid using star import in __init__.py
Diffstat:
2 files changed, 38 insertions(+), 1 deletion(-)
diff --git a/adblock/__init__.py b/adblock/__init__.py
@@ -1 +1,4 @@
-from .adblock import *
+from .adblock import __version__, Engine, FilterSet, BlockerResult, UrlSpecificResources
+
+
+__all__ = ("Engine", "FilterSet", "BlockerResult", "UrlSpecificResources")
diff --git a/tests/test_imports.py b/tests/test_imports.py
@@ -0,0 +1,34 @@
+import re
+import adblock
+
+
+def get_added_classes():
+ """
+ Try to get the names of all classes that we added to the Python module
+ from Rust. As always, we unfortunately don't have access to the Rust AST
+ so we have to make do with regular expressions.
+ """
+ classes = []
+ with open("src/lib.rs", "r", encoding="utf-8") as rs_f:
+ for line in rs_f:
+ match = re.match(r"m\.add_class::<(.+)>\(\)\?;", line.strip())
+ if match is not None:
+ classes.append(match.group(1))
+ return classes
+
+
+def test_added_classes():
+ """
+ Make sure that there's no class that we added in Rust but didn't import in
+ `__init__.py` and vice versa.
+ """
+ added_classes = get_added_classes()
+ assert added_classes == list(adblock.__all__)
+
+
+def test_dunder_all_classes_imported():
+ """
+ Make sure that there's no class in `__all__` that we haven't imported.
+ """
+ for c in adblock.__all__:
+ assert hasattr(adblock, c)