[TIP] The strange case of missing docstrings coverage

Lele Gaifax lele at metapensiero.it
Fri Jan 13 09:06:43 PST 2023


Hi all,

since some time (half an year or so) I'm seeing something weird that,
albeit very very secondary, puzzles me.

I have a quite "complex" application, composed by several pieces running
in their own Docker container. It is very well tested, with pytest and
coverage.py, so that more than 97% of its 16kLoC gets exercised. Given
that it also comes with a Sphinx technical documentation, most classes,
methods, functions and module-level symbols are carry a docstring.

On exactly one of my development machines, coverage.py reports as
"missing" the lines related to docstrings belonging to *most* of the
symbols (yes, *most*: rarely but some of them, here and there, result
"executed"). All other "instances" (my own laptop, co-workers, CI
host...) thus reports an higher coverage!

To be clear, given the following module foo.py:

  """Foo module documentation.
  """

  SOME_CONSTANT = 42
  "This is the answer to anything."

  class Bar:
      "Something like a pub."

      DEFAULT_DRINK = 'water'
      "What to spill by default."
      
      def spill(self, what=DEFAULT_DRINK):
          "Fill a glass of `what`."

          return SOME_CONSTANT

and a test_foo.py containing:

  import foo
  
  def test_bar():
      bar = foo.Bar()
      assert bar.spill() == foo.SOME_CONSTANT

the coverage.json obtained at the end (produced by `coverage combine;
coverage json`), tells me that the fifth and the eleventh lines, the one
with docstring for SOME_CONSTANT and DEFAULT_DRINK, have not been
executed. Only that one.

I'm currently using pytest 7.2.0 and coverage 7.0.5: I'm not sure when I
started seeing this, but was surely using pytest 6 and coverage 6.4.x at
that time.

Worth of noting is that while all the Docker containers run Python 3.9
(even the tests run in a dedicated container), the "outer" development
environment (where the mentioned `coverage combine` gets executed) may
run a different version.

A couple of days ago I updated the system Python on the machine in
question (a Debian sid, by the way), recreating the development virtual
environment using Python 3.11.1 (it was 3.10.9), but got the same.

As said, this is very marginal and low priority, but I spent some time,
now and then, investigating what could make the difference, without
luck.

Thanks in advance for any hints!

ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele at metapensiero.it  |                 -- Fortunato Depero, 1929.




More information about the testing-in-python mailing list