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

Codecov reporting wrong coverage information. #1489

Open
lk-geimfari opened this issue Jan 31, 2024 · 8 comments
Open

Codecov reporting wrong coverage information. #1489

lk-geimfari opened this issue Jan 31, 2024 · 8 comments

Comments

@lk-geimfari
Copy link
Owner

lk-geimfari commented Jan 31, 2024

I have no idea why, but commit 79f6257 (Add pytest entry in pyproject.toml) decreased coverage by 42.21%. Clearly, Codecov is reporting incorrect coverage information and the question is "Why"?

@lk-geimfari
Copy link
Owner Author

@sobolevn Would you have any guesses about that? Ever run into anything like this?

@lk-geimfari
Copy link
Owner Author

This is what happens when we comment out the pytest plugin: https://app.codecov.io/gh/lk-geimfari/mimesis/pull/1502

The coverage increased to 99%.

@sobolevn
Copy link
Collaborator

sobolevn commented Feb 29, 2024

Try to move imports inside the fixture functions. sys.modules is populated too early.

@lk-geimfari
Copy link
Owner Author

@sobolevn Do you mean like this?

@pytest.fixture(scope="session")
def _mimesis_cache() -> _CacheCallable:
    from mimesis.locales import Locale
    from mimesis.schema import Field

    cached_instances: dict[Locale, Field] = {}

    def factory(locale: Locale) -> Field:
        if locale not in cached_instances:
            cached_instances[locale] = Field(locale)
        return cached_instances[locale]

    return factory


@pytest.fixture()
def mimesis_locale():  # type: ignore
    """Specifies which locale to use."""
    from mimesis.locales import Locale

    return Locale.DEFAULT

@sobolevn
Copy link
Collaborator

sobolevn commented Mar 1, 2024

yes

@lk-geimfari
Copy link
Owner Author

lk-geimfari commented Mar 2, 2024

Alas, it didn't helped.

@ghost
Copy link

ghost commented Mar 21, 2024

This is due to how the import system works and how you're using __init__.py.
Neither are wrong or bad.

When python tries to import mimesis.plugins.pytest, it loads mimesis.__init__.py and looks for plugins.
Because the init refers to most of the files in the package, they're all loaded too early.

You can see this in action by doing the following:
Modify as many files as you dare in the mimesis folder with the following line:
from pathlib import Path; (Path(__file__).parent / Path(__file__).stem).touch()

Now run the python repl and enter this: from mimesis.plugins import bloop

You'll get a few new files created for your trouble:
image

@lk-geimfari
Copy link
Owner Author

This is due to how the import system works and how you're using __init__.py. Neither are wrong or bad.

When python tries to import mimesis.plugins.pytest, it loads mimesis.__init__.py and looks for plugins. Because the init refers to most of the files in the package, they're all loaded too early.

You can see this in action by doing the following: Modify as many files as you dare in the mimesis folder with the following line: from pathlib import Path; (Path(__file__).parent / Path(__file__).stem).touch()

Now run the python repl and enter this: from mimesis.plugins import bloop

You'll get a few new files created for your trouble: image

Yeah, I get it, but i did not figured out how can I fix this without breaking anything. Ideas or PR are welcomen.

@ghost ghost mentioned this issue Mar 24, 2024
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.

2 participants