Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Improve usability of jp_read_emitted_events fixture #74

Merged
merged 2 commits into from
Apr 17, 2023

Conversation

kevin-bates
Copy link
Member

Prior to this change, if a test using events attempts to read emitted events using the fixture jp_read_emitted_events where either no events were emitted or the handler wasn't registered correctly, a JSONDecodeError will be produced, along with its stack trace:

tests/test_gateway.py:504: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
/opt/miniconda3/envs/server-dev/lib/python3.9/site-packages/jupyter_events/pytest_plugin.py:30: in _read
    output = [json.loads(item) for item in lines]
/opt/miniconda3/envs/server-dev/lib/python3.9/site-packages/jupyter_events/pytest_plugin.py:30: in <listcomp>
    output = [json.loads(item) for item in lines]
/opt/miniconda3/envs/server-dev/lib/python3.9/json/__init__.py:346: in loads
    return _default_decoder.decode(s)
/opt/miniconda3/envs/server-dev/lib/python3.9/json/decoder.py:337: in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <json.decoder.JSONDecoder object at 0x10e8f1e50>, s = '', idx = 0

    def raw_decode(self, s, idx=0):
        """Decode a JSON document from ``s`` (a ``str`` beginning with
        a JSON document) and return a 2-tuple of the Python
        representation and the index in ``s`` where the document ended.
    
        This can be used to decode a JSON document from a string that may
        have extraneous data at the end.
    
        """
        try:
            obj, end = self.scan_once(s, idx)
        except StopIteration as err:
>           raise JSONDecodeError("Expecting value", s, err.value) from None
E           json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

/opt/miniconda3/envs/server-dev/lib/python3.9/json/decoder.py:355: JSONDecodeError

This pull request avoids the call to json.loads() when the event sink is empty and returns None, leading to a more friendly and intuitive error situation:

>       output = jp_read_emitted_events()[0]
E       TypeError: 'NoneType' object is not subscriptable

tests/test_gateway.py:504: TypeError

While looking into this change, I noticed that the default value for the handlers trait was set to [] despite the help string stating that None is the default, so I've gone ahead and changed the default to None.

@kevin-bates kevin-bates requested a review from Zsailer April 17, 2023 16:42
@kevin-bates kevin-bates added the bug Something isn't working label Apr 17, 2023
@Zsailer
Copy link
Member

Zsailer commented Apr 17, 2023

Thanks, @kevin-bates! LGTM!

@Zsailer Zsailer merged commit a0082af into jupyter:main Apr 17, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants