This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: test_asyncio fails when run under -O
Type: Stage: resolved
Components: asyncio Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: asvetlov, brett.cannon, gvanrossum, kumaraditya, miss-islington, serhiy.storchaka, yselivanov
Priority: normal Keywords: easy, patch

Created on 2015-03-31 13:31 by brett.cannon, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 29894 merged kumaraditya, 2021-12-02 14:34
PR 30195 merged kumaraditya, 2021-12-19 08:42
PR 30212 merged serhiy.storchaka, 2021-12-20 09:09
PR 30213 merged miss-islington, 2021-12-20 10:23
PR 30265 merged miss-islington, 2021-12-26 11:13
Messages (12)
msg239704 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2015-03-31 13:31
Ton of failures along the lines of:

======================================================================
ERROR: test_ctor (test.test_asyncio.test_unix_events.UnixReadPipeTransportTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/bcannon/Repositories/cpython/default/Lib/test/test_asyncio/test_unix_events.py", line 329, in setUp
    self.loop = self.new_test_loop()
  File "/Users/bcannon/Repositories/cpython/default/Lib/asyncio/test_utils.py", line 413, in new_test_loop
    self.set_event_loop(loop)
  File "/Users/bcannon/Repositories/cpython/default/Lib/asyncio/test_utils.py", line 407, in set_event_loop
    events.set_event_loop(None)
  File "/Users/bcannon/Repositories/cpython/default/Lib/asyncio/events.py", line 581, in set_event_loop
    get_event_loop_policy().set_event_loop(loop)
AttributeError: 'object' object has no attribute 'set_event_loop'
msg239706 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2015-03-31 13:36
-O disables assertions, and asyncio has a ton of assertions. Some tests maybe rely on them? I guess that they are real bugs (assert must be replaced with a regular if+raise) in the middle of the failures.
msg355043 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-10-21 10:27
This issue is still relevant 4 years later: many test_asyncio tests fail with python3 -O :-(
msg407179 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-11-27 23:58
I'm sure there's more to it, but I found at least this failure:

~/cpython$ ./python.exe -O -m test test_asyncio -m test_set_event_loop
Raised RLIMIT_NOFILE: 256 -> 1024
0:00:00 load avg: 2.02 Run tests sequentially
0:00:00 load avg: 2.02 [1/1] test_asyncio
test test_asyncio failed -- Traceback (most recent call last):
  File "/Users/guido/cpython/Lib/test/test_asyncio/test_events.py", line 2595, in test_set_event_loop
    self.assertRaises(AssertionError, policy.set_event_loop, object())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AssertionError: AssertionError not raised by set_event_loop

test_asyncio failed (1 failure)

== Tests result: FAILURE ==

1 test failed:
    test_asyncio

Total duration: 134 ms
Tests result: FAILURE


Also this one:

~/cpython$ ./python.exe -O -m test test_asyncio -m test_create_datagram_endpoint_addr_error
0:00:00 load avg: 2.19 Run tests sequentially
0:00:00 load avg: 2.19 [1/1] test_asyncio
test test_asyncio failed -- Traceback (most recent call last):
  File "/Users/guido/cpython/Lib/test/test_asyncio/test_base_events.py", line 1593, in test_create_datagram_endpoint_addr_error
    self.assertRaises(
    ^^^^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/unittest/case.py", line 734, in assertRaises
    return context.handle('assertRaises', args, kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/unittest/case.py", line 218, in handle
    callable_obj(*args, **kwargs)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/asyncio/base_events.py", line 637, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/asyncio/base_events.py", line 1287, in create_datagram_endpoint
    infos = await self._ensure_resolved(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/guido/cpython/Lib/asyncio/base_events.py", line 1369, in _ensure_resolved
    info = _ipaddr_info(host, port, family, type, proto, *address[2:])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: _ipaddr_info() takes from 5 to 7 positional arguments but 12 were given

test_asyncio failed (1 error)

== Tests result: FAILURE ==

1 test failed:
    test_asyncio

Total duration: 165 ms
Tests result: FAILURE


I think calling assertRaises(AssertionError, ...) is definitely an anti-pattern. We can't remove all assertions from the asyncio library, but I think the ones that are explicitly checked for by the tests should go, at least. (Probably replaced with TypeError or ValueError in most cases.)
msg407309 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-11-29 18:27
I'd be happy to mentor someone who wants to tackle this.
msg407355 - (view) Author: Kumar Aditya (kumaraditya) * (Python triager) Date: 2021-11-30 09:25
I would like to tackle this, I'll work on this from next week.
msg407879 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2021-12-06 23:40
New changeset 265918bb1d782ab85c7dbc835eb62d6cfc2145b7 by Kumar Aditya in branch 'main':
bpo-23819: asyncio: Replace AssertionError with TypeError where it makes sense (GH-29894)
https://github.com/python/cpython/commit/265918bb1d782ab85c7dbc835eb62d6cfc2145b7
msg408950 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-20 10:23
New changeset 6ca78affc8023bc5023189d64d8050857662042a by Serhiy Storchaka in branch 'main':
bpo-23819: Get rid of assert statements in test_asyncio (GH-30212)
https://github.com/python/cpython/commit/6ca78affc8023bc5023189d64d8050857662042a
msg408957 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-20 12:51
New changeset 95948169d75bed3936284ea2225e83e07ec5fe20 by Miss Islington (bot) in branch '3.10':
bpo-23819: Get rid of assert statements in test_asyncio (GH-30212) (GH-30213)
https://github.com/python/cpython/commit/95948169d75bed3936284ea2225e83e07ec5fe20
msg409189 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-26 11:13
New changeset a23ab7b6d8b3ae3a47747c0c4bceb2370cc48dcc by Kumar Aditya in branch 'main':
bpo-23819: Fix asyncio tests on python optimized mode (GH-30195)
https://github.com/python/cpython/commit/a23ab7b6d8b3ae3a47747c0c4bceb2370cc48dcc
msg409194 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-12-26 11:54
New changeset 07229054a129a72b4ffdf29252eb73c6154c0ccf by Miss Islington (bot) in branch '3.9':
[3.9] bpo-23819: Fix asyncio tests on python optimized mode (GH-30195) (GH-30265)
https://github.com/python/cpython/commit/07229054a129a72b4ffdf29252eb73c6154c0ccf
msg410135 - (view) Author: Andrew Svetlov (asvetlov) * (Python committer) Date: 2022-01-09 07:42
Is anything left to do?
History
Date User Action Args
2022-04-11 14:58:14adminsetgithub: 68007
2022-01-09 07:42:51asvetlovsetstatus: open -> closed
resolution: fixed
messages: + msg410135

stage: patch review -> resolved
2021-12-26 11:54:22serhiy.storchakasetmessages: + msg409194
2021-12-26 11:13:24miss-islingtonsetpull_requests: + pull_request28482
2021-12-26 11:13:17serhiy.storchakasetmessages: + msg409189
2021-12-20 12:51:22serhiy.storchakasetmessages: + msg408957
2021-12-20 10:23:39serhiy.storchakasetmessages: + msg408950
2021-12-20 10:23:14miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request28434
2021-12-20 09:09:14serhiy.storchakasetnosy: + serhiy.storchaka
pull_requests: + pull_request28433
2021-12-19 08:42:45kumaradityasetpull_requests: + pull_request28416
2021-12-06 23:40:40asvetlovsetnosy: + asvetlov
messages: + msg407879
2021-12-02 14:34:53kumaradityasetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request28118
2021-11-30 09:25:16kumaradityasetnosy: + kumaraditya
messages: + msg407355
2021-11-29 18:27:02gvanrossumsetkeywords: + easy

messages: + msg407309
2021-11-29 09:25:55vstinnersetnosy: - vstinner
2021-11-27 23:58:01gvanrossumsetmessages: + msg407179
2021-11-27 11:50:22iritkatrielsetversions: + Python 3.11, - Python 3.5
2019-10-21 10:27:16vstinnersetmessages: + msg355043
2015-03-31 13:36:58vstinnersetmessages: + msg239706
2015-03-31 13:31:35brett.cannoncreate