Currently, Context.get(var) returns None when "var in context" is false.
That's surprising and different than var.get(), especially when var has a
default value.
Code:
---
import contextvars
name = contextvars.ContextVar('name', default='victor')
context = contextvars.copy_context()
print(name in context)
print(context.get(name))
print(name.get())
---
Output:
---
False
None
victor
---
Context.get() must raise a lookup error by default if var is not in
context. It should return the default argument if it's set, it's just that
the default parameter must not have a default value (None).
I'l fine that Context.get(default=None) and var.get() behaves differently
(return None vs victor in my example) when var isn't set and var has a
default value.
Victor
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com