open-telemetry / opentelemetry-python

OpenTelemetry Python API and SDK
https://opentelemetry.io
Apache License 2.0
1.76k stars 617 forks source link

semconv: Usage of `Final` annotation for `Enum` attributes is an error #4083

Closed yangskyboxlabs closed 2 months ago

yangskyboxlabs commented 2 months ago

Describe your environment

opentelemetry-semantic-conventions==0.47b0

What happened?

https://github.com/open-telemetry/opentelemetry-python/pull/3966 added many Final annotations to semantic convention literals.

However, usage of any annotation for Enum member attributes is considered invalid, and a bare Final not being flagged appear to be an edge case that was overlooked in at least pyright: https://github.com/microsoft/pyright/issues/8543#issuecomment-2251570823

This leads to misleading type checker errors.

Steps to Reproduce

Consider:

from opentelemetry.semconv.attributes.otel_attributes import OtelStatusCodeValues

def get_ok() -> OtelStatusCodeValues:
    return OtelStatusCodeValues.OK

Expected Result

No type violations in get_ok().

Actual Result

Expression of type "Literal['OK']" is incompatible with return type "OtelStatusCodeValues"
  "Literal['OK']" is incompatible with "OtelStatusCodeValues"Pylance[reportReturnType](https://github.com/microsoft/pyright/blob/main/docs/configuration.md#reportReturnType)

Additional context

No response

Would you like to implement a fix?

None

emdneto commented 2 months ago

Thanks for reporting it. The Final annotation was previously adopted to detect duplication on constants definitions. Maybe no need to use it in Enums.