Skip to content
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

TypeError from opentelemetry.context.contextvars_context in detach #4163

Open
hyoinandout opened this issue Aug 29, 2024 · 5 comments · May be fixed by #4164
Open

TypeError from opentelemetry.context.contextvars_context in detach #4163

hyoinandout opened this issue Aug 29, 2024 · 5 comments · May be fixed by #4164
Labels
bug Something isn't working

Comments

@hyoinandout
Copy link
Contributor

Describe your environment

Python version: 3.12.4
SDK version: 1.25.0
API version: 1.25.0

What happened?

TypeError from opentelemetry.context.contextvars_context in detach.
The function detach expected an instance of Token, but it got None.
Thus, it fails to detach context.

Steps to Reproduce

Pass None to _RUNTIME_CONTEXT.detach

Expected Result

This kind of error should be elaborated, without avoiding mypy

Actual Result

TypeError occurs

Additional context

No response

Would you like to implement a fix?

Yes

@hyoinandout hyoinandout added the bug Something isn't working label Aug 29, 2024
@xrmx
Copy link
Contributor

xrmx commented Aug 30, 2024

Why do you think this should handled instead of delegating to mypy?

@hyoinandout
Copy link
Contributor Author

Hi, afaik, the related line here is ignoring mypy.
In opentelemetry-api/src/opentelemetry/context/contextvars_context.py#50

self._current_context.reset(token)  # type: ignore

Due to this comment, mypy cannot detect TypeError.

If you are suggesting to uncomment the ignore, I'm ok, the draft PR is just my initial proposal.

@xrmx
Copy link
Contributor

xrmx commented Sep 2, 2024

Hi, afaik, the related line here is ignoring mypy. In opentelemetry-api/src/opentelemetry/context/contextvars_context.py#50

self._current_context.reset(token)  # type: ignore

Due to this comment, mypy cannot detect TypeError.

If you are suggesting to uncomment the ignore, I'm ok, the draft PR is just my initial proposal.

Missed that, thanks. Yeah, I think it would make sense to add type annotations here if feasible

@hyoinandout
Copy link
Contributor Author

I added type annotations - could you have a check on the latest PR of this issue?

@rslinckx
Copy link

rslinckx commented Sep 4, 2024

@xrmx I managed to reproduce a problem with a 'None' token using a partially instrumented celery (sender not instrumented, worker instrumented) as seen above. This causes the context.detach to fail. I'm not sure if the fix is simply to to check if the token is not None before calling detach ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants