coveragepy icon indicating copy to clipboard operation
coveragepy copied to clipboard

With python 3 modules are importable even without __init__.py

Open felix-ht opened this issue 2 years ago • 3 comments

With https://peps.python.org/pep-0420/ - released with python 3.3 it is no longer required to create an empty __init__.py file.

However Coverage still skips folders without an __init__.py

https://github.com/nedbat/coveragepy/blob/0aa1070a2c9a99e10b7790b9a6a40a631ba5514e/coverage/files.py#L409

This causes unexpected results for the total coverage as valid python code is not included in the total coverage!

rel: https://github.com/pytest-dev/pytest-cov/issues/88#issuecomment-667079121

felix-ht avatar May 25 '22 10:05 felix-ht

@felix-ht I don't know how common it is for people to omit the __init__.py file. That logic was there originally so that bin/ directories would be skipped since the files in them wouldn't be importable. Maybe an option?

BTW, how did you create this issue? There are no labels on it that usually get assigned to new issues.

nedbat avatar May 27 '22 11:05 nedbat

The official use case for omitting the init.py is to create a namespace packages (Having to separate packages that share a namespace).

An option would really be an good solution - something like --include_namespace_packages

I created the issue by going to the source code mentioned above and clicked reference in new issue. This seems to skip the normal template step. image

felix-ht avatar May 27 '22 12:05 felix-ht

I have this directory hierarchy.

src/
    __init__.py
    main.py
    app.py

tests/
   test_app.py

My .coveragerc looks like this.

