-
Notifications
You must be signed in to change notification settings - Fork 624
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
Improve type hints in opentelemetry.instrumentation.utils
#3128
Open
Kludex
wants to merge
2
commits into
open-telemetry:main
Choose a base branch
from
Kludex:kludex/fix-type-hints-on-opentelemetry-instrumentation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,13 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import annotations | ||
|
||
import urllib.parse | ||
from contextlib import contextmanager | ||
from importlib import import_module | ||
from re import escape, sub | ||
from typing import Dict, Iterable, Sequence, Union | ||
from typing import Any, Dict, Generator, Sequence | ||
|
||
from wrapt import ObjectProxy | ||
|
||
|
@@ -44,9 +46,9 @@ | |
|
||
|
||
def extract_attributes_from_object( | ||
obj: any, attributes: Sequence[str], existing: Dict[str, str] = None | ||
obj: Any, attributes: Sequence[str], existing: Dict[str, str] | None = None | ||
) -> Dict[str, str]: | ||
extracted = {} | ||
extracted: dict[str, str] = {} | ||
if existing: | ||
extracted.update(existing) | ||
for attr in attributes: | ||
|
@@ -81,7 +83,7 @@ def http_status_to_status_code( | |
return StatusCode.ERROR | ||
|
||
|
||
def unwrap(obj: Union[object, str], attr: str): | ||
def unwrap(obj: object, attr: str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a type of redundancy here, since But on the first line of this function, the type checker is able to infer if it's a |
||
"""Given a function that was wrapped by wrapt.wrap_function_wrapper, unwrap it | ||
|
||
The object containing the function to unwrap may be passed as dotted module path string. | ||
|
@@ -152,7 +154,7 @@ def _start_internal_or_server_span( | |
return span, token | ||
|
||
|
||
def _url_quote(s) -> str: # pylint: disable=invalid-name | ||
def _url_quote(s: Any) -> str: # pylint: disable=invalid-name | ||
if not isinstance(s, (str, bytes)): | ||
return s | ||
quoted = urllib.parse.quote(s) | ||
|
@@ -163,13 +165,13 @@ def _url_quote(s) -> str: # pylint: disable=invalid-name | |
return quoted.replace("%", "%%") | ||
|
||
|
||
def _get_opentelemetry_values() -> dict: | ||
def _get_opentelemetry_values() -> dict[str, Any]: | ||
""" | ||
Return the OpenTelemetry Trace and Span IDs if Span ID is set in the | ||
OpenTelemetry execution context. | ||
""" | ||
# Insert the W3C TraceContext generated | ||
_headers = {} | ||
_headers: dict[str, Any] = {} | ||
propagator.inject(_headers) | ||
return _headers | ||
|
||
|
@@ -196,7 +198,7 @@ def is_http_instrumentation_enabled() -> bool: | |
|
||
|
||
@contextmanager | ||
def _suppress_instrumentation(*keys: str) -> Iterable[None]: | ||
def _suppress_instrumentation(*keys: str) -> Generator[None]: | ||
"""Suppress instrumentation within the context.""" | ||
ctx = context.get_current() | ||
for key in keys: | ||
|
@@ -209,7 +211,7 @@ def _suppress_instrumentation(*keys: str) -> Iterable[None]: | |
|
||
|
||
@contextmanager | ||
def suppress_instrumentation() -> Iterable[None]: | ||
def suppress_instrumentation() -> Generator[None]: | ||
"""Suppress instrumentation within the context.""" | ||
with _suppress_instrumentation( | ||
_SUPPRESS_INSTRUMENTATION_KEY, _SUPPRESS_INSTRUMENTATION_KEY_PLAIN | ||
|
@@ -218,7 +220,7 @@ def suppress_instrumentation() -> Iterable[None]: | |
|
||
|
||
@contextmanager | ||
def suppress_http_instrumentation() -> Iterable[None]: | ||
def suppress_http_instrumentation() -> Generator[None]: | ||
"""Suppress instrumentation within the context.""" | ||
with _suppress_instrumentation(_SUPPRESS_HTTP_INSTRUMENTATION_KEY): | ||
yield |
Oops, something went wrong.
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.
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.
This is the most important change.
any
cannot be used asAny
.