nmlorg / metabot

Modularized, multi-account bot.
https://metabot.readthedocs.io/
5 stars 0 forks source link

Convert to native namespace packages #96

Closed nmlorg closed 5 months ago

nmlorg commented 5 months ago

See #53.

Holy hell this was an odyssey.

mkdir importhell
cd importhell
git clone https://github.com/nmlorg/metabot
cd metabot
git reset --hard 732c35e
cd ..
git clone https://github.com/nmlorg/metabot.calendars.google
cd metabot.calendars.google
git reset --hard 4e6947e
. activate
echo 'def test_simple(): import pprint, sys; pprint.pprint(sys.path); import metabot; print(metabot); import metabot.calendars; print(metabot.calendars); import metabot.calendars.google; print(metabot.calendars.base); print(metabot.calendars.google); assert 0' > metabot/calendars/test_google.py
pytest
['/home/n/importhell/metabot.calendars.google',
 '/home/n/importhell/metabot.calendars.google/.venv/bin',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/n/importhell/metabot.calendars.google/.venv/lib/python3.10/site-packages']
<module 'metabot' from '/home/n/importhell/metabot.calendars.google/metabot/__init__.py'>
<module 'metabot.calendars' from '/home/n/importhell/metabot.calendars.google/metabot/calendars/__init__.py'>
<module 'metabot.calendars.base' from '/home/n/importhell/metabot.calendars.google/.venv/lib/python3.10/site-packages/metabot/calendars/base.py'>
<module 'metabot.calendars.google' from '/home/n/importhell/metabot.calendars.google/metabot/calendars/google.py'>
E   assert 0
ls -d .venv/lib/python3.10/site-packages/*metabot*
.venv/lib/python3.10/site-packages/__editable___metabot_calendars_google_0_2_finder.py
.venv/lib/python3.10/site-packages/__editable__.metabot.calendars.google-0.2.pth
.venv/lib/python3.10/site-packages/metabot
.venv/lib/python3.10/site-packages/metabot-0.3.20.1.dist-info
.venv/lib/python3.10/site-packages/metabot.calendars.google-0.2.dist-info
pip install -e ../metabot
pytest
['/home/n/importhell/metabot.calendars.google',
 '/home/n/importhell/metabot.calendars.google/.venv/bin',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/n/importhell/metabot.calendars.google/.venv/lib/python3.10/site-packages']
<module 'metabot' from '/home/n/importhell/metabot.calendars.google/metabot/__init__.py'>
<module 'metabot.calendars' from '/home/n/importhell/metabot.calendars.google/metabot/calendars/__init__.py'>
E   ImportError: cannot import name 'base' from 'metabot.calendars' (/home/n/importhell/metabot.calendars.google/metabot/calendars/__init__.py)
ls -d .venv/lib/python3.10/site-packages/*metabot*
.venv/lib/python3.10/site-packages/__editable___metabot_0_3_20_1_finder.py
.venv/lib/python3.10/site-packages/__editable__.metabot-0.3.20.1.pth
.venv/lib/python3.10/site-packages/__editable___metabot_calendars_google_0_2_finder.py
.venv/lib/python3.10/site-packages/__editable__.metabot.calendars.google-0.2.pth
.venv/lib/python3.10/site-packages/metabot-0.3.20.1.dist-info
.venv/lib/python3.10/site-packages/metabot.calendars.google-0.2.dist-info
rm metabot/__init__.py metabot/calendars/__init__.py
rm ../metabot/metabot/__init__.py ../metabot/metabot/*/__init__.py
pip install -e ../metabot -e .
pytest
['/home/n/importhell/metabot.calendars.google/metabot/calendars',
 '/home/n/importhell/metabot.calendars.google/.venv/bin',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/n/importhell/metabot.calendars.google/.venv/lib/python3.10/site-packages',
 '__editable__.metabot-0.3.20.1.finder.__path_hook__',
 '__editable__.metabot.calendars.google-0.2.finder.__path_hook__']
<module 'metabot' (<_frozen_importlib_external._NamespaceLoader object at 0x7efb542c1450>)>
<module 'metabot.calendars' (<_frozen_importlib_external._NamespaceLoader object at 0x7efb542c13f0>)>
E   ModuleNotFoundError: No module named 'google.api_core'; 'google' is not a package

(This is because import google.api_core is now attempting to import api_core from /home/n/importhell/metabot.calendars.google/metabot/calendars/google.py!)

pytest --import-mode=append
['/home/n/importhell/metabot.calendars.google/.venv/bin',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/n/importhell/metabot.calendars.google/.venv/lib/python3.10/site-packages',
 '__editable__.metabot-0.3.20.1.finder.__path_hook__',
 '__editable__.metabot.calendars.google-0.2.finder.__path_hook__',
 '/home/n/importhell/metabot.calendars.google/metabot/calendars']
<module 'metabot' (<_frozen_importlib_external._NamespaceLoader object at 0x7310511694b0>)>
<module 'metabot.calendars' (<_frozen_importlib_external._NamespaceLoader object at 0x731051169570>)>
E   ModuleNotFoundError: No module named 'google.api_core'; 'google' is not a package

(/home/n/importhell/metabot.calendars.google/metabot/calendars/google.py is still taking precedence over /home/n/importhell/metabot.calendars.google/.venv/lib/python3.10/site-packages/google/.)

pytest --import-mode=importlib
ERROR collecting metabot/calendars/test_google.py
.venv/lib/python3.10/site-packages/__editable___metabot_0_3_20_1_finder.py:27: in find_spec
    return PathFinder.find_spec(fullname, path=[MAPPING[parent]])
<frozen importlib._bootstrap_external>:1448: in find_spec
    ???
<frozen importlib._bootstrap_external>:1222: in __init__
    ???
<frozen importlib._bootstrap_external>:1238: in _get_parent_path
    ???
E   KeyError: 'metabot'

(No idea what's going on here.)

pytest -o consider_namespace_packages=true
['/home/n/importhell/metabot.calendars.google',
 '/home/n/importhell/metabot.calendars.google/.venv/bin',
 '/usr/lib/python310.zip',
 '/usr/lib/python3.10',
 '/usr/lib/python3.10/lib-dynload',
 '/home/n/importhell/metabot.calendars.google/.venv/lib/python3.10/site-packages',
 '__editable__.metabot-0.3.20.1.finder.__path_hook__',
 '__editable__.metabot.calendars.google-0.2.finder.__path_hook__']
<module 'metabot' (<_frozen_importlib_external._NamespaceLoader object at 0x78ca84fecd60>)>
<module 'metabot.calendars' (<_frozen_importlib_external._NamespaceLoader object at 0x78ca84fecfa0>)>
<module 'metabot.calendars.base' from '/home/n/importhell/metabot/metabot/calendars/base.py'>
<module 'metabot.calendars.google' from '/home/n/importhell/metabot.calendars.google/metabot/calendars/google.py'>
E   assert 0

Thank you to https://github.com/pytest-dev/pytest/issues/2371#issuecomment-2021330057.