dapper91 / pjrpc

python json-rpc client/server without boilerplate
https://pjrpc.readthedocs.io
The Unlicense
30 stars 3 forks source link

examples/aio_pika_server.py: Use asyncio.new_event_loop() for 3.10+ & mypy fixes #89

Closed bernhardkaindl closed 1 year ago

bernhardkaindl commented 1 year ago

Hello @dapper91!

Note: To be merged before #90, but #90 includes this commit.

This is a trivial improvement to fix mypy warnings (except for add_user(), see #90 for it) in examples/aio_pika_server.py:

Details:

If asyncio.get_event_loop() is called when there is no running event loop, Python 3.10+ warns:

DeprecationWarning: There is no current event loop

https://docs.python.org/3/library/asyncio-eventloop.html#asyncio.get_event_loop

Because this function has rather complex behavior (especially when custom event loop policies are in use), using the get_running_loop() function is preferred to get_event_loop() in coroutines and callbacks. ... Deprecated since version 3.10: Deprecation warning is emitted if there is no running event loop. In future Python releases, this function will be an alias of get_running_loop().

Because examples/aio_pika_server.py uses it to start the event loop, it must change in this way:

-    loop = asyncio.get_event_loop()
+    loop = asyncio.new_event_loop()

Reference: https://stackoverflow.com/questions/71546992/asyncio-alternatives-to-deprecated-run-forever

While at it, also fix a few mypy errors in this example by adding type annotations and use yarl.URL as needed.

codecov-commenter commented 1 year ago

Codecov Report

Merging #89 (9aebce5) into dev (064c5c6) will increase coverage by 0.05%. The diff coverage is 97.63%.

@@            Coverage Diff             @@
##              dev      #89      +/-   ##
==========================================
+ Coverage   79.49%   79.55%   +0.05%     
==========================================
  Files          41       41              
  Lines        2717     2719       +2     
==========================================
+ Hits         2160     2163       +3     
+ Misses        557      556       -1     
Flag Coverage Δ
unittests 79.55% <97.63%> (+0.05%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
pjrpc/client/backend/aio_pika.py 0.00% <0.00%> (ø)
pjrpc/client/backend/kombu.py 0.00% <0.00%> (ø)
pjrpc/__about__.py 100.00% <100.00%> (ø)
pjrpc/client/client.py 97.55% <100.00%> (ø)
pjrpc/client/integrations/pytest.py 91.85% <100.00%> (ø)
pjrpc/common/__init__.py 91.66% <100.00%> (ø)
pjrpc/common/common.py 89.28% <100.00%> (+0.82%) :arrow_up:
pjrpc/common/exceptions.py 98.64% <100.00%> (ø)
pjrpc/common/v20.py 91.22% <100.00%> (ø)
pjrpc/server/dispatcher.py 87.73% <100.00%> (+0.04%) :arrow_up:
... and 5 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

bernhardkaindl commented 1 year ago

Looks good, @dapper91 - may I merge?

Thanks, Bernhard