Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

[Bug]: openbb_econdb/utils/helpers.py json.load needs to use context manager #6508

Closed
jjfantini opened this issue Jun 18, 2024 · 4 comments · Fixed by #6512
Closed

[Bug]: openbb_econdb/utils/helpers.py json.load needs to use context manager #6508

jjfantini opened this issue Jun 18, 2024 · 4 comments · Fixed by #6512

Comments

@jjfantini
Copy link

jjfantini commented Jun 18, 2024

Describe the bug
I am running some pytests for my humblDATA package. I am using a few bob calls to get financial data, and I am running into an unclosed file error:

To Reproduce
Anytime I use a function that imports obb

Screenshots

=========================================================== FAILURES ============================================================
_________________________________________________ test_humblobject_availability _________________________________________________

cls = <class '_pytest.runner.CallInfo'>, func = <function call_and_report.<locals>.<lambda> at 0x30f039580>, when = 'call'
reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)

    @classmethod
    def from_call(
        cls,
        func: Callable[[], TResult],
        when: Literal["collect", "setup", "call", "teardown"],
        reraise: Optional[
            Union[Type[BaseException], Tuple[Type[BaseException], ...]]
        ] = None,
    ) -> "CallInfo[TResult]":
        """Call func, wrapping the result in a CallInfo.

        :param func:
            The function to call. Called without arguments.
        :param when:
            The phase in which the function is called.
        :param reraise:
            Exception or exceptions that shall propagate if raised by the
            function, instead of being wrapped in the CallInfo.
        """
        excinfo = None
        start = timing.time()
        precise_start = timing.perf_counter()
        try:
>           result: Optional[TResult] = func()

menv/lib/python3.11/site-packages/_pytest/runner.py:341:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
menv/lib/python3.11/site-packages/_pytest/runner.py:241: in <lambda>
    lambda: runtest_hook(item=item, **kwds), when=when, reraise=reraise
menv/lib/python3.11/site-packages/pluggy/_hooks.py:513: in __call__
    return self._hookexec(self.name, self._hookimpls.copy(), kwargs, firstresult)
menv/lib/python3.11/site-packages/pluggy/_manager.py:120: in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
menv/lib/python3.11/site-packages/_pytest/threadexception.py:87: in pytest_runtest_call
    yield from thread_exception_runtest_hook()
menv/lib/python3.11/site-packages/_pytest/threadexception.py:63: in thread_exception_runtest_hook
    yield
menv/lib/python3.11/site-packages/_pytest/unraisableexception.py:90: in pytest_runtest_call
    yield from unraisable_exception_runtest_hook()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

    def unraisable_exception_runtest_hook() -> Generator[None, None, None]:
        with catch_unraisable_exception() as cm:
            try:
                yield
            finally:
                if cm.unraisable:
                    if cm.unraisable.err_msg is not None:
                        err_msg = cm.unraisable.err_msg
                    else:
                        err_msg = "Exception ignored in"
                    msg = f"{err_msg}: {cm.unraisable.object!r}\n\n"
                    msg += "".join(
                        traceback.format_exception(
                            cm.unraisable.exc_type,
                            cm.unraisable.exc_value,
                            cm.unraisable.exc_traceback,
                        )
                    )
>                   warnings.warn(pytest.PytestUnraisableExceptionWarning(msg))
E                   pytest.PytestUnraisableExceptionWarning: Exception ignored in: <_io.FileIO [closed]>
E
E                   Traceback (most recent call last):
E                     File "/Users/jjfantini/github/humblFINANCE-org/humblDATA/menv/lib/python3.11/site-packages/openbb_econdb/utils/helpers.py", line 269, in <module>
E                       SYMBOL_TO_INDICATOR = json.load(
E                                             ^^^^^^^^^^
E                   ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/jjfantini/github/humblFINANCE-org/humblDATA/menv/lib/python3.11/site-packages/openbb_econdb/utils/symbol_to_indicator.json' mode='r' encoding='UTF-8'>

menv/lib/python3.11/site-packages/_pytest/unraisableexception.py:80: PytestUnraisableExceptionWarning
------------------- generated xml file: /Users/jjfantini/github/humblFINANCE-org/humblDATA/reports/pytest.xml -------------------
==================================================== short test summary info ====================================================
FAILED tests/unittests/core/standard_models/abstract/test_humblobject.py::test_humblobject_availability - pytest.PytestUnraisableExceptionWarning: Exception ignored in: <_io.FileIO [closed]>

Traceback (most recent call last):
  File "/Users/jjfantini/github/humblFINANCE-org/humblDATA/menv/lib/python3.11/site-packages/openbb_econdb/utils/helpers.py", line 269, in <module>
    SYMBOL_TO_INDICATOR = json.load(
                          ^^^^^^^^^^
ResourceWarning: unclosed file <_io.TextIOWrapper name='/Users/jjfantini/github/humblFINANCE-org/humblDATA/menv/lib/python3.11/site-packages/openbb_econdb/utils/symbol_to_indicator.json' mode='r' encoding='UTF-8'>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================= 1 failed in 8.03s =======================================================

Desktop (please complete the following information):

  • OS: macOS 15
  • Python version: 3.11.9
  • Micromamba Environment
  • OpenBB: 4.2.2

Additional context
It seems that using json.load() leaves files hanging open when setting up a pytest. These files should be read in with a context manager.

@jjfantini jjfantini changed the title [Bug]: [Bug]: openbb_econdb/utils/helpers.py json.load needs to use context manager Jun 18, 2024
@deeleeramone
Copy link
Contributor

Thanks for the report, that will be patched soon!

@jjfantini
Copy link
Author

When do you think this will make its way to the next patch...4.2.3?

@hjoaquim
Copy link
Contributor

When do you think this will make its way to the next patch...4.2.3?

Hi @jjfantini 👋 we just made this fix available on our latest release: https://pypi.org/project/openbb/ (4.2.3)

@jjfantini
Copy link
Author

Thank you for the quick fix! It saved me a headache in my test files 🤣

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants