Comments (3)
Agreed ValueError
maybe is not the best exception to raise, RuntimeError
maybe is more adequate
I think explicitly specifying default has the same effect in making it more explicit in the code
Nope, with the returning a default
strategy you can fall into having defaults
all over your code without you noticing. Same for set
operation, where you may think you are setting it but you're not.
If you don't like the try
catch
code you can go with suppress
:
value = default
with contextlib.suppress(ValueError):
value = context.get('key')
I know its not as easy as having a param in the function call, but I really want users of this to think about what they are doing, we suffered this in production because of treating the same way not being properly initialized or not having the key in the context.
Furthermore, the doc still in current sources indicates, default will be returned if there is no loop
Will update, thanks for reporting
from aiotask-context.
I am not sure ValueError communicates that there isn't a loop. The doc on ValueError says this: "Raised when an operation or function receives an argument that has the right type but an inappropriate value"
Furthermore, the doc still in current sources indicates, default will be returned if there is no loop. I think explicitly specifying default has the same effect in making it more explicit in the code... if you think its insufficient, you could add an optional flag require_loop, and default it to True. Then someone could just do this:
context.get(key, default=None, require_loop=False)
and thats pretty explicit and self-documenting as well.
from aiotask-context.
The behavior you mention was an old one (and apparently forgot to update the docs). I did this opinionated change to make it more explicit that there was no loop, otherwise you can run into situations where you think you are retrieving/setting some variable when you aren't.
I know it forces you to use more lines in your code because you have to do:
try:
bar = context.get('foo')
except ValueError:
bar = None
but I prefer this part being explicit
from aiotask-context.
Related Issues (15)
- Support wait_for and others
- Context is global for all the parallel coroutines running HOT 1
- Catch AttributeError when context does not exist HOT 3
- Add RTD integration
- Add CHANGELOG and CONTRIBUTING
- Task factory used in example HOT 1
- Python 3.7: PendingDeprecationWarning: Task.current_task() is deprecated, use asyncio.current_task() instead HOT 5
- Does it have integration with aiohttp using gunicorn? HOT 1
- Please remove dependency to "pytest-runner" HOT 1
- how to use with sanic mutiple worker HOT 1
- Add test for count reference
- TypeError: 'Context' object does not support item assignment
- Tests fail on Python 3.11
- Add tests for uvloop support
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from aiotask-context.