lk-geimfari / mimesis

Mimesis is a robust data generator for Python that can produce a wide range of fake data in multiple languages.
https://mimesis.name
MIT License
4.4k stars 334 forks source link

Codecov reporting wrong coverage information. #1489

Open lk-geimfari opened 8 months ago

lk-geimfari commented 8 months ago

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 commented 7 months ago

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

lk-geimfari commented 7 months ago

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 commented 7 months ago

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

lk-geimfari commented 7 months ago

@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 commented 7 months ago

yes

lk-geimfari commented 7 months ago

Alas, it didn't helped.

ghost commented 6 months ago

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 commented 6 months ago

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.