How to correctly use PEP 604 – Allow writing union types as X | Y

96 views Asked by At

I have following code:

import logging

from synapse import module_api
from synapse.events import EventBase
from synapse.module_api import ModuleApi


class SpamCheckerConfig:
    pass


class SpamChecker:
    api: ModuleApi
    logger = logging.getLogger(__name__)

    def __init__(self, config: SpamCheckerConfig, api: ModuleApi):
        self.api = api
        self.logger.info("Initialized Module %s", __name__)

    async def check_event_for_spam(self, event: EventBase) -> module_api.NOT_SPAM | module_api.errors.Codes:
        self.logger.info("Checking event for spam: %s", event)
        return module_api.NOT_SPAM

The method signature is from the Synapse documentation: https://matrix-org.github.io/synapse/develop/modules/spam_checker_callbacks.html#check_event_for_spam

Python version used is Python 3.11

However, I get following error:

Starting synapse with args -m synapse.app.homeserver --config-path /data/homeserver.yaml
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/usr/local/lib/python3.11/site-packages/synapse/app/homeserver.py", line 399, in <module>
    main()
  File "/usr/local/lib/python3.11/site-packages/synapse/app/homeserver.py", line 389, in main
    hs = setup(sys.argv[1:])
         ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/synapse/app/homeserver.py", line 298, in setup
    config = HomeServerConfig.load_or_generate_config(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/synapse/config/_base.py", line 802, in load_or_generate_config
    obj.parse_config_dict(
  File "/usr/local/lib/python3.11/site-packages/synapse/config/_base.py", line 823, in parse_config_dict
    self.invoke_all(
  File "/usr/local/lib/python3.11/site-packages/synapse/config/_base.py", line 422, in invoke_all
    res[config_class.section] = getattr(config, func_name)(*args, **kwargs)
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/synapse/config/modules.py", line 33, in read_config
    self.loaded_modules.append(load_module(module, config_path))
                               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/synapse/util/module_loader.py", line 47, in load_module
    module = importlib.import_module(module_name)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/app/modules/mportal/spam_checker.py", line 12, in <module>
    class SpamChecker:
  File "/app/modules/mportal/spam_checker.py", line 20, in SpamChecker
    async def check_event_for_spam(self, event: EventBase) -> module_api.NOT_SPAM | module_api.errors.Codes:
                                                              ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
TypeError: unsupported operand type(s) for |: 'str' and 'EnumType'

Why is this happening? PEP 604 suggests that it should work. How can I make the code work?

0

There are 0 answers