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

Missed lines with asyncio & context managers #441

Closed
nedbat opened this issue Oct 27, 2015 · 11 comments
Closed

Missed lines with asyncio & context managers #441

nedbat opened this issue Oct 27, 2015 · 11 comments
Labels
bug Something isn't working

Comments

@nedbat
Copy link
Owner

nedbat commented Oct 27, 2015

Originally reported by Hynek Schlawack (Bitbucket: hynek, GitHub: hynek)


I have a rather odd problem I cannot reproduce in clean room.

I have a rather simple class and method:

https://gist.github.com/hynek/3ad620d07a7999f4daf5

Line 21 – or any line before the except – is not reported as covered. Even if it’s a print statement and see its output.

It seems to be related with the context manager because it's a) always the last line and b) when I rewrite it as https://gist.github.com/hynek/cfe2cc32f337e2676466 the coverage works just fine.

I’m sorry I can’t be more helpful but I’m confused as hell myself. The pool is from aiopg and seems to be semi-magical: https://github.com/aio-libs/aiopg/blob/master/aiopg/pool.py#L302 so dunno if that helps…


@nedbat
Copy link
Owner Author

nedbat commented Jan 3, 2016

@hynek any chance you've come up with more information about this? Can you provide explicit instructions for reproducing it, even if it's a particular hash in your repo with details of how to run the tests?

@nedbat
Copy link
Owner Author

nedbat commented Jan 3, 2016

Original comment by Hynek Schlawack (Bitbucket: hynek, GitHub: hynek)


I was able to strip it down reasonably: https://ox.cx/stuff/sscce.tbz2

Preparations

You need a postgres DB that you have to configure in test.yml.

This DB needs a single table:

CREATE TABLE "public"."tokens" (
	"token" text NOT NULL COLLATE "default",
	"system" text NOT NULL COLLATE "default",
	"purpose" text NOT NULL COLLATE "default",
	"user_id" int4 NOT NULL,
	"created" timestamp(6) WITH TIME ZONE DEFAULT timezone('UTC'::text, now()),
	"valid" bool NOT NULL DEFAULT true
)
WITH (OIDS=FALSE);
ALTER TABLE "public"."tokens" ADD PRIMARY KEY ("token") NOT DEFERRABLE INITIALLY IMMEDIATE;

Reproduction

  • create a Python 3.5 venv
  • pip install -r dev-requirements.txt
  • run make

You should be seeing src/tokens/aio.py 25 0 6 1 97% 34->35.

If you add a XXX in that line though, the test will explode.


Let me know if you need something.

@nedbat
Copy link
Owner Author

nedbat commented Jan 11, 2016

I haven't had a chance to reproduce this yet, but it sure looks like a duplicate of #146. Would you mind trying 4.1b1 to see if it is fixed? https://pypi.python.org/pypi/coverage/4.1b1

@nedbat
Copy link
Owner Author

nedbat commented Jan 11, 2016

Original comment by Hynek Schlawack (Bitbucket: hynek, GitHub: hynek)


Sadly, 4.1b1 seems worse.

I’ve updated, did an coverage erase, and run coverage run -m pytest.

I’m getting the following output:

Name                     Stmts   Miss Branch BrPart  Cover   Missing
--------------------------------------------------------------------
src/tokens/__init__.py       3      0      0      0   100%
src/tokens/_common.py       14      0      2      0   100%
src/tokens/aio.py           26      0     14      3    92%   17->-5, 34->-23, 36->-23
src/tokens/rnd.py           20      0      4      0   100%
--------------------------------------------------------------------
TOTAL                       63      0     20      3    96%
  • line 17 is the docstring of a class definition,
  • line 34 is the context manager as before,
  • and 36 is an except clause.

In htmlcov:

2016-01-11 at 07.52.png

@nedbat
Copy link
Owner Author

nedbat commented Jan 11, 2016

Hmm, the class docstring is confusing me: I had this happen in code of mine, and the first change I made after b1 was to change it again. About the await's: it looks like I need to understand them better. I'll try your code.

@nedbat
Copy link
Owner Author

nedbat commented Jan 13, 2016

I'm understanding await and yield from better: looks like I shouldn't consider them exits from the function. Discussion here if you want to chime in: https://groups.google.com/d/topic/python-tulip/sdM8KKz3XWk/discussion

@nedbat
Copy link
Owner Author

nedbat commented Jan 13, 2016

Original comment by Hynek Schlawack (Bitbucket: hynek, GitHub: hynek)


I’m on the ML and I saw the thread. Sadly my understanding of asyncio is still rather superficial ATM so I don’t have much to add. :(

@nedbat
Copy link
Owner Author

nedbat commented Jan 19, 2016

This will be fixed in 4.1b2.

@nedbat
Copy link
Owner Author

nedbat commented Jan 19, 2016

Original comment by Hynek Schlawack (Bitbucket: hynek, GitHub: hynek)


I can confirm that the current default branch fixes all asyncio problems I was aware of! Thanks Ned!

@nedbat
Copy link
Owner Author

nedbat commented Feb 24, 2016

Original comment by Hynek Schlawack (Bitbucket: hynek, GitHub: hynek)


OK I’m afraid I’ve found another similar case:

try:
    foo
except Something:
   raise  # <-- can't  get this covered

If you wonder what that construct is good for: I want to whitelist certain exceptions that can pass unaltered but wrap all others.

Should I reopen or open a new issue?

@nedbat
Copy link
Owner Author

nedbat commented Feb 24, 2016

A new issue would be best, thanks.

@nedbat nedbat closed this as completed Feb 24, 2016
@nedbat nedbat added major bug Something isn't working labels Jun 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant