def validate_user_access_to_subscribers_helper(user_profile: Optional[UserProfile], stream_dict: Mapping[(str, Any)], check_user_subscribed: Callable[([UserProfile], bool)]) -> None:
    "Helper for validate_user_access_to_subscribers that doesn't require\n    a full stream object.  This function is a bit hard to read,\n    because it is carefully optimized for performance in the two code\n    paths we call it from:\n\n    * In `bulk_get_subscriber_user_ids`, we already know whether the\n    user was subscribed via `sub_dict`, and so we want to avoid a\n    database query at all (especially since it calls this in a loop);\n    * In `validate_user_access_to_subscribers`, we want to only check\n    if the user is subscribed when we absolutely have to, since it\n    costs a database query.\n\n    The `check_user_subscribed` argument is a function that reports\n    whether the user is subscribed to the stream.\n\n    Note also that we raise a ValidationError in cases where the\n    caller is doing the wrong thing (maybe these should be\n    AssertionErrors), and JsonableError for 400 type errors.\n    "
    if (user_profile is None):
        raise ValidationError('Missing user to validate access for')
    if (user_profile.realm_id != stream_dict['realm_id']):
        raise ValidationError('Requesting user not in given realm')
    if user_profile.is_guest:
        if check_user_subscribed(user_profile):
            return
    if ((not user_profile.can_access_public_streams()) and (not stream_dict['invite_only'])):
        raise JsonableError(_('Subscriber data is not available for this stream'))
    if user_profile.is_realm_admin:
        return
    if (stream_dict['invite_only'] and (not check_user_subscribed(user_profile))):
        raise JsonableError(_('Unable to retrieve subscribers for private stream'))
Helper for validate_user_access_to_subscribers that doesn't require
a full stream object.  This function is a bit hard to read,
because it is carefully optimized for performance in the two code
paths we call it from:

* In `bulk_get_subscriber_user_ids`, we already know whether the
user was subscribed via `sub_dict`, and so we want to avoid a
database query at all (especially since it calls this in a loop);
* In `validate_user_access_to_subscribers`, we want to only check
if the user is subscribed when we absolutely have to, since it
costs a database query.

The `check_user_subscribed` argument is a function that reports
whether the user is subscribed to the stream.

Note also that we raise a ValidationError in cases where the
caller is doing the wrong thing (maybe these should be
AssertionErrors), and JsonableError for 400 type errors.
def validate_user_access_to_subscribers_helper(user_profile: Optional[UserProfile], stream_dict: Mapping[(str, Any)], check_user_subscribed: Callable[([UserProfile], bool)]) -> None:
    "Helper for validate_user_access_to_subscribers that doesn't require\n    a full stream object.  This function is a bit hard to read,\n    because it is carefully optimized for performance in the two code\n    paths we call it from:\n\n    * In `bulk_get_subscriber_user_ids`, we already know whether the\n    user was subscribed via `sub_dict`, and so we want to avoid a\n    database query at all (especially since it calls this in a loop);\n    * In `validate_user_access_to_subscribers`, we want to only check\n    if the user is subscribed when we absolutely have to, since it\n    costs a database query.\n\n    The `check_user_subscribed` argument is a function that reports\n    whether the user is subscribed to the stream.\n\n    Note also that we raise a ValidationError in cases where the\n    caller is doing the wrong thing (maybe these should be\n    AssertionErrors), and JsonableError for 400 type errors.\n    "
    if (user_profile is None):
        raise ValidationError('Missing user to validate access for')
    if (user_profile.realm_id != stream_dict['realm_id']):
        raise ValidationError('Requesting user not in given realm')
    if user_profile.is_guest:
        if check_user_subscribed(user_profile):
            return
    if ((not user_profile.can_access_public_streams()) and (not stream_dict['invite_only'])):
        raise JsonableError(_('Subscriber data is not available for this stream'))
    if user_profile.is_realm_admin:
        return
    if (stream_dict['invite_only'] and (not check_user_subscribed(user_profile))):
        raise JsonableError(_('Unable to retrieve subscribers for private stream'))