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

B904 from None check gets confused when class defined in except has a raise #190

Closed
davidism opened this issue Oct 5, 2021 · 3 comments · Fixed by #191
Closed

B904 from None check gets confused when class defined in except has a raise #190

davidism opened this issue Oct 5, 2021 · 3 comments · Fixed by #191

Comments

@davidism
Copy link
Contributor

davidism commented Oct 5, 2021

In Werkzeug, we want to handle the case where Python was not compiled with SSL, so we try import ssl and define a placeholder class in except if the import fails. The class will raise for any attribute access. B904 gets confused and thinks it should be written raise ... from None even though that doesn't make sense here.

try:
    import ssl
except ImportError:

    class _SslDummy:
        def __getattr__(self, name: str) -> t.Any:
            raise RuntimeError("SSL support unavailable")  # noqa: B904

    ssl = _SslDummy()

Note that I had to add # noqa: B904 to the raise line.

@cooperlees
Copy link
Collaborator

In Werkzeug, we want to handle the case where Python was not compiled with SSL, so we try import ssl and define a placeholder class in except if the import fails. The class will raise for any attribute access. B904 gets confused and thinks it should be written raise ... from None even though that doesn't make sense here.

try:
    import ssl
except ImportError:

    class _SslDummy:
        def __getattr__(self, name: str) -> t.Any:
            raise RuntimeError("SSL support unavailable")  # noqa: B904

    ssl = _SslDummy()

Note that I had to add # noqa: B904 to the raise line.

I'm sure this is a dummy bit of code, but just making sure, please state in the RuntimeError a more descriptive error:

RuntimeError("SSL support unavailable with your build of python runtime. Please ensure you've compiled it in.")

@davidism
Copy link
Contributor Author

davidism commented Oct 7, 2021

That's the real code, copy pasted 😅 I did notice the message was a bit terse and will fix it.

@cooperlees
Copy link
Collaborator

That's the real code, copy pasted sweat_smile I did notice the message was a bit terse and will fix it.

I'm glad my dislike for vague error messages will possibly help someone one day understand this more easily!

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 a pull request may close this issue.

2 participants