From e73f1f402b96e7761a16a39419f9fd4c1938f1fc Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 13:39:51 +0300 Subject: [PATCH 01/11] Add .envrc file --- .envrc | 1 + .gitignore | 3 +++ 2 files changed, 4 insertions(+) create mode 100644 .envrc diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..94840b3 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +layout python3 diff --git a/.gitignore b/.gitignore index 7bdcea6..e27d017 100644 --- a/.gitignore +++ b/.gitignore @@ -103,5 +103,8 @@ venv.bak/ # mypy .mypy_cache/ +# direnv subdir +.direnv/ + # Emacs backup files *~ From 847deee653bbad78a13b1a826d39f150d71a7080 Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 13:40:02 +0300 Subject: [PATCH 02/11] Fix test exception text --- tests/test_basics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_basics.py b/tests/test_basics.py index fa38266..4d4a240 100644 --- a/tests/test_basics.py +++ b/tests/test_basics.py @@ -331,6 +331,6 @@ def task(): worker.start() worker.join() - match = "default broker doesn't have a results backend" + match = "broker doesn't have a results backend" with pytest.raises(RuntimeError, match=match): job.get_result() From b98ac8522851de66c27a2c7cec48245d8a959e6a Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 13:47:21 +0300 Subject: [PATCH 03/11] Add __code__() to ProxiedInstanceMixin --- flask_melodramatiq/lazy_broker.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index 055698f..f7ac87b 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -70,6 +70,11 @@ def __repr__(self): return object.__repr__(self) return repr(self._proxied_instance) + def __code__(self): + if self._proxied_instance is None: + return object.__code__(self) + return self._proxied_instance.__code__ + def __getattr__(self, name): if self._proxied_instance is None: raise RuntimeError( From 7858d2d4f1764f97e795118adc6f69e79945a7eb Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 13:50:55 +0300 Subject: [PATCH 04/11] Revert adding __code__() to ProxiedInstanceMixin --- flask_melodramatiq/lazy_broker.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index f7ac87b..055698f 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -70,11 +70,6 @@ def __repr__(self): return object.__repr__(self) return repr(self._proxied_instance) - def __code__(self): - if self._proxied_instance is None: - return object.__code__(self) - return self._proxied_instance.__code__ - def __getattr__(self, name): if self._proxied_instance is None: raise RuntimeError( From 767f70cf1606012ec4b30e74f4af49fd3933a078 Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 13:51:26 +0300 Subject: [PATCH 05/11] Use raw string for Broker's docstring --- flask_melodramatiq/lazy_broker.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index 055698f..7b83a17 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -359,7 +359,7 @@ def after_process_boot(self, broker): class Broker(LazyBrokerMixin, dramatiq.brokers.stub.StubBroker): __doc__ = LAZY_BROKER_DOCSTRING_TEMPLATE.format( - description="""A lazy broker of dynamically configurable type. + description=r"""A lazy broker of dynamically configurable type. The type of the broker should be specified by the "*config_prefix*\_CLASS" setting in the Flask application From d89c91480101f566104d091a769d3d9c5db95e5a Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 13:53:49 +0300 Subject: [PATCH 06/11] Add print --- flask_melodramatiq/lazy_broker.py | 1 + 1 file changed, 1 insertion(+) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index 7b83a17..ba266f9 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -72,6 +72,7 @@ def __repr__(self): def __getattr__(self, name): if self._proxied_instance is None: + print(name) raise RuntimeError( 'init_app() must be called on lazy brokers before use. ' 'Did you forget to pass the "app" to broker\'s constructor?' From 923e7b45091c4f54da7c10a566e7648695ed4710 Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 14:05:40 +0300 Subject: [PATCH 07/11] Check for __code__ in ProxiedInstanceMixin.__getattr__ --- flask_melodramatiq/lazy_broker.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index ba266f9..3bd5e2f 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -72,7 +72,10 @@ def __repr__(self): def __getattr__(self, name): if self._proxied_instance is None: - print(name) + if name == '__code__': + # `iscoroutinefunction` may be called on Actor instances, + # which calls __code__(). + raise AttributeError raise RuntimeError( 'init_app() must be called on lazy brokers before use. ' 'Did you forget to pass the "app" to broker\'s constructor?' From 762768261762e6854321a32b245ed754dfddba47 Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 14:09:52 +0300 Subject: [PATCH 08/11] Check for __defaults__ in ProxiedInstanceMixin.__getattr__ --- flask_melodramatiq/lazy_broker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index 3bd5e2f..29d751e 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -72,9 +72,9 @@ def __repr__(self): def __getattr__(self, name): if self._proxied_instance is None: - if name == '__code__': + if name in ['__code__', '__defaults__']: # `iscoroutinefunction` may be called on Actor instances, - # which calls __code__(). + # which calls __code__() and __defaults__(). raise AttributeError raise RuntimeError( 'init_app() must be called on lazy brokers before use. ' From 5134178127fa2404becbf15cecd49fe349c034b7 Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 14:11:34 +0300 Subject: [PATCH 09/11] Check for __defaults__ in ProxiedInstanceMixin.__getattr__ --- flask_melodramatiq/lazy_broker.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index 29d751e..782fa64 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -72,9 +72,9 @@ def __repr__(self): def __getattr__(self, name): if self._proxied_instance is None: - if name in ['__code__', '__defaults__']: + if name in ['__code__', '__defaults__', '__kwdefaults__']: # `iscoroutinefunction` may be called on Actor instances, - # which calls __code__() and __defaults__(). + # which checks these names. raise AttributeError raise RuntimeError( 'init_app() must be called on lazy brokers before use. ' From 9cdb4defbddf6159e102d909c7d616b5a0c9c0ff Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 14:16:03 +0300 Subject: [PATCH 10/11] Check for _is_coroutine_marker in ProxiedInstanceMixin.__getattr__ --- flask_melodramatiq/lazy_broker.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index 782fa64..2796d74 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -72,7 +72,12 @@ def __repr__(self): def __getattr__(self, name): if self._proxied_instance is None: - if name in ['__code__', '__defaults__', '__kwdefaults__']: + if name in [ + '__code__', + '__defaults__', + '__kwdefaults__', + '_is_coroutine_marker', + ]: # `iscoroutinefunction` may be called on Actor instances, # which checks these names. raise AttributeError From 5ec10e150401db6a73816acf5476010f875cada5 Mon Sep 17 00:00:00 2001 From: Evgeni Pandurski Date: Wed, 25 Oct 2023 15:23:25 +0300 Subject: [PATCH 11/11] Improve comment --- flask_melodramatiq/lazy_broker.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flask_melodramatiq/lazy_broker.py b/flask_melodramatiq/lazy_broker.py index 2796d74..39d3e3e 100644 --- a/flask_melodramatiq/lazy_broker.py +++ b/flask_melodramatiq/lazy_broker.py @@ -78,8 +78,9 @@ def __getattr__(self, name): '__kwdefaults__', '_is_coroutine_marker', ]: - # `iscoroutinefunction` may be called on Actor instances, - # which checks these names. + # For `GenericActor`s, `iscoroutinefunction` may be called + # on unregistered actor proxies, which checks for these + # attribute names. raise AttributeError raise RuntimeError( 'init_app() must be called on lazy brokers before use. '