Add check before iterating over dist.requires at load_instrumentor (#3168)

* Add check before iterate over dist.requires

* Changelog

* Add test
This commit is contained in:
Tammy Baylis 2025-01-08 01:02:55 -08:00 committed by GitHub
parent 3d5935f4f6
commit 147e3f754e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 6 deletions

View File

@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-httpx` Fix `RequestInfo`/`ResponseInfo` type hints
([#3105](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3105))
- `opentelemetry-instrumentation` Fix `get_dist_dependency_conflicts` if no distribution requires
([#3168](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3168))
## Version 1.29.0/0.50b0 (2024-12-11)

View File

@ -47,13 +47,14 @@ def get_dist_dependency_conflicts(
extra = "extra"
instruments = "instruments"
instruments_marker = {extra: instruments}
for dep in dist.requires:
if extra not in dep or instruments not in dep:
continue
if dist.requires:
for dep in dist.requires:
if extra not in dep or instruments not in dep:
continue
req = Requirement(dep)
if req.marker.evaluate(instruments_marker):
instrumentation_deps.append(req)
req = Requirement(dep)
if req.marker.evaluate(instruments_marker):
instrumentation_deps.append(req)
return get_dependency_conflicts(instrumentation_deps)

View File

@ -86,3 +86,19 @@ class TestDependencyConflicts(TestBase):
str(conflict),
'DependencyConflict: requested: "test-pkg~=1.0; extra == "instruments"" but found: "None"',
)
def test_get_dist_dependency_conflicts_requires_none(self):
class MockDistribution(Distribution):
def locate_file(self, path):
pass
def read_text(self, filename):
pass
@property
def requires(self):
return None
dist = MockDistribution()
conflict = get_dist_dependency_conflicts(dist)
self.assertTrue(conflict is None)