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

Context managers confuse branch coverage #146

Closed
nedbat opened this issue Sep 7, 2011 · 16 comments
Closed

Context managers confuse branch coverage #146

nedbat opened this issue Sep 7, 2011 · 16 comments
Labels
branch bug Something isn't working

Comments

@nedbat
Copy link
Owner

nedbat commented Sep 7, 2011

In this code:

from contextlib import contextmanager

@contextmanager
def dummy():
    yield

for i in range(10):
    with dummy():
        print "hello"  # line 9
    print "bye"
print "done"   # line 11

Line 9 claims to have an untaken branch to line 11.


@nedbat
Copy link
Owner Author

nedbat commented Sep 12, 2012

Original comment by Wichert Akkerman (Bitbucket: wichert, GitHub: wichert)


FWIW: I just ran into this as well using Python 2.7.3 and mock 1.0b1.

@nedbat
Copy link
Owner Author

nedbat commented Sep 27, 2012

Original comment by Laurens Van Houtven (Bitbucket: lvh, GitHub: lvh)


I ran in to this today as well, by opening files using the with statement inside a for loop.

@nedbat
Copy link
Owner Author

nedbat commented Sep 27, 2012

Another report of this from lvh:

class T(unittest.TestCase):
    def setUp(self):
        # SNIP SNIP
        for fileName in ["index.html", "style.css", "app.js"]:
            with location.child(fileName).open("w") as f:
                f.write("base {ext}".format(ext=fileName.split(".")[-1])) # NO JUMP TO NEXT LINE

        f = frontend.FrontEnd(store=s, build=b, domain="frontend.test")

@nedbat
Copy link
Owner Author

nedbat commented Jul 9, 2013

Issue #248 was marked as a duplicate of this issue.

@nedbat
Copy link
Owner Author

nedbat commented Feb 19, 2014

Original comment by stefano_palazzo (Bitbucket: stefano_palazzo, GitHub: Unknown)


Here's an example that probably describes the same issue:

#!python

class Context:

    def __init__(self, do_raise):
        self.do_raise = do_raise

    def __enter__(self):
        if self.do_raise:
            raise FileNotFoundError()
        return self

    def __exit__(self, *exc_info):
        pass


def load(do_raise):
    try:
        with Context(do_raise) as f:
            print(f)
    except FileNotFoundError:
        pass


load(True)
load(False)

With "print(f)" claiming to have an untaken branch to "except FileNotFoundError:".

@nedbat
Copy link
Owner Author

nedbat commented Jul 7, 2014

Original comment by Anthony Sottile (Bitbucket: asottile, GitHub: asottile)


Interestingly the following example seems to only plague 2.6 (not 2.7, 3.3, 3.4, pypy from my testing)

from __future__ import print_function
from __future__ import unicode_literals

import contextlib


@contextlib.contextmanager
def noop():
    yield


def test_foo():
    with noop():
        with noop():
            print('1')
        print('2')

@nedbat
Copy link
Owner Author

nedbat commented Jul 18, 2014

Issue #319 was marked as a duplicate of this issue.

@nedbat
Copy link
Owner Author

nedbat commented Mar 22, 2015

Issue #362 was marked as a duplicate of this issue.

@nedbat
Copy link
Owner Author

nedbat commented Apr 24, 2015

Original comment by Jessamyn Smith (Bitbucket: jessamynsmith, GitHub: jessamynsmith)


As Ned just taught me, if this is plaguing you, you can add the following to the erroneously not-covered line:

pragma: no branch

@nedbat
Copy link
Owner Author

nedbat commented Aug 5, 2015

Original comment by Buck Evan (Bitbucket: bukzor, GitHub: bukzor)


@asottile I've just run into this issue on python 2.7.6.

#!python

class Trivial:
    def __enter__(self):
        print('enter!')
    def __exit__(self, type, value, tb):
        print('exit!')

def demo():
    try:
        with Trivial():
            print('ok')  # this line flagged as missed branch
    except:
        pass

@nedbat
Copy link
Owner Author

nedbat commented Sep 28, 2015

Original comment by Dan Jones (Bitbucket: dannosaur, GitHub: dannosaur)


Ran into this as well on Python 2.7.10 (coverage 3.7.1);

#!python

class Something:
    def something_else(self):
        try:
            with transaction.atomic():
                # ...
                print "x"
            print "y"
        except Exception:
           print "z"

"print x" has a missed branch to "except Exception:".

@nedbat
Copy link
Owner Author

nedbat commented Jan 2, 2016

This will be fixed when the ast-branch is merged.

@nedbat
Copy link
Owner Author

nedbat commented Jan 3, 2016

Issue #384 was marked as a duplicate of this issue.

@nedbat
Copy link
Owner Author

nedbat commented Jan 8, 2016

This is fixed in 44719bd (bb), which will be 4.1

@nedbat nedbat closed this as completed Jan 8, 2016
@nedbat
Copy link
Owner Author

nedbat commented Jan 24, 2016

Issue #470 was marked as a duplicate of this issue.

@nedbat
Copy link
Owner Author

nedbat commented Feb 2, 2016

Issue #473 was marked as a duplicate of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant