Skip to content

Commit

Permalink
general: add an adhoc test for checking mixin behaviour with namespac…
Browse files Browse the repository at this point in the history
…e packages and __init__.py hack

also use that hack in my.fbmessenger
  • Loading branch information
karlicoss committed Jun 1, 2022
1 parent 179b657 commit 8336d18
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions misc/check_legacy_init_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def check_ok(*cmd: str, **kwargs) -> None:
# note: dump_chat_history should really be deprecated, but it's a quick way to check we actually fell back to fbmessenger/export.py
# NOTE: this is the most common legacy usecase
check_warn('-c', 'from my.fbmessenger import messages, dump_chat_history')
check_warn('-m', 'my.core', 'query' , 'my.fbmessenger.messages')
check_warn('-m', 'my.core', 'query' , 'my.fbmessenger.messages', '-o', 'pprint', '--limit=10')
check_warn('-m', 'my.core', 'doctor', 'my.fbmessenger')

# todo kinda annoying it doesn't work when executed as -c (but does as script!)
Expand All @@ -69,8 +69,14 @@ def check_ok(*cmd: str, **kwargs) -> None:
check_ok ('-c', 'import my.fbmessenger.export')
check_ok ('-c', 'from my.fbmessenger.export import *')
check_ok ('-c', 'from my.fbmessenger.export import messages, dump_chat_history')
check_ok ('-m', 'my.core', 'query' , 'my.fbmessenger.export.messages')
check_ok ('-m', 'my.core', 'query' , 'my.fbmessenger.export.messages', '-o', 'pprint', '--limit=10')
check_ok ('-m', 'my.core', 'doctor', 'my.fbmessenger.export')

# NOTE:
# to check that overlays work, run something like
# PYTHONPATH=misc/overlay_for_init_py_test/ hpi query my.fbmessenger.all.messages -s -o pprint --limit=10
# you should see 1, 2, 3 from mixin.py
# TODO would be nice to add an automated test for this

# TODO with reddit, currently these don't work properly at all
# only when imported from scripts etc?
7 changes: 7 additions & 0 deletions misc/overlay_for_init_py_test/my/fbmessenger/all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from my.fbmessenger import export
from . import mixin


def messages():
yield from mixin.messages()
yield from export.messages()
2 changes: 2 additions & 0 deletions misc/overlay_for_init_py_test/my/fbmessenger/mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def messages():
yield from ['1', '2', '3']
13 changes: 13 additions & 0 deletions my/fbmessenger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@

# to prevent it from apprearing in modules list/doctor
from ..core import __NOT_HPI_MODULE__

###
# this is to trick mypy into treating this as a proper namespace package
# should only be used for backwards compatibility on packages that are convernted into namespace & all.py pattern
# - https://www.python.org/dev/peps/pep-0382/#namespace-packages-today
# - https://github.com/karlicoss/hpi_namespace_experiment
# - discussion here https://memex.zulipchat.com/#narrow/stream/279601-hpi/topic/extending.20HPI/near/269946944
from pkgutil import extend_path
__path__ = extend_path(__path__, __name__)
# 'this' source tree ends up first in the pythonpath when we extend_path()
# so we need to move 'this' source tree towards the end to make sure we prioritize overlays
__path__ = __path__[1:] + __path__[:1]
###

0 comments on commit 8336d18

Please sign in to comment.