feat: suppress open client warnings in SPARQLWrapper.queries#127
Merged
Conversation
4833ba4 to
2a1ce8b
Compare
The `SPARQLWrapper.queries` method is a sync wrapper around async code and intended to be a convenience method for users wishing to execute multiple operations concurrently but do not want to write async code themselves. `SPARQLWrapper.queries` internally creates a `SPARQLWrapper` instance with a shared `httpx.AsyncClient` that is then used in an async context manger for calls to `SPARQLWrapper.aquery`. > `SPARQLWrapper.queries` ergo is a managed application of `SPARQLWrapper.aquery` that uses the context manager API of `SPARQLWrapper` internally. This application causes the sparqlx `ClientManager` to emit a warning about an open client though. While this is technically correct and intended behavior, it is superfluos and potentially misleading for `SPARQLWrapper.queries` which actually manages the client through the context manager. It is therefore preferable to suppress client warnings for `SPARQLWrapper.queries`. The change uses a PEP-806-inspired async wrapper around the sync `warnings.catch_warnings` context manager to suppress `Userwarnings` coming from `sparqlx.utils.client_manager` within `SPARQLWrapper.queries`. Note: `warnings.catch_warnings` is not concurrency-safe, see [Concurrent safety of Context Managers](https://docs.python.org/3/library/warnings.html#concurrent-safety-of-context-managers) in the `warnings` docs. Although the implemented change uses a `contextlib.contextmanager` which closures context state and can ergo mitigate concurrency-safety problems, `warnings` filters are global and would actually need explicit locking or protection from a `ContextVar`. In the case at hand, this is of no concern however, since `SPARQLWrapper.queries` controls its own encapsulated event loop. Closes #124.
2a1ce8b to
5d507b1
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
SPARQLWrapper.queriesmethod is a sync wrapper around async code and intended to be a convenience method for users wishing to execute multiple operations concurrently but do not want to write async code themselves.SPARQLWrapper.queriesinternally creates aSPARQLWrapperinstance with a sharedhttpx.AsyncClientthat is then used in an async context manger for calls toSPARQLWrapper.aquery.This application causes the sparqlx
ClientManagerto emit a warning about an open client though. While this is technically correct and intended behavior, it is superfluos and potentially misleading forSPARQLWrapper.querieswhich actually manages the client through the context manager.It is therefore preferable to suppress client warnings for
SPARQLWrapper.queries.The change uses a PEP-806-inspired async wrapper around the sync
warnings.catch_warningscontext manager to suppressUserwarningscoming fromsparqlx.utils.client_managerwithinSPARQLWrapper.queries.Note:
warnings.catch_warningsis not concurrency-safe, see Concurrent safety of ContextManagers in the
warningsdocs. Although the implemented change uses acontextlib.contextmanagerwhich closures context state and can ergo mitigate concurrency-safety problems,warningsfilters are global and would actually need explicit locking or protection from aContextVar. In the case at hand, this is of no concern however, sinceSPARQLWrapper.queriescontrols its own encapsulated event loop.Closes #124.