Skip to content

Commit 62f9b85

Browse files
move alias module to own file
1 parent e17ff8b commit 62f9b85

File tree

4 files changed

+53
-44
lines changed

4 files changed

+53
-44
lines changed

src/apipkg/__init__.py

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import cast
1717
from typing import Iterable
1818

19+
from ._alias_module import AliasModule
1920
from ._importing import _py_abspath
2021
from ._importing import distribution_version as distribution_version # NOQA:F401
2122
from ._importing import importobj
@@ -225,38 +226,3 @@ def __dict__(self) -> dict[str, Any]: # type: ignore
225226
except AttributeError:
226227
pass
227228
return ns
228-
229-
230-
def AliasModule(modname: str, modpath: str, attrname: str | None = None) -> ModuleType:
231-
cached_obj: object | None = None
232-
233-
def getmod() -> object:
234-
nonlocal cached_obj
235-
if cached_obj is None:
236-
cached_obj = importobj(modpath, attrname)
237-
return cached_obj
238-
239-
x = modpath + ("." + attrname if attrname else "")
240-
repr_result = f"<AliasModule {modname!r} for {x!r}>"
241-
242-
class AliasModule(ModuleType):
243-
def __repr__(self) -> str:
244-
return repr_result
245-
246-
def __getattribute__(self, name: str) -> object:
247-
try:
248-
return getattr(getmod(), name)
249-
except ImportError:
250-
if modpath == "pytest" and attrname is None:
251-
# hack for pylibs py.test
252-
return None
253-
else:
254-
raise
255-
256-
def __setattr__(self, name: str, value: object) -> None:
257-
setattr(getmod(), name, value)
258-
259-
def __delattr__(self, name: str) -> None:
260-
delattr(getmod(), name)
261-
262-
return AliasModule(str(modname))

src/apipkg/_alias_module

Whitespace-only changes.

src/apipkg/_alias_module.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from __future__ import annotations
2+
3+
from types import ModuleType
4+
5+
from ._importing import importobj
6+
7+
8+
def AliasModule(modname: str, modpath: str, attrname: str | None = None) -> ModuleType:
9+
10+
cached_obj: object | None = None
11+
12+
def getmod() -> object:
13+
nonlocal cached_obj
14+
if cached_obj is None:
15+
cached_obj = importobj(modpath, attrname)
16+
return cached_obj
17+
18+
x = modpath + ("." + attrname if attrname else "")
19+
repr_result = f"<AliasModule {modname!r} for {x!r}>"
20+
21+
class AliasModule(ModuleType):
22+
def __repr__(self) -> str:
23+
return repr_result
24+
25+
def __getattribute__(self, name: str) -> object:
26+
try:
27+
return getattr(getmod(), name)
28+
except ImportError:
29+
if modpath == "pytest" and attrname is None:
30+
# hack for pylibs py.test
31+
return None
32+
else:
33+
raise
34+
35+
def __setattr__(self, name: str, value: object) -> None:
36+
setattr(getmod(), name, value)
37+
38+
def __delattr__(self, name: str) -> None:
39+
delattr(getmod(), name)
40+
41+
return AliasModule(str(modname))

test_apipkg.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
import pytest
99

10-
import apipkg
11-
10+
import apipkg._alias_module
1211

1312
#
1413
# test support for importing modules
1514
#
15+
1616
ModuleType = types.ModuleType
1717

1818

@@ -705,15 +705,15 @@ def test_extra_attributes(tmpdir, monkeypatch):
705705

706706

707707
def test_aliasmodule_aliases_an_attribute():
708-
am = apipkg.AliasModule("mymod", "pprint", "PrettyPrinter")
708+
am = apipkg._alias_module.AliasModule("mymod", "pprint", "PrettyPrinter")
709709
r = repr(am)
710710
assert "<AliasModule 'mymod' for 'pprint.PrettyPrinter'>" == r
711711
assert am.format
712712
assert not hasattr(am, "lqkje")
713713

714714

715715
def test_aliasmodule_aliases_unimportable_fails():
716-
am = apipkg.AliasModule("mymod", "qlwkejqlwe", "main")
716+
am = apipkg._alias_module.AliasModule("mymod", "qlwkejqlwe", "main")
717717
r = repr(am)
718718
assert "<AliasModule 'mymod' for 'qlwkejqlwe.main'>" == r
719719
# this would pass starting with apipkg 1.3 to work around a pytest bug
@@ -725,21 +725,21 @@ def test_aliasmodule_pytest_autoreturn_none_for_hack(monkeypatch):
725725
def error(*k):
726726
raise ImportError(k)
727727

728-
monkeypatch.setattr(apipkg, "importobj", error)
728+
monkeypatch.setattr(apipkg._alias_module, "importobj", error)
729729
# apipkg 1.3 added this hack
730-
am = apipkg.AliasModule("mymod", "pytest")
730+
am = apipkg._alias_module.AliasModule("mymod", "pytest")
731731
r = repr(am)
732732
assert "<AliasModule 'mymod' for 'pytest'>" == r
733733
assert am.test is None
734734

735735

736736
def test_aliasmodule_unicode():
737-
am = apipkg.AliasModule("mymod", "pprint")
737+
am = apipkg._alias_module.AliasModule("mymod", "pprint")
738738
assert am
739739

740740

741741
def test_aliasmodule_repr():
742-
am = apipkg.AliasModule("mymod", "sys")
742+
am = apipkg._alias_module.AliasModule("mymod", "sys")
743743
r = repr(am)
744744
assert "<AliasModule 'mymod' for 'sys'>" == r
745745
am.version
@@ -861,7 +861,9 @@ def test_importlib_find_spec_fake_module(find_spec):
861861

862862

863863
def test_importlib_find_spec_aliasmodule(find_spec):
864-
am = apipkg.AliasModule("apipkg.testmodule.example.email_spec", "email")
864+
am = apipkg._alias_module.AliasModule(
865+
"apipkg.testmodule.example.email_spec", "email"
866+
)
865867
spec = find_spec(am.__name__)
866868
assert spec is am.__spec__
867869

0 commit comments

Comments
 (0)