Skip to content

More robust import of tests.large_text module #10606

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

Conversation

msabramo
Copy link
Contributor

@msabramo msabramo commented May 6, 2025

Maybe this is just something in my environment; maybe not. When trying to run make test-unit today, I kept getting an error:

ModuleNotFoundError: No module named 'tests.large_text'

E.g.:

Screenshot 2025-05-06 at 5 04 01 PM Screenshot 2025-05-06 at 5 04 13 PM
(.venv)
abramowi at Marcs-MBP-3 in ~/Code/OpenSource/litellm (main●●)
$ make test-unit
poetry run pytest tests/litellm/
============================================================= test session starts =============================================================
platform darwin -- Python 3.12.9, pytest-7.4.4, pluggy-1.5.0
rootdir: /Users/abramowi/Code/OpenSource/litellm
plugins: respx-0.22.0, postgresql-7.0.1, anyio-4.5.2, asyncio-0.21.2, mock-3.14.0, requests-mock-1.12.1
asyncio: mode=Mode.STRICT
collected 662 items / 2 errors

=================================================================== ERRORS ====================================================================
___________________________________ ERROR collecting tests/litellm/litellm_core_utils/test_token_counter.py ___________________________________
ImportError while importing test module '/Users/abramowi/Code/OpenSource/litellm/tests/litellm/litellm_core_utils/test_token_counter.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/litellm/litellm_core_utils/test_token_counter.py:26: in <module>
    from tests.large_text import text
E   ModuleNotFoundError: No module named 'tests.large_text'
________________________________ ERROR collecting tests/litellm/litellm_core_utils/test_token_counter_tool.py _________________________________
ImportError while importing test module '/Users/abramowi/Code/OpenSource/litellm/tests/litellm/litellm_core_utils/test_token_counter_tool.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/python@3.12/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/litellm/litellm_core_utils/test_token_counter_tool.py:13: in <module>
    from test_token_counter import token_counter
tests/litellm/litellm_core_utils/test_token_counter.py:26: in <module>
    from tests.large_text import text
E   ModuleNotFoundError: No module named 'tests.large_text'
============================================================== warnings summary ===============================================================
tests/litellm/integrations/test_custom_prompt_management.py:27
  /Users/abramowi/Code/OpenSource/litellm/tests/litellm/integrations/test_custom_prompt_management.py:27: PytestCollectionWarning: cannot collect test class 'TestCustomPromptManagement' because it has a __init__ constructor (from: tests/litellm/integrations/test_custom_prompt_management.py)
    class TestCustomPromptManagement(CustomPromptManagement):

tests/litellm/integrations/arize/test_arize_utils.py:181
  /Users/abramowi/Code/OpenSource/litellm/tests/litellm/integrations/arize/test_arize_utils.py:181: PytestCollectionWarning: cannot collect test class 'TestArizeLogger' because it has a __init__ constructor (from: tests/litellm/integrations/arize/test_arize_utils.py)
    class TestArizeLogger(CustomLogger):

tests/litellm/litellm_core_utils/test_streaming_handler.py:505
  /Users/abramowi/Code/OpenSource/litellm/tests/litellm/litellm_core_utils/test_streaming_handler.py:505: PytestUnknownMarkWarning: Unknown pytest.mark.flaky - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.flaky(reruns=3)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================================================== short test summary info ===========================================================
ERROR tests/litellm/litellm_core_utils/test_token_counter.py
ERROR tests/litellm/litellm_core_utils/test_token_counter_tool.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================== 3 warnings, 2 errors in 1.75s ========================================================
make: *** [test-unit] Error 2

I think it's because the existing sys.path.insert hack depends on what directory the user is in (it depends on the user's current directory when running pytest; it's not related to the location of the test file, though I often make that mistake myself).

For example if I'm in ~/Code/OpenSource/litellm when I run make test-unit then os.path.abspath("../../..") evaluates to '/Users/abramowi'.

Screenshot 2025-05-06 at 5 07 59 PM

I changed it to actually make the path relative to the current file, which should be more robust than having it depend on the user's current directory.

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details

  • I have added a screenshot of my new test passing locally

    Screenshot 2025-05-06 at 4 57 32 PM Screenshot 2025-05-06 at 5 01 49 PM
  • My PR passes all unit tests on make test-unit

  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🐛 Bug Fix
✅ Test

Changes

Maybe this is just something in my environment; maybe not. When trying
to run `make test-unit` today, I kept getting this:

