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

Handle traceback.format_exception() API change in Python 3.10 #2018

Merged

Conversation

musicinmybrain
Copy link
Contributor

@musicinmybrain musicinmybrain commented Aug 5, 2021

The first argument of traceback.format_exception() has been renamed from “etype” to “exc” and is now positional-only. Passing it as a positional argument rather than a keyword argument fixes a TypeError on Python 3.10 while remaining compatible with previous Python releases:

  • On Python 3.10, the parameter should be the exception object; it is now possible to call format_exception() with only the exception object, omitting “value” and “tb”. Here the type of the exception object is given instead; however, this is OK because “value” and “tb” are also given, and therefore the positional “exc” parameter is ignored and may have any value or type whatsoever.
  • From Python 3.5 to 3.9, the first argument is “etype” and is correctly the type of the exception object; however, its actual value is still ignored in favor of the type of “value”.
  • Prior to Python 3.5, the “etype” would actually be used, if this project supported those Python versions.

The changes in the traceback module in Python 3.10 are documented at:
https://docs.python.org/3.10/library/traceback.html#traceback.print_exception
https://docs.python.org/3.10/library/traceback.html#traceback.format_exception
https://docs.python.org/3.10/library/traceback.html#traceback.format_exception_only
https://docs.python.org/3.10/library/traceback.html#traceback.TracebackException.format_exception_only

Description

See the formatted commit message text above.

Fixes # (issue)

No issue has been filed.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

I’m working on updating the python-opentelemetry package on Fedora Linux, which I just started maintaining. I can confirm this fixes failing opentelemetry-opentracing-shim tests on Fedora Rawhide (the development version) with a pre-release version of Python 3.10. The updated package isn’t yet ready, but fortunately you don’t need to use the same environment as I am to replicate this.

Suffice it to say that if you try to run the tests on Python 3.10 in any environment, you should see a TypeError in the opentelemetry-opentracing-shim tests, and you should find that this PR fixes it. Furthermore, you can confirm compatibility with Python 3.9 and older by running the tests however you normally run them. Nothing about this PR is sensitive to the particulars of your environment other than the Python version.

  • Run the tests on Python 3.10 and on existing supported Python versions—with no special requirements for the test environment.

Does This PR Require a Contrib Repo Change?

Answer the following question based on these examples of changes that would require a Contrib Repo Change:

  • The OTel specification has changed which prompted this PR to update the method interfaces of opentelemetry-api/ or opentelemetry-sdk/

  • The method interfaces of opentelemetry-instrumentation/ have changed

  • The method interfaces of test/util have changed

  • Scripts in scripts/ that were copied over to the Contrib repo have changed

  • Configuration files that were copied over to the Contrib repo have changed (when consistency between repositories is applicable) such as in

    • pyproject.toml
    • isort.cfg
    • .flake8
  • When a new .github/CODEOWNER is added

  • Major changes to project information, such as in:

    • README.md
    • CONTRIBUTING.md
  • Yes. - Link to PR:

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added N/A—this PR only fixes existing test code
  • Documentation has been updated N/A—this PR is only an internal change to test code

The first argument of traceback.format_exception() has been renamed from
“etype” to “exc” and is now positional-only. Passing it as a positional
argument rather than a keyword argument fixes a TypeError on Python 3.10
while remaining compatible with previous Python releases:
  • On Python 3.10, the parameter *should* be the exception object; it
    is now possible to call format_exception() with only the exception
    object, omitting “value” and “tb”. Here the type of the exception
    object is given instead; however, this is OK because “value” and
    “tb” are also given, and therefore the positional “exc” parameter is
    ignored and may have any value or type whatsoever.
  • From Python 3.5 to 3.9, the first argument is “etype” and is
    correctly the type of the exception object; however, its actual
    value is still ignored in favor of the type of “value”.
  • Prior to Python 3.5, the “etype” would actually be used, if this
    project supported those Python versions.

The changes in the traceback module in Python 3.10 are documented at:
https://docs.python.org/3.10/library/traceback.html#traceback.print_exception
https://docs.python.org/3.10/library/traceback.html#traceback.format_exception
https://docs.python.org/3.10/library/traceback.html#traceback.format_exception_only
https://docs.python.org/3.10/library/traceback.html#traceback.TracebackException.format_exception_only
@musicinmybrain musicinmybrain requested review from a team, aabmass and ocelotl and removed request for a team August 5, 2021 17:37
@ocelotl
Copy link
Contributor

ocelotl commented Aug 5, 2021

Is this a fix for #1975?

@musicinmybrain
Copy link
Contributor Author

Is this a fix for #1975?

No, I don’t currently have a fix for #1975, which affects the opentelemetry-exporter-jaeger-proto-grpc package.

Instead, this PR fixes an unrelated test failure in opentelemetry-opentracing-shim on Python 3.10. As far as I can tell, that failure has not been reported as an issue.

@srikanthccv
Copy link
Member

Can we add 3.10 to the CI? I am wondering if it is possible since final release 3.10 yet to be done.

@lzchen lzchen merged commit 1048391 into open-telemetry:main Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants