-
Notifications
You must be signed in to change notification settings - Fork 711
Add contextmanager for Context #215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -99,6 +100,17 @@ def __getitem__(self, name: str) -> "object": | |||
def __setitem__(self, name: str, value: "object") -> None: | |||
self.__setattr__(name, value) | |||
|
|||
@contextmanager # type: ignore | |||
def __call__( | |||
self, **kwargs: typing.Dict[str, "object"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are the quotes around object really needed?
Overall it looks good to me. I just have a question that's is not strictly related to this PR, please consider the following code:
It prints:
Is that correct to print |
Good question! The current Removing a slot would be a nice-to-have thing, however it comes with high cost of introducing a contention on the hot path. Regarding the number of slots and memory consumption, we probably should be explicit that Context is similar like TLS (thread local storage), which should be properly planned instead of being used for random stuff. Thoughts? |
I think that's undesired behavior. It will not have an effect in many cases I imagine, but it will definitely be confusing for the use case where None is a possible real value rather than an indicator of an absence of one. |
Sorry, just read the reply. Something you could also do is have a stacked object proxy that recurses upward, and push / pop off the stack. This would enable appropriate values for when something is found / not found, at the cost off lookup. I guess maybe alternatively it would be fine if the caveat was documented somewhere like the docstring. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM as Context(...)
or Context.use(...)
. But I don't think I understand your comment about contention. Why is del self[key]
significantly worse than self[key] = None
?
Poking around, I think we've talked about it before, but if we're willing to drop support for other kinds of backing context we may get a much faster and simpler API by using |
I think with a bit more careful implementation and maybe some API changes, we could at least have our implementation have little to no overhead over ContextVar if ContextVar is used internally. |
We have a potential contention in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After renaming __call__
-> use
, I'll approve. 👌
@@ -99,6 +100,17 @@ def __getitem__(self, name: str) -> "object": | |||
def __setitem__(self, name: str, value: "object") -> None: | |||
self.__setattr__(name, value) | |||
|
|||
@contextmanager # type: ignore | |||
def __call__( | |||
self, **kwargs: typing.Dict[str, "object"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words:
self, **kwargs: typing.Dict[str, "object"] | |
self, **kwargs: typing.Dict[str, object] |
It looks great overall, going in the right direction. |
* feat: add grpc plugin * fix: remove test typo * fix: nyc missing from test * fix: linting and NodeTracer export * chore: rename grpc test file * test: add error code tests * refactor: logger is given to plugin * fix(grpc): using logger before defined * refactor: move private statics to internal file * test: add withSpan OK test * refactor: follow plugin naming pattern * refactor(tests): replace ProxyTracer * refactor(grpc): move helper funcs to utils * refactor(tests): use new memory exporter helper
Resolves #183.
Now it is much simpler to restore context:
You will get: