Skip to content

3.14 field annotation not covered #1908

Closed

Description

@nickdrozd

The following snippet gets counted as 100% coverage under 3.13 and earlier versions, but under 3.14 it is counted as having a missing line:

class X:
    x: int  # <-- not covered under 3.14
python3.14 -m coverage run asdf.py && python3.14 -m coverage combine --quiet && python3.14 -m coverage report --fail-under 100
Name      Stmts   Miss Branch BrPart  Cover
-------------------------------------------
asdf.py       2      1      0      0    50%
-------------------------------------------
TOTAL         2      1      0      0    50%
Coverage failure: total of 50 is less than fail-under=100

Adding from __future__ import annotations causes everything to be counted:

from __future__ import annotations

class X:
    x: int  # <-- covered

Could have something to do with https://docs.python.org/3.14/whatsnew/3.14.html#whatsnew314-pep649?

Activity

  1. added
    bug
    on Dec 22, 2024
  2. nedbat commented on Dec 22, 2024

    @nedbat
    Member

    This definitely is because of deferred annotations. I'm not sure what to do here. The fact is there is code that has not been run. But also, it is likely never to be run, and it's fine that it isn't run.

    But it can have errors. This will be an error if you run it in 3.13:

    class X:
        x: inttttt
        y = 1
    % python3.13 att.py
    Traceback (most recent call last):
      File "/Users/ned/coverage/trunk/att.py", line 1, in <module>
        class X:
            x: inttttt
            y = 1
      File "/Users/ned/coverage/trunk/att.py", line 2, in X
        x: inttttt
           ^^^^^^^
    NameError: name 'inttttt' is not defined
    

    In 3.14, it won't error, but it will if you try to get the annotation:

    class X:
        x: inttttt
        y = 1
    
    from annotationlib import get_annotations, Format
    get_annotations(X, format=Format.VALUE)
    % python3.14 att.py
    Traceback (most recent call last):
      File "/Users/ned/coverage/trunk/att.py", line 7, in <module>
        get_annotations(X, format=Format.VALUE)
        ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
      File "/usr/local/pyenv/pyenv/versions/3.14.0a3/lib/python3.14/annotationlib.py", line 707, in get_annotations
        ann = _get_dunder_annotations(obj)
      File "/usr/local/pyenv/pyenv/versions/3.14.0a3/lib/python3.14/annotationlib.py", line 842, in _get_dunder_annotations
        ann = _BASE_GET_ANNOTATIONS(obj)
      File "/Users/ned/coverage/trunk/att.py", line 2, in __annotate__
        x: inttttt
           ^^^^^^^
    NameError: name 'inttttt' is not defined
    
  3. added a commit that references this issue on Dec 22, 2024
  4. nickdrozd commented on Dec 22, 2024

    @nickdrozd
    ContributorAuthor

    As a user, my feeling is that if the field annotation is considered covered in 3.13 with from __future__ import annotations, then it should also be considered covered under 3.14 without the import.

    Here is a naive suggestion. I assume there is a line somewhere to the effect of:

    if has_future_annotations_import():
        field_annotation.count_as_covered()

    Maybe that could be changed to:

    if has_future_annotations_import() or PY_VER_314_PLUS:
        field_annotation.count_as_covered()
  5. changed the title [-]3.14 field annoation not covered[/-] [+]3.14 field annotation not covered[/+] on Dec 22, 2024
  6. nedbat commented on Dec 22, 2024

    @nedbat
    Member

    All of the annotations are put into code objects called __annotate__, so I can ignore those when looking for executable lines, and the problem is fixed in commit 2d04835.

  7. nickdrozd commented on Dec 22, 2024

    @nickdrozd
    ContributorAuthor

    Tested; works. Thanks!

  8. nedbat commented on Dec 26, 2024

    @nedbat
    Member

    This is now released as part of coverage 7.6.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugfixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions