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

fix: avoid conversion to bool in debug response #2384

Merged
merged 3 commits into from Sep 29, 2023

Conversation

geeshta
Copy link
Sponsor Contributor

@geeshta geeshta commented Sep 28, 2023

Pull Request Checklist

  • New code has 100% test coverage
  • (If applicable) The prose documentation has been updated to reflect the changes introduced by this PR
  • (If applicable) The reference documentation has been updated to reflect the changes introduced by this PR

Description

Avoid implicit conversion to bool in _debug_response.py as this causes problems with some sqlalchemy classes, where instead of the original DB error being propagated, a TypeError is raised. See #2381

Close Issue(s)

Closes #2381

@geeshta geeshta requested review from a team as code owners September 28, 2023 23:35
Copy link
Contributor

@peterschutt peterschutt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest adding a test - this is the type of thing that a tool, or an overzealous refactor might reverse at some point down the track without realizing why it is why it is.

This reproduces it:

from inspect import getinnerframes

from litestar.middleware.exceptions._debug_response import get_symbol_name


def test_get_symbol_name_where_type_doesnt_support_bool() -> None:
    class Test:
        def __bool__(self) -> bool:
            raise TypeError("This type doesn't support bool")

        def method(self) -> None:
            raise RuntimeError("Oh no!")

    try:
        Test().method()
    except Exception as e:
        exc = e

    frame = getinnerframes(exc.__traceback__, 2)[-1]
    assert get_symbol_name(frame) == "Test.method"

litestar/middleware/exceptions/_debug_response.py Outdated Show resolved Hide resolved
@geeshta
Copy link
Sponsor Contributor Author

geeshta commented Sep 29, 2023

I suggest adding a test - this is the type of thing that a tool, or an overzealous refactor might reverse at some point down the track without realizing why it is why it is.

This reproduces it:

from inspect import getinnerframes

from litestar.middleware.exceptions._debug_response import get_symbol_name


def test_get_symbol_name_where_type_doesnt_support_bool() -> None:
    class Test:
        def __bool__(self) -> bool:
            raise TypeError("This type doesn't support bool")

        def method(self) -> None:
            raise RuntimeError("Oh no!")

    try:
        Test().method()
    except Exception as e:
        exc = e

    frame = getinnerframes(exc.__traceback__, 2)[-1]
    assert get_symbol_name(frame) == "Test.method"

I've added the test. I added some type guards because pyright complained about exc being potentially unbound and __traceback__ being potentially None... other solution would by just # type: ignore them.

Copy link
Contributor

@peterschutt peterschutt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

@github-actions
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/2384

@peterschutt
Copy link
Contributor

@all-contributors add @geeshta for code bug

@allcontributors
Copy link
Contributor

@peterschutt

I've put up a pull request to add @geeshta! 🎉

@peterschutt peterschutt merged commit cf131a5 into litestar-org:main Sep 29, 2023
16 checks passed
@geeshta geeshta deleted the fix-debug-response branch September 29, 2023 10:44
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.

Bug: converting objects to a Bool in debug response
2 participants