[run]
omit=
    # omit everything in test
    test/**
[paths]
source=
    # Include all files in source.
    # This still doesn't seem to include files that
    # don't have an explicit test for them.
    # Seems to be an issue with the way pytest-cov works.
    # Need to resolve this if this we start to have uncovered files.
    src/**

When I run

pytest --cov --cov-report=html

my coverage report looks like

Module statements missing excluded coverage
src/app.py 19 0 0 100%
Total 19 0 0 100%

I would have expected main.py in the report. Any help on what I have missed that causes that to happen?

createdbysk avatar Jul 01 '22 15:07 createdbysk

@createdbysk: I don't know what is in your main.py; have you tested forcing values in the report sections? such as skip_empty, etc.

romain-dartigues avatar Sep 19 '22 13:09 romain-dartigues

@createdbysk Your problem seems to be the opposite of the issue described at the top: you have a __init__.py file. Can you try changing your source= setting from src/** to just plain src? If that doesn't work, please open a new issue.

nedbat avatar Nov 13 '22 23:11 nedbat

This was finished in commit 709c7443.

nedbat avatar Nov 17 '22 12:11 nedbat

This is now released as part of coverage 7.0.0b1.

nedbat avatar Dec 03 '22 18:12 nedbat

Are you sure this is part of 7.0.0b1 :thinking:?

[coverage:run]
branch = true
parallel = true
source =
  src/
  tests/

[coverage:paths]
equivalent =
  src/
  __pypackages__/

[coverage:report]
include_namespace_packages = true
precision = 2
omit =
  src/*/__init__.py
  src/*/__main__.py
  tests/__init__.py

[coverage:json]
output = htmlcov/coverage.json
% for py in 3.7 3.8 3.9 3.10 3.11; do python$py -m pip freeze | grep coverage; done                    
coverage==7.0.0b1
coverage==7.0.0b1
coverage==7.0.0b1
coverage==7.0.0b1
coverage==7.0.0b1

# however:
% for py in 3.7 3.8 3.9 3.10 3.11; do python$py -c 'import coverage; print(coverage.__version__)'; done
6.5.0
6.5.0
6.5.0
6.5.0
6.5.0
# maybe not yet updated since pre-release?
% for py in 3.7 3.8 3.9 3.10 3.11; do python$py -m coverage report --rcfile=config/coverage.ini; done 
/media/data/dev/mkdocstrings-python/__pypackages__/3.7/lib/coverage/config.py:306: CoverageWarning: Unrecognized option '[coverage:report] include_namespace_packages=' in config file config/coverage.ini
  real_section, unknown, filename
Name                                            Stmts   Miss Branch BrPart     Cover
------------------------------------------------------------------------------------
src/mkdocstrings_handlers/python/handler.py       111     23     36      7    70.07%
src/mkdocstrings_handlers/python/rendering.py      92     39     40      6    56.82%
tests/conftest.py                                  30      1      2      0    96.88%
tests/test_handler.py                              24      0      0      0   100.00%
tests/test_rendering.py                            16      0      6      0   100.00%
tests/test_themes.py                                9      0      0      0   100.00%
------------------------------------------------------------------------------------
TOTAL                                             282     63     84     13    72.13%
/media/data/dev/mkdocstrings-python/__pypackages__/3.8/lib/coverage/config.py:304: CoverageWarning: Unrecognized option '[coverage:report] include_namespace_packages=' in config file config/coverage.ini
  warn(
Name                                            Stmts   Miss Branch BrPart     Cover
------------------------------------------------------------------------------------
src/mkdocstrings_handlers/python/handler.py       111     23     36      7    70.07%
src/mkdocstrings_handlers/python/rendering.py      93     39     40      6    57.14%
tests/conftest.py                                  34      1      2      0    97.22%
tests/test_handler.py                              25      0      0      0   100.00%
tests/test_rendering.py                            18      0      6      0   100.00%
tests/test_themes.py                               10      0      0      0   100.00%
------------------------------------------------------------------------------------
TOTAL                                             291     63     84     13    72.80%
/media/data/dev/mkdocstrings-python/__pypackages__/3.9/lib/coverage/config.py:304: CoverageWarning: Unrecognized option '[coverage:report] include_namespace_packages=' in config file config/coverage.ini
  warn(
Name                                            Stmts   Miss Branch BrPart     Cover
------------------------------------------------------------------------------------
src/mkdocstrings_handlers/python/handler.py       111     23     36      7    70.07%
src/mkdocstrings_handlers/python/rendering.py      93     39     40      6    57.14%
tests/conftest.py                                  34      1      2      0    97.22%
tests/test_handler.py                              25      0      0      0   100.00%
tests/test_rendering.py                            18      0      6      0   100.00%
tests/test_themes.py                               10      0      0      0   100.00%
------------------------------------------------------------------------------------
TOTAL                                             291     63     84     13    72.80%
/media/data/dev/mkdocstrings-python/__pypackages__/3.10/lib/coverage/config.py:304: CoverageWarning: Unrecognized option '[coverage:report] include_namespace_packages=' in config file config/coverage.ini
  warn(
Name                                            Stmts   Miss Branch BrPart     Cover
------------------------------------------------------------------------------------
src/mkdocstrings_handlers/python/handler.py       112     23     38      7    69.33%
src/mkdocstrings_handlers/python/rendering.py      93     39     40      6    57.14%
tests/conftest.py                                  34      1      2      0    97.22%
tests/test_handler.py                              25      0      4      0   100.00%
tests/test_rendering.py                            18      0      6      0   100.00%
tests/test_themes.py                               10      0      0      0   100.00%
------------------------------------------------------------------------------------
TOTAL                                             292     63     90     13    72.77%
/media/data/dev/mkdocstrings-python/__pypackages__/3.11/lib/coverage/config.py:304: CoverageWarning: Unrecognized option '[coverage:report] include_namespace_packages=' in config file config/coverage.ini
  warn(
Name                                            Stmts   Miss Branch BrPart     Cover
------------------------------------------------------------------------------------
src/mkdocstrings_handlers/python/handler.py       112     23     40      7    69.74%
src/mkdocstrings_handlers/python/rendering.py      93     39     42      6    57.78%
tests/conftest.py                                  34      1     10      0    97.73%
tests/test_handler.py                              25      0      6      0   100.00%
tests/test_rendering.py                            18      0      8      0   100.00%
tests/test_themes.py                               10      0      8      0   100.00%
------------------------------------------------------------------------------------
TOTAL

Every version says Unrecognized option '[coverage:report] include_namespace_packages=' :confused:

pawamoy avatar Dec 11 '22 15:12 pawamoy

Something is odd about your installation:

% pip install coverage==7.0.0b1
Collecting coverage==7.0.0b1
  Downloading coverage-7.0.0b1-cp39-cp39-macosx_10_9_x86_64.whl (187 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 188.0/188.0 kB 2.6 MB/s eta 0:00:00
Installing collected packages: coverage
Successfully installed coverage-7.0.0b1

% python -c 'import coverage; print(coverage.__version__)'
7.0.0b1

Since you are getting "6.5.0" reported as the version, you are using 6.5.0, which is why the option isn't recognized. I don't know how the installation can be mixed like that though.

nedbat avatar Dec 11 '22 15:12 nedbat

You must be right, 6.5.0 is probably taking precedence from elsewhere. I'll report back once I fixed my setup.

pawamoy avatar Dec 11 '22 15:12 pawamoy

Yup, pip refused to upgrade to 7.0.0b1 when I asked it to install (for a reason that is irrelevant here). Made it install the right version and it's working as expected, without warnings :slightly_smiling_face: Sorry for the noise, and thank you!

pawamoy avatar Dec 11 '22 15:12 pawamoy