Skip to content

Commit

Permalink
Fix mypy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Mar 13, 2023
1 parent 653bd1a commit 20016f3
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 40 deletions.
6 changes: 3 additions & 3 deletions opentelemetry-api/src/opentelemetry/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# pylint: disable=wrong-import-position
from opentelemetry.context.context import Context, _RuntimeContext # noqa
from opentelemetry.environment_variables import OTEL_PYTHON_CONTEXT
from opentelemetry.util._importlib_metadata import entry_points # type: ignore
from opentelemetry.util._importlib_metadata import entry_points

logger = logging.getLogger(__name__)
_RUNTIME_CONTEXT = None # type: typing.Optional[_RuntimeContext]
Expand Down Expand Up @@ -58,8 +58,8 @@ def wrapper( # type: ignore[misc]
try:

_RUNTIME_CONTEXT = next( # type: ignore
iter( # type: ignore
entry_points( # type: ignore
iter(
entry_points(
group="opentelemetry_context",
name=configured_context,
)
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-api/src/opentelemetry/propagate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def example_route():
from opentelemetry.context.context import Context
from opentelemetry.environment_variables import OTEL_PROPAGATORS
from opentelemetry.propagators import composite, textmap
from opentelemetry.util._importlib_metadata import entry_points # type: ignore
from opentelemetry.util._importlib_metadata import entry_points

logger = getLogger(__name__)

Expand Down Expand Up @@ -136,8 +136,8 @@ def inject(

propagators.append( # type: ignore
next( # type: ignore
iter( # type: ignore
entry_points( # type: ignore
iter(
entry_points(
group="opentelemetry_propagator",
name=propagator,
)
Expand Down
125 changes: 94 additions & 31 deletions opentelemetry-api/src/opentelemetry/util/_importlib_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# type: ignore

from sys import version_info
from typing import Dict, Tuple, Union
from typing import Dict, Tuple, Union, overload, Optional, List

# FIXME remove this when support for 3.7 is dropped.
if version_info.minor == 7:
Expand All @@ -28,22 +27,43 @@

# This is a cache to avoid going through creating a dictionary for all
# entry points for 3.7 every time the entry points function is called.
result_entry_points_37 = {}
result_entry_points_37: Dict[str, Tuple[EntryPoint, ...]] = {}

@overload
def entry_points(
group: str = None, name: str = None
) -> Union[Tuple[EntryPoint], Dict[str, Tuple[EntryPoint]]]:
group: None, name: None
) -> Dict[str, Tuple[EntryPoint, ...]]:
...

@overload
def entry_points(group: str, name: None) -> Tuple[EntryPoint, ...]:
...

@overload
def entry_points(group: None, name: str) -> Tuple[EntryPoint, ...]:
...

@overload
def entry_points(group: str, name: str) -> Tuple[EntryPoint, ...]:
...

def entry_points(
group: Optional[str] = None,
name: Optional[str] = None
) -> Union[Tuple[EntryPoint, ...], Dict[str, Tuple[EntryPoint, ...]]]:

if group is None and name is None:

if not result_entry_points_37:

entry_point: EntryPoint

for entry_point in importlib_metadata_entry_points():

if entry_point.group not in result_entry_points_37.keys():
result_entry_points_37[entry_point.group] = []
result_entry_points_37[entry_point.group] = [] # type: ignore

result_entry_points_37[entry_point.group].append(
result_entry_points_37[entry_point.group].append( # type: ignore
entry_point
)

Expand All @@ -53,19 +73,19 @@ def entry_points(
return result_entry_points_37

if group is not None and name is None:
return tuple(
entry_point
return tuple( # type: ignore
entry_point # type: ignore
for entry_point in importlib_metadata_entry_points(group=group)
)

if group is None and name is not None:
return tuple(
entry_point
return tuple( # type: ignore
entry_point # type: ignore
for entry_point in importlib_metadata_entry_points(name=name)
)

return tuple(
entry_point
return tuple( # type: ignore
entry_point # type: ignore
for entry_point in importlib_metadata_entry_points(
group=group, name=name
)
Expand All @@ -74,17 +94,38 @@ def entry_points(
# FIXME remove this file when support for 3.9 is dropped.
elif version_info.minor in (8, 9):
# pylint: disable=import-error
from importlib.metadata import EntryPoint
from importlib.metadata import (
from importlib.metadata import EntryPoint # type: ignore
from importlib.metadata import ( # type: ignore
entry_points as importlib_metadata_entry_points,
)
from importlib.metadata import version

@overload # type: ignore
def entry_points(
group: None, name: None
) -> Dict[str, Tuple[EntryPoint, ...]]:
...

@overload
def entry_points(group: str, name: None) -> Tuple[EntryPoint, ...]:
...

@overload
def entry_points(group: None, name: str) -> Tuple[EntryPoint, ...]:
...

@overload
def entry_points(group: str, name: str) -> Tuple[EntryPoint, ...]:
...

def entry_points(
group: str = None, name: str = None
) -> Union[Tuple[EntryPoint], Dict[str, Tuple[EntryPoint]]]:
group: Optional[str] = None,
name: Optional[str] = None
) -> Union[Tuple[EntryPoint, ...], Dict[str, Tuple[EntryPoint, ...]]]:

result_key_entry_points = importlib_metadata_entry_points()
result_key_entry_points: Dict[str, Tuple[EntryPoint, ...]] = (
importlib_metadata_entry_points() # type: ignore
)

if group is None and name is None:
return result_key_entry_points
Expand All @@ -108,46 +149,68 @@ def entry_points(

name_group_entry_points = []

for result_entry_point in result_key_entry_points[group]:
for result_entry_point in result_key_entry_points[group]: # type: ignore
if result_entry_point.name == name:
name_group_entry_points.append(result_entry_point)
break

return tuple(name_group_entry_points)

else:
from importlib.metadata import EntryPoint
from importlib.metadata import (
from importlib.metadata import EntryPoint # type: ignore
from importlib.metadata import ( # type: ignore
entry_points as importlib_metadata_entry_points,
)
from importlib.metadata import version

@overload # type: ignore
def entry_points(
group: str = None, name: str = None
) -> Union[Tuple, Dict]:
group: None, name: None
) -> Dict[str, Tuple[EntryPoint, ...]]:
...

@overload
def entry_points(group: str, name: None) -> Tuple[EntryPoint, ...]:
...

@overload
def entry_points(group: None, name: str) -> Tuple[EntryPoint, ...]:
...

@overload
def entry_points(group: str, name: str) -> Tuple[EntryPoint, ...]:
...

def entry_points(
group: Optional[str] = None,
name: Optional[str] = None
) -> Union[Tuple[EntryPoint, ...], Dict[str, Tuple[EntryPoint, ...]]]:

if group is None and name is None:
result_entry_points = {}
result_entry_points: Dict[str, Tuple[EntryPoint, ...]] = {}

key: str
value: List[EntryPoint]

for key, value in importlib_metadata_entry_points().items():
for key, value in importlib_metadata_entry_points().items(): # type: ignore
result_entry_points[key] = tuple(value)

return result_entry_points

if group is not None and name is None:
return tuple(
entry_point
return tuple( # type: ignore
entry_point # type: ignore
for entry_point in importlib_metadata_entry_points(group=group)
)

if group is None and name is not None:
return tuple(
entry_point
return tuple( # type: ignore
entry_point # type: ignore
for entry_point in importlib_metadata_entry_points(name=name)
)

return tuple(
entry_point
return tuple( # type: ignore
entry_point # type: ignore
for entry_point in importlib_metadata_entry_points(
group=group, name=name
)
Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-api/src/opentelemetry/util/_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from os import environ
from typing import TYPE_CHECKING, TypeVar, cast

from opentelemetry.util._importlib_metadata import entry_points # type: ignore
from opentelemetry.util._importlib_metadata import entry_points

if TYPE_CHECKING:
from opentelemetry.metrics import MeterProvider
Expand All @@ -41,8 +41,8 @@ def _load_provider(
return cast(
Provider,
next( # type: ignore
iter( # type: ignore
entry_points( # type: ignore
iter(
entry_points(
group=f"opentelemetry_{provider}",
name=provider_name,
)
Expand Down
4 changes: 4 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ deps =
coverage: pytest
coverage: pytest-cov
mypy,mypyinstalled: mypy
; FIXME remove this when support for 3.7 is dropped. This is being added only to make
; mypy run with code from opentelemetry.utils._importlib_metadata which uses the third
; party importlib-metadata package.
mypy,mypyinstalled: importlib-metadata

; proto 3 and 4 tests install the respective version of protobuf
proto3: protobuf~=3.19.0
Expand Down

0 comments on commit 20016f3

Please sign in to comment.