From 8336d184348b166414f912618ca2ad85653db215 Mon Sep 17 00:00:00 2001 From: Dima Gerasimov Date: Wed, 1 Jun 2022 21:57:36 +0100 Subject: [PATCH] general: add an adhoc test for checking mixin behaviour with namespace packages and __init__.py hack also use that hack in my.fbmessenger --- misc/check_legacy_init_py.py | 10 ++++++++-- misc/overlay_for_init_py_test/my/fbmessenger/all.py | 7 +++++++ .../my/fbmessenger/mixin.py | 2 ++ my/fbmessenger/__init__.py | 13 +++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 misc/overlay_for_init_py_test/my/fbmessenger/all.py create mode 100644 misc/overlay_for_init_py_test/my/fbmessenger/mixin.py diff --git a/misc/check_legacy_init_py.py b/misc/check_legacy_init_py.py index 53eb1697..102b9244 100755 --- a/misc/check_legacy_init_py.py +++ b/misc/check_legacy_init_py.py @@ -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!) @@ -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? diff --git a/misc/overlay_for_init_py_test/my/fbmessenger/all.py b/misc/overlay_for_init_py_test/my/fbmessenger/all.py new file mode 100644 index 00000000..848de5f3 --- /dev/null +++ b/misc/overlay_for_init_py_test/my/fbmessenger/all.py @@ -0,0 +1,7 @@ +from my.fbmessenger import export +from . import mixin + + +def messages(): + yield from mixin.messages() + yield from export.messages() diff --git a/misc/overlay_for_init_py_test/my/fbmessenger/mixin.py b/misc/overlay_for_init_py_test/my/fbmessenger/mixin.py new file mode 100644 index 00000000..2f694806 --- /dev/null +++ b/misc/overlay_for_init_py_test/my/fbmessenger/mixin.py @@ -0,0 +1,2 @@ +def messages(): + yield from ['1', '2', '3'] diff --git a/my/fbmessenger/__init__.py b/my/fbmessenger/__init__.py index 2e60d17c..2a3ba7f9 100644 --- a/my/fbmessenger/__init__.py +++ b/my/fbmessenger/__init__.py @@ -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] +###