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

regression: typing error with start_as_current_span on Python<3.10 #3836

Closed
PietroPasotti opened this issue Apr 5, 2024 · 15 comments · Fixed by #4389
Closed

regression: typing error with start_as_current_span on Python<3.10 #3836

PietroPasotti opened this issue Apr 5, 2024 · 15 comments · Fixed by #4389
Labels
bug Something isn't working

Comments

@PietroPasotti
Copy link

I suspect #3633 broke type checkers' ability to inspect the signature of start_as_current_span.

Describe your environment
python 3.11
opentelemetry-exporter-otlp-proto-http==1.24.0
pyright==1.1.331

Steps to reproduce
run pyright on code using tracer.start_as_current_span(name) or tracer.start_as_current_span(name=name)

What is the expected behavior?
nothing :)

What is the actual behavior?
error: No parameter named "name" (reportGeneralTypeIssues)
or
error: Expected 0 positional arguments (reportGeneralTypeIssues)

Additional context
Probably introduced by #3633

@PietroPasotti PietroPasotti added the bug Something isn't working label Apr 5, 2024
@xrmx
Copy link
Contributor

xrmx commented Apr 5, 2024

@PietroPasotti thanks for reporting, any idea on how to fix that?

@PietroPasotti
Copy link
Author

Sorry but no, I looked a bit into it but I'm not familiar enough with ParamSpec to be useful. Will take another look if I have time next week.

@pmcollins
Copy link
Member

@PietroPasotti do you get the error when you run pyright against docs/examples/auto-instrumentation/client.py on latest main? I'm getting a different error, but not one for start_as_current_span:

auto-instrumentation % pyright client.py
/Users/x/python/open-telemetry/opentelemetry-python/docs/examples/auto-instrumentation/client.py
  /Users/x/python/open-telemetry/opentelemetry-python/docs/examples/auto-instrumentation/client.py:30:29 - error: Cannot access member "add_span_processor" for type "TracerProvider"
    Member "add_span_processor" is unknown (reportAttributeAccessIssue)
1 error, 0 warnings, 0 informations 

@jenshnielsen
Copy link
Contributor

@PietroPasotti does this happen for you with 3.11. I am seeing a similar issue with 3.9 and older microsoft/Qcodes#5899

I believe the 3.9 issue is caused by typing.ParamSpec not existing on 3.9 which results in the type effectivly just being a string that would never type check with 3.9. Normally I would suggest that opentelemetry should depend on typing_extensions and use the backported version for older python versions but I am not sure if that is against some policy?

For 3.11 I guess it could be an issue specific to an older patch version. Which exact version are you running? I think there may have been some fixes to paramspec in the patch versions. Note also that the version of pyright that you are using is fairly old. Latest version is 1.1.358. Could you try with a newer one?

@PietroPasotti
Copy link
Author

tried with pyright 1.1.358, the error is different but on the same line.
Now it gives:

error: Expected 0 positional arguments (reportCallIssue)

on tracer.start_as_current_span(name)

@PietroPasotti
Copy link
Author

PietroPasotti commented Apr 15, 2024

Modifying the source to import ParamSpec from typing_extensions fixes it.
that's obviously not a solution for the issue, but it might be useful debugging info :)

@jenshnielsen
Copy link
Contributor

@PietroPasotti Which exact version of python 3.11 are you using. Looks like there were bugfixes to Paramspec in 3.11.1 and 3.11.3
which may be the reason

https://docs.python.org/release/3.11.9/whatsnew/changelog.html#python-3-11-9

@PietroPasotti
Copy link
Author

3.11.8

@dimaqq
Copy link

dimaqq commented Jan 16, 2025

Edit: I was wrong, this is not dependent on Python interpreter version, rather on Python version setting in pyright.

@dimaqq
Copy link

dimaqq commented Jan 17, 2025

Minimum Reproducer

# example.py
import opentelemetry.trace
tracer = opentelemetry.trace.get_tracer(__name__)
with tracer.start_as_current_span("some label here"): ...
# pyproject.toml
[tool.pyright]
pythonVersion = "3.9"
🦐/c/otel-bug> uvx --with opentelemetry-api==1.29.0 --with pyright pyright example.py
/code/otel-bug/example.py
  /code/otel-bug/example.py:3:35 - error: Expected 0 positional arguments (reportCallIssue)
1 error, 0 warnings, 0 informations

Varying pythonVersion:

  • 3.0 ✅
  • 3.1 ✅
  • 3.2 ✅
  • 3.3 ✅
  • 3.4 ✅
  • 3.5 ❌
  • 3.6 ❌
  • 3.7 ❌
  • 3.8 ❌
  • 3.9 ❌
  • 3.10 ✅
  • 3.11 ✅
  • 3.12 ✅
  • 3.13 ✅
  • 3.14 ✅

Varying opentelemetry-api version:

  • 1.20.0 ✅
  • 1.21.0 ✅
  • 1.22.0 ✅
  • 1.23.0 ✅
  • 1.24.0 ❌
  • 1.25.0 ❌
  • 1.26.0 ❌
  • 1.27.0 ❌
  • 1.28.0 ❌
  • 1.28.1 ❌
  • 1.28.2 ❌
  • 1.29.0 ❌

@dimaqq
Copy link

dimaqq commented Jan 17, 2025

@PietroPasotti or maintainers, would you consider editing the bug headline to include something like "regression on Python<3.10" ?

@PietroPasotti PietroPasotti changed the title typing issue with start_as_current_span regression: typing error with start_as_current_span on Python<3.10 Jan 17, 2025
@aabmass
Copy link
Member

aabmass commented Jan 17, 2025

Thanks for testing all those versions @dimaqq. It's interesting that pre 3.5 works 😆

I have a PR that does

if TYPE_CHECKING:
  from typing_extensions import ParamSpec

...

I'm trying to avoid adding typing-extensions as a hard dependency to the API package for users not using typing at all. Users type checking against this lib wil need to install typing-extensions on their own (which you probably already have since MyPy and pyright depend on it)

Any concerns?

@dimaqq
Copy link

dimaqq commented Jan 18, 2025

I think that’s a great way out!

@dimaqq
Copy link

dimaqq commented Jan 28, 2025

P.S. I'm hoping for a new release that incorporates this change.
When I'm done the feature I'm working on, it will be:

  • either wait for new release
  • or sprinkle the codebase with 123 # type: ignore comments
  • or go back to 1.23.0

@dimaqq
Copy link

dimaqq commented Feb 12, 2025

New release has been published, opentelemetry-api~=1.30 and we're good to go!

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
6 participants