-
Notifications
You must be signed in to change notification settings - Fork 664
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
Running mypy on sdk resources #773 #4360
base: main
Are you sure you want to change the base?
Conversation
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 these changes need to type check sdk resources? We are already checking it AFAICS, per mypy: mypy --install-types --non-interactive --namespace-packages --explicit-package-bases opentelemetry-sdk/src/opentelemetry/sdk/resources
in tox.ini
.
return None | ||
|
||
plugin_handled = False | ||
|
||
error_handler_entry_points = entry_points( | ||
error_handler_entry_points = entry_points( # type: ignore |
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 can be typed, https://github.com/python/importlib_metadata/blob/main/importlib_metadata/__init__.py#L1039C42-L1039C43
Need to check if we are exporting it from our util module
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.
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.
basedpyright: Import "opentelemetry.util._importlib_metadata" could not be resolved. This results in following errors when mypy is executed -
opentelemetry-sdk/src/opentelemetry/sdk/error_handler/init.py:119: error: Type of variable becomes "Any" due to an unfollowed import [no-any-unimported]
opentelemetry-sdk/src/opentelemetry/sdk/error_handler/init.py:119: error: Expression has type "Any" [misc]
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.
@xrmx Please let me know if the above errors can be handled differently.
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.
I mean something like this error_handler_entry_points : Entrypoints = entry_points(
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 PR has more type ignores than actual typing improvements.
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 a tricky one to type hint because of the way https://github.com/open-telemetry/opentelemetry-python/blob/main/opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py is implemented.
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.
@Kludex do you have any suggestions?
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 PR is really only adding type: ignore
, there isn't any benefit to do this.
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.
My suggestion is for the project to fix the mypy setup in this project, or move to pyright.
I'm happy to help, if the maintainers are willing.
If you can help, please create an issue about the mypy setup.
opentelemetry-sdk/src/opentelemetry/sdk/metrics/_internal/exemplar/exemplar_reservoir.py
Outdated
Show resolved
Hide resolved
This does not execute mypy on sdk/src folder. Will add |
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.
It may be nice to enable https://docs.astral.sh/ruff/rules/blanket-type-ignore/#blanket-type-ignore-pgh003 to make it easier to understand on review what's the purpose of the ignores @xrmx
opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py
Outdated
Show resolved
Hide resolved
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.
More honest review done.
@@ -61,6 +61,7 @@ def _handle(self, error: Exception, *args, **kwargs): | |||
|
|||
from abc import ABC, abstractmethod | |||
from logging import getLogger | |||
from typing import Optional |
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.
I'd rather prefer to use from __future__ import annotations
on the top of the imports, and use pipe (|
) annotation.
@@ -69,7 +70,7 @@ def _handle(self, error: Exception, *args, **kwargs): | |||
|
|||
class ErrorHandler(ABC): | |||
@abstractmethod | |||
def _handle(self, error: Exception, *args, **kwargs): | |||
def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore[misc, no-untyped-def] |
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.
The no-untyped-def
can be removed by properly type hint it:
def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore[misc, no-untyped-def] | |
def _handle(self, error: Exception, *args: Any, **kwargs: Any) -> None: |
Why is the misc
there?
@@ -83,7 +84,7 @@ class _DefaultErrorHandler(ErrorHandler): | |||
""" | |||
|
|||
# pylint: disable=useless-return | |||
def _handle(self, error: Exception, *args, **kwargs): | |||
def _handle(self, error: Exception, *args, **kwargs) -> None: # type: ignore[no-untyped-def] |
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.
Same as above.
pass | ||
|
||
# pylint: disable=no-self-use | ||
def __exit__(self, exc_type, exc_value, traceback): | ||
if exc_value is None: | ||
def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: ignore[no-untyped-def] |
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.
Use type hints mentioned here: https://stackoverflow.com/a/58808179
And remove the type ignore
.
def __exit__(self, exc_type, exc_value, traceback): | ||
if exc_value is None: | ||
def __exit__(self, exc_type, exc_value, traceback) -> Optional[bool]: # type: ignore[no-untyped-def] | ||
if exc_value is None: # type: ignore |
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.
If above applied, the ignore is not needed.
if exc_value is None: # type: ignore | |
if exc_value is None: |
return None | ||
|
||
plugin_handled = False | ||
|
||
error_handler_entry_points = entry_points( | ||
error_handler_entry_points = entry_points( # type: ignore[misc] |
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.
What's the misc
error here?
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.
Same question applies to the below.
@@ -129,7 +129,7 @@ def collect(self, point_attributes: Attributes) -> Optional[Exemplar]: | |||
{ | |||
k: v | |||
for k, v in self.__attributes.items() | |||
if k not in point_attributes | |||
if k not in point_attributes # type: ignore[operator] |
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 seems valid. What values Attributes
can assume?
def __init__(self, size: int, **kwargs) -> None: # type: ignore[no-untyped-def] | ||
super().__init__(**kwargs) # type: ignore[misc] |
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.
What's the signature of ExemplarReservoir
?
Description
Addressing running mypy on opentelemetry-sdk iteratively so we don't have to make one big change addressing all mypy issues at once.
Fixes # (issue)
Type of change
Run mypy against SDK resources
How Has This Been Tested?
I executed the following tox commands:
Checklist: