DataDog / dd-trace-py

Datadog Python APM Client
https://ddtrace.readthedocs.io/
Other
532 stars 408 forks source link

TypeError: type 'TracedMongoClient' is not subscriptable #10130

Open ehemmerlin opened 1 month ago

ehemmerlin commented 1 month ago

Summary of problem

Our Python application is using type checkers and the library PyMongo. "As of version 4.1, PyMongo ships with type hints. With type hints, Python type checkers can easily find bugs before they reveal themselves in your code." Source: https://pymongo.readthedocs.io/en/stable/examples/type_hints.html

Our Python microservice running on Kubernetes is working, but the following error occurs when adding "admission.datadoghq.com/python-lib.version" : "v2.10.1" to it.

  File "/KA/python_processing/services/__init__.py", line 10, in <module>
    from .resource_service import ResourceService
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "/datadog-lib/ddtrace_pkgs/site-packages-ddtrace-py3.12-manylinux2014/ddtrace/internal/module.py", line 250, in _exec_module
    self.loader.exec_module(module)
  File "/KA/python_processing/services/resource_service.py", line 7, in <module>
    class ResourceService:
  File "/KA/python_processing/services/resource_service.py", line 12, in ResourceService
    def __init__(self, mongo_client: pymongo.MongoClient[Dict[str, Any]]):
                                     ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
TypeError: type 'TracedMongoClient' is not subscriptable

Removing the use of type checkers in our Python microservices solves temporarily the issue, but we need to use type checkers. So we expect dd-trace-py to be compatible with type checkers.

Which version of dd-trace-py are you using?

2.10.1

Which version of pip are you using?

Python 3.12

Which libraries and their versions are you using?

PyMongo version 4.6.3

How can we reproduce your problem?

Use PyMongo version 4.6.3 and type hints in a Python microservice then add Datadog Python library version 2.10.1 to instrument this microservice.

What is the result that you get?

Our Python microservice crashed with this error

  File "/KA/python_processing/services/__init__.py", line 10, in <module>
    from .resource_service import ResourceService
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "/datadog-lib/ddtrace_pkgs/site-packages-ddtrace-py3.12-manylinux2014/ddtrace/internal/module.py", line 250, in _exec_module
    self.loader.exec_module(module)
  File "/KA/python_processing/services/resource_service.py", line 7, in <module>
    class ResourceService:
  File "/KA/python_processing/services/resource_service.py", line 12, in ResourceService
    def __init__(self, mongo_client: pymongo.MongoClient[Dict[str, Any]]):
                                     ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
TypeError: type 'TracedMongoClient' is not subscriptable

What is the result that you expected?

Our Python microservice should run without any error when Datadog Python library version 2.10.1 is added to instrument this microservice.

emmettbutler commented 1 month ago

Thank you for the report, @ehemmerlin. We'll get on it.

cc @mabdinur