Skip to content

Type hint for configure_scope suggests it can return None but it should not #1799

@rafrafek

Description

@rafrafek

How do you use Sentry?

Sentry Saas (sentry.io)

Version

1.12.1

Steps to Reproduce

import sentry_sdk
with sentry_sdk.configure_scope() as scope:
    pass

Expected Result

configure_scope should always return ContextManager and should never return None.

Instead of:

-> (ContextManager | None)

should always return:

-> (ContextManager)

Actual Result

Pylance complains:

(function) configure_scope(callback: ((Scope) -> None) | None = None) -> (ContextManager | None)
Object of type "None" cannot be used with "with"

Pylance reportOptionalContextManager

image

Workaround

import sentry_sdk
scope_ctx = sentry_sdk.configure_scope()
if scope_ctx is not None:
    with scope_ctx as scope:
        pass

or using :=:

import sentry_sdk
if (scope_ctx := sentry_sdk.configure_scope()) is not None:
    with scope_ctx as scope:
        pass

Metadata

Metadata

Labels

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions