sanic-org / sanic

Accelerate your web app development | Build fast. Run fast.
https://sanic.dev
MIT License
18k stars 1.54k forks source link

Code coverage warning: "No source for code: '/my/local/folder/_run_response_middleware'. (couldnt-parse)" #2345

Closed NonStatic2014 closed 2 years ago

NonStatic2014 commented 2 years ago

Describe the bug After upgrade sanic from 21.6 to 21.9, coverage, we noticed when run coverage xml, there is an error like below:

No source for code: '/my/local/folder/_run_request_middleware'.

with coverage xml -i there are more warnings:

/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/_run_request_middleware': No source for code: '/my/local/folder/_run_request_middleware'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/_run_response_middleware': No source for code: '/my/local/folder/_run_response_middleware'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/connection_task': No source for code: '/my/local/folder/connection_task'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/handle_exception': No source for code: '/my/local/folder/handle_exception'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/handle_request': No source for code: '/my/local/folder/handle_request'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/http1_request_header': No source for code: '/my/local/folder/http1_request_header'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/http1_response_header': No source for code: '/my/local/folder/http1_response_header'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/read': No source for code: '/my/local/folder/read'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
/my/local/virtualenv/lib/python3.8/site-packages/coverage/report.py:87: CoverageWarning: Couldn't parse '/my/local/folder/send': No source for code: '/my/local/folder/send'. (couldnt-parse)
  coverage._warn(msg, slug="couldnt-parse")
Wrote XML report to coverage.xml

Code snippet

In blueprint.py

my_bp = Blueprint("my_blueprint")

Create app in a different file:

my_app = Sanic(name, load_env=False, log_config=LOG_SETTINGS)
my_app.blueprint(my_bp)
my_app.register_middleware( ..... )

Expected behavior No warning or error while generating code coverage report, like using sanic 21.6.

Environment (please complete the following information):

Additional context

$ pip freeze | grep "sanic\|coverage\|pytest"
coverage==6.2
pytest==6.2.5
pytest-asyncio==0.15.1
sanic==21.9.3
sanic-routing==0.7.2
sanic-testing==0.8.2
ahopkins commented 2 years ago

Why are you running coverage for Sanic?

You will not get coverage for these functions because they are rewritten at startup time based upon your application's code.

NonStatic2014 commented 2 years ago

I'm trying to run coverage on my server code but hit error/warning on sanic code. this is the place actually confused me.

NonStatic2014 commented 2 years ago

blueprint.py and other python code of my server is in the folder /my/local/folder/. I don't expect any coverage number from sanic.

And can you explain a little more about this?

they are rewritten at startup time based upon your application's code.

ahopkins commented 2 years ago

However you are running coverage, it is including the portions of Sanic that are imported into your application. What version of coverage are you using? See this article.

And can you explain a little more about this?

Sure! This is an important feature of Sanic going forward. I am myself working out this whole coverage issue because it is a PITA. We handle it currently by simply ignoring those methods. Not ideal.

What is happening is that when you first load Sanic, it does some self inspection of the types of handlers that you have setup. Currently it is looking for a few specific items: (1) what signals are implemented by the application; and (2) what content_type is each route handler likely to produce. The first item is used to trim out unneeded code from executing, thus making Sanic more efficient. The second is used to determine how to handle error formatting in an appropriate manner.

In the coming year, there likely will be some more of these introspection style optimizations.

ahopkins commented 2 years ago

I am closing this now as the issue is not really related to Sanic. Feel free to continue the conversation.

rbm-radius commented 1 year ago

@ahopkins respectfully I'd like to reopen this--it is a Sanic issue because of the generated code, which causes confusion with other tools looking for persistent modules. No other package I've used out of ~250 in my current app does this.

ahopkins commented 1 year ago

Feel free to create a new issue and reference this. I would ask @rbm-radius if you can provide a use case and a simple repeatable example of how/where this is causing problems.

rbm-radius commented 1 year ago

@ahopkins done! Let me know if more examples are required.