```
(.venv)
abramowi at Marcs-MBP-3 in ~/Code/OpenSource/litellm (main●●)
$ make test-unit
poetry run pytest tests/litellm/
============================================================= test session starts =============================================================
platform darwin -- Python 3.12.9, pytest-7.4.4, pluggy-1.5.0
rootdir: /Users/abramowi/Code/OpenSource/litellm
plugins: respx-0.22.0, postgresql-7.0.1, anyio-4.5.2, asyncio-0.21.2, mock-3.14.0, requests-mock-1.12.1
asyncio: mode=Mode.STRICT
collected 662 items / 2 errors

=================================================================== ERRORS ====================================================================
___________________________________ ERROR collecting tests/litellm/litellm_core_utils/test_token_counter.py ___________________________________
ImportError while importing test module '/Users/abramowi/Code/OpenSource/litellm/tests/litellm/litellm_core_utils/test_token_counter.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/litellm/litellm_core_utils/test_token_counter.py:26: in <module>
    from tests.large_text import text
E   ModuleNotFoundError: No module named 'tests.large_text'
________________________________ ERROR collecting tests/litellm/litellm_core_utils/test_token_counter_tool.py _________________________________
ImportError while importing test module '/Users/abramowi/Code/OpenSource/litellm/tests/litellm/litellm_core_utils/test_token_counter_tool.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/homebrew/Cellar/[email protected]/3.12.9/Frameworks/Python.framework/Versions/3.12/lib/python3.12/importlib/__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/litellm/litellm_core_utils/test_token_counter_tool.py:13: in <module>
    from test_token_counter import token_counter
tests/litellm/litellm_core_utils/test_token_counter.py:26: in <module>
    from tests.large_text import text
E   ModuleNotFoundError: No module named 'tests.large_text'
============================================================== warnings summary ===============================================================
tests/litellm/integrations/test_custom_prompt_management.py:27
  /Users/abramowi/Code/OpenSource/litellm/tests/litellm/integrations/test_custom_prompt_management.py:27: PytestCollectionWarning: cannot collect test class 'TestCustomPromptManagement' because it has a __init__ constructor (from: tests/litellm/integrations/test_custom_prompt_management.py)
    class TestCustomPromptManagement(CustomPromptManagement):

tests/litellm/integrations/arize/test_arize_utils.py:181
  /Users/abramowi/Code/OpenSource/litellm/tests/litellm/integrations/arize/test_arize_utils.py:181: PytestCollectionWarning: cannot collect test class 'TestArizeLogger' because it has a __init__ constructor (from: tests/litellm/integrations/arize/test_arize_utils.py)
    class TestArizeLogger(CustomLogger):

tests/litellm/litellm_core_utils/test_streaming_handler.py:505
  /Users/abramowi/Code/OpenSource/litellm/tests/litellm/litellm_core_utils/test_streaming_handler.py:505: PytestUnknownMarkWarning: Unknown pytest.mark.flaky - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/stable/how-to/mark.html
    @pytest.mark.flaky(reruns=3)

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
=========================================================== short test summary info ===========================================================
ERROR tests/litellm/litellm_core_utils/test_token_counter.py
ERROR tests/litellm/litellm_core_utils/test_token_counter_tool.py
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================== 3 warnings, 2 errors in 1.75s ========================================================
make: *** [test-unit] Error 2
```

I think it's because the existing `sys.path.insert` hack depends on what
directory the user is in (it depends on the user's current directory
when running `pytest`; it's not related to the location of the test
file, though I often make that mistake myself). I changed it to actually
make the path relative to the current file, which should be more robust
than having it depend on the user's current directory.
Copy link

vercel bot commented May 6, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
litellm ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 6, 2025 11:57pm

@msabramo
Copy link
Contributor Author

msabramo commented May 7, 2025

I guess you guys still have reservations about #10484?

I ask because not only does that improve the speed of pytest runs, but it makes the output less noisier. Notice how in the output below, the filename test_daily_spend_update_queue.py is repeated several times

(.venv)
abramowi at Marcs-MBP-3 in ~/Code/OpenSource/litellm (more-robust-import-of-large_text-module●)
$ make test-unit
...
tests/litellm/proxy/db/db_transaction_queue/test_daily_spend_update_queue.py ..                                                         [  0%]
tests/litellm/proxy/db/db_transaction_queue/test_spend_update_queue.py .                                                                [  0%]
tests/litellm/integrations/test_agentops.py ....                                                                                        [  1%]
tests/litellm/proxy/db/db_transaction_queue/test_daily_spend_update_queue.py .                                                          [  1%]
tests/litellm/proxy/db/db_transaction_queue/test_spend_update_queue.py .                                                                [  1%]
tests/litellm/proxy/db/db_transaction_queue/test_daily_spend_update_queue.py .
Screenshot 2025-05-06 at 5 13 06 PM

Here's another example of the repeating filenames:

tests/litellm/proxy/client/test_http_client.py .                                                                                        [ 71%]
tests/litellm/proxy/client/test_http_commands.py .                                                                                      [ 71%]
tests/litellm/proxy/client/test_http_client.py .                                                                                        [ 71%]
tests/litellm/proxy/client/test_http_commands.py .                                                                                      [ 72%]
tests/litellm/proxy/client/test_http_client.py .                                                                                        [ 72%]
tests/litellm/proxy/client/test_http_commands.py .                                                                                      [ 72%]
tests/litellm/proxy/client/test_http_client.py .                                                                                        [ 72%]
tests/litellm/proxy/client/test_http_commands.py .                                                                                      [ 72%]
tests/litellm/proxy/client/test_http_client.py .                                                                                        [ 72%]
tests/litellm/proxy/client/test_http_commands.py .
Screenshot 2025-05-06 at 5 12 01 PM

@msabramo msabramo changed the title More robust import of large_text module More robust import of tests.large_text module May 7, 2025
@msabramo
Copy link
Contributor Author

Closing in favor of #10662 which fixes this in a nicer way using conftest.py.

@msabramo msabramo closed this May 13, 2025
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 this pull request may close these issues.

1 participant