internlm/SWE-Fixer-Train-110K huggingface dataset:
Repository: googleapis/google-cloud-python

Issue:
Pub/Sub Subscriber Client.subscribe() may drop callback on the floor
From [source][1]

```python
    def subscribe(self, subscription, callback=None, flow_control=()):
        ...
        if callable(callback):
            subscr.open(callback)
```

Instead it should be something like:

```python
if callback is not None:
    if callable(callback):
        subscr.open(callback)
    else:
        raise TypeError('Hey man, that is not, like, callable, man.')
```

[1]: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/cc7da65b4a31d7a33055afe0bd6dc9855c3d6493/pubsub/google/cloud/pubsub_v1/subscriber/client.py#L130-L131


Discussion:

Resolving patch:
diff --git a/pubsub/google/cloud/pubsub_v1/subscriber/client.py b/pubsub/google/cloud/pubsub_v1/subscriber/client.py
--- a/pubsub/google/cloud/pubsub_v1/subscriber/client.py
+++ b/pubsub/google/cloud/pubsub_v1/subscriber/client.py
@@ -124,9 +124,15 @@ def subscribe(self, subscription, callback=None, flow_control=()):
         Returns:
             ~.pubsub_v1.subscriber.consumer.base.BaseConsumer: An instance
                 of the defined ``consumer_class`` on the client.
+
+        Raises:
+            TypeError: If ``callback`` is not callable.
         """
         flow_control = types.FlowControl(*flow_control)
         subscr = self._policy_class(self, subscription, flow_control)
         if callable(callback):
             subscr.open(callback)
+        elif callback is not None:
+            error = '{!r} is not callable, please check input'.format(callback)
+            raise TypeError(error)
         return subscr


Test patch:
diff --git a/pubsub/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py b/pubsub/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
--- a/pubsub/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
+++ b/pubsub/tests/unit/pubsub_v1/subscriber/test_subscriber_client.py
@@ -14,6 +14,7 @@
 
 from google.auth import credentials
 import mock
+import pytest
 
 from google.cloud.pubsub_v1 import subscriber
 from google.cloud.pubsub_v1.subscriber.policy import thread
@@ -54,3 +55,12 @@ def test_subscribe_with_callback():
         subscription = client.subscribe('sub_name_b', callback)
         open_.assert_called_once_with(callback)
     assert isinstance(subscription, thread.Policy)
+
+
+def test_subscribe_with_failed_callback():
+    creds = mock.Mock(spec=credentials.Credentials)
+    client = subscriber.Client(credentials=creds)
+    callback = 'abcdefg'
+    with pytest.raises(TypeError) as exc_info:
+        subscription = client.subscribe('sub_name_b', callback)
+    assert callback in str(exc_info.value)
Repository: googleapis/google-cloud-python

Issue:
PubSub: correctly decrement the byte counter in drop()
I'm looking at this 2 files:
- https://github.com/GoogleCloudPlatform/google-cloud-python/blob/5c010e3790c9067e6d7bf8ba37724ee5121eadb7/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py#L215
- https://github.com/GoogleCloudPlatform/google-cloud-python/blob/5c010e3790c9067e6d7bf8ba37724ee5121eadb7/pubsub/tests/unit/pubsub_v1/subscriber/test_policy_base.py

Should the code in `base.py` line 215 be `max` instead of `min`?
```
self._bytes = min([self._bytes, 0])
```

Current code:
1. current state `self._bytes = 50`
2. then we call `self._bytes -= 20`
3. now `self._bytes == 30`
4. but then we do `self._bytes = min([30, 0])` -> `self._bytes = 0`

Should be:
1. current state `self._bytes = 50`
2. then we call `self._bytes -= 20`
3. now `self._bytes == 30`
4. but then we do `self._bytes = max([30, 0])` -> `self._bytes = 30`

even if we drop it below 0, we should use `max`:
1. `self._bytes = 10`
2. `self._bytes -= 20`
3. `self._bytes == -10`
4. `min([-10, 0]) == -10` should be `max([-10, 0]) == 0`

Or is there something I miss?



Discussion:

Resolving patch:
diff --git a/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py b/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py
--- a/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py
+++ b/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py
@@ -212,7 +212,10 @@ def drop(self, ack_id, byte_size):
         if ack_id in self.managed_ack_ids:
             self.managed_ack_ids.remove(ack_id)
             self._bytes -= byte_size
-            self._bytes = min([self._bytes, 0])
+            if self._bytes < 0:
+                _LOGGER.debug(
+                    'Bytes was unexpectedly negative: %d', self._bytes)
+                self._bytes = 0
 
         # If we have been paused by flow control, check and see if we are
         # back within our limits.


Test patch:
diff --git a/pubsub/tests/unit/pubsub_v1/subscriber/test_policy_base.py b/pubsub/tests/unit/pubsub_v1/subscriber/test_policy_base.py
--- a/pubsub/tests/unit/pubsub_v1/subscriber/test_policy_base.py
+++ b/pubsub/tests/unit/pubsub_v1/subscriber/test_policy_base.py
@@ -12,6 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import logging
 import time
 
 from google.api_core import exceptions
@@ -136,6 +137,18 @@ def test_drop():
     assert policy._bytes == 0
 
 
+@mock.patch.object(base, '_LOGGER', spec=logging.Logger)
+def test_drop_unexpected_negative(_LOGGER):
+    policy = create_policy()
+    policy.managed_ack_ids.add('ack_id_string')
+    policy._bytes = 0
+    policy.drop('ack_id_string', 20)
+    assert len(policy.managed_ack_ids) == 0
+    assert policy._bytes == 0
+    _LOGGER.debug.assert_called_once_with(
+        'Bytes was unexpectedly negative: %d', -20)
+
+
 def test_drop_below_threshold():
     """Establish that we resume a paused subscription.
Repository: googleapis/google-cloud-python

Issue:
Pub / Sub clients don't add scopes to passed-in credentials
This was reported in https://github.com/GoogleCloudPlatform/google-auth-library-python/issues/225 but I have yet to confirm.


Discussion:
/cc @jonparrott 

The fix is probably in [`grpc_helpers`][1], where we could use 

```python
if credentials is None:
    credentials, _ = google.auth.default(scopes=scopes)
elif not credentials.has_scopes(scopes):
    credentials = credentials.with_scopes(scopes)
```

[1]: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/fbe4e243d8cf03d4ffc60c7b19e86a0166e44514/api_core/google/api_core/grpc_helpers.py#L128-L129

Resolving patch:
diff --git a/api_core/google/api_core/grpc_helpers.py b/api_core/google/api_core/grpc_helpers.py
--- a/api_core/google/api_core/grpc_helpers.py
+++ b/api_core/google/api_core/grpc_helpers.py
@@ -20,6 +20,7 @@
 from google.api_core import exceptions
 from google.api_core import general_helpers
 import google.auth
+import google.auth.credentials
 import google.auth.transport.grpc
 import google.auth.transport.requests
 
@@ -127,6 +128,9 @@ def create_channel(target, credentials=None, scopes=None, **kwargs):
     """
     if credentials is None:
         credentials, _ = google.auth.default(scopes=scopes)
+    else:
+        credentials = google.auth.credentials.with_scopes_if_required(
+            credentials, scopes)
 
     request = google.auth.transport.requests.Request()
 


Test patch:
diff --git a/api_core/tests/unit/test_grpc_helpers.py b/api_core/tests/unit/test_grpc_helpers.py
--- a/api_core/tests/unit/test_grpc_helpers.py
+++ b/api_core/tests/unit/test_grpc_helpers.py
@@ -18,6 +18,7 @@
 
 from google.api_core import exceptions
 from google.api_core import grpc_helpers
+import google.auth.credentials
 
 
 def test__patch_callable_name():
@@ -169,3 +170,19 @@ def test_create_channel_explicit(secure_authorized_channel):
     assert channel is secure_authorized_channel.return_value
     secure_authorized_channel.assert_called_once_with(
         mock.sentinel.credentials, mock.ANY, target)
+
+
+@mock.patch('google.auth.transport.grpc.secure_authorized_channel')
+def test_create_channel_explicit_scoped(unused_secure_authorized_channel):
+    scopes = ['1', '2']
+
+    credentials = mock.create_autospec(
+        google.auth.credentials.Scoped, instance=True)
+    credentials.requires_scopes = True
+
+    grpc_helpers.create_channel(
+        mock.sentinel.target,
+        credentials=credentials,
+        scopes=scopes)
+
+    credentials.with_scopes.assert_called_once_with(scopes)
Repository: googleapis/google-cloud-python

Issue:
Pub/Sub API lists the wrong (slightly incompatible) type for Message.attributes
When a subscriber receives a message, the API says it has an 'attributes' member that is a dict:

https://github.com/GoogleCloudPlatform/google-cloud-python/blob/11e310ab9f63f06252ff2b9ada201fd8c11ce06c/pubsub/google/cloud/pubsub_v1/subscriber/message.py#L36

However, it is not, in fact, a dict; it is a protobuf map. The behaviour is quite different, which the Python protobuf API documentation explains at some length:

https://developers.google.com/protocol-buffers/docs/reference/python-generated#map-fields

This is not a small difference... the semantic gap is substantial, because using the `.attributes` type in place of a dict breaks code like this:

```
message_rev = None
if some_condition:
  try:
    message_rev = int(message.attributes['revision'])
  except KeyError:
    message_rev = DEFAULT
```

In this case, instead of assigning the value `DEFAULT`, this code simply raises a new exception that doesn't happen with dicts (it's a ValueError, because the try-block will execute `int('')`). This is an incompatible, breaking change.

Again, this is a subtle, but important, API difference. At a minimum, it seems like the PubSub API docs should point to the Protobuf docs, instead of simply listing the incorrect type.


Discussion:

Resolving patch:
diff --git a/pubsub/google/cloud/pubsub_v1/subscriber/message.py b/pubsub/google/cloud/pubsub_v1/subscriber/message.py
--- a/pubsub/google/cloud/pubsub_v1/subscriber/message.py
+++ b/pubsub/google/cloud/pubsub_v1/subscriber/message.py
@@ -33,7 +33,8 @@ class Message(object):
             to use this directly.
         data (bytes): The data in the message. Note that this will be a
             :class:`bytes`, not a text string.
-        attributes (dict): The attributes sent along with the message.
+        attributes (.ScalarMapContainer): The attributes sent along with the
+            message. See :attr:`attributes` for more information on this type.
         publish_time (datetime): The time that this message was originally
             published.
     """
@@ -85,8 +86,18 @@ def __repr__(self):
     def attributes(self):
         """Return the attributes of the underlying Pub/Sub Message.
 
+        .. warning::
+
+            A ``ScalarMapContainer`` behaves slightly differently than a
+            ``dict``. For a Pub / Sub message this is a ``string->string`` map.
+            When trying to access a value via ``map['key']``, if the key is
+            not in the map, then the default value for the string type will
+            be returned, which is an empty string. It may be more intuitive
+            to just cast the map to a ``dict`` or to one use ``map.get``.
+
         Returns:
-            dict: The message's attributes.
+            .ScalarMapContainer: The message's attributes. This is a
+            ``dict``-like object provided by ``google.protobuf``.
         """
         return self._message.attributes
 


Test patch:
Repository: googleapis/google-cloud-python

Issue:
Pub / Sub `Message.__str__` does not unpack a protobuf map
For example

```Message {
    data: b'Wooooo! The claaaaaw! (index=12)'
    attributes: <google.protobuf.pyext._message.ScalarMapContainer object at 0x7f526922a7b0>
}
```

~~IIUC `str(ScalarMapContainer)` will resemble something user friendly (but we are using `repr()`)~~ I was wrong.


Discussion:

Resolving patch:
diff --git a/pubsub/google/cloud/pubsub_v1/subscriber/message.py b/pubsub/google/cloud/pubsub_v1/subscriber/message.py
--- a/pubsub/google/cloud/pubsub_v1/subscriber/message.py
+++ b/pubsub/google/cloud/pubsub_v1/subscriber/message.py
@@ -14,10 +14,37 @@
 
 from __future__ import absolute_import
 
+import json
 import math
 import time
 
 
+_MESSAGE_REPR = """\
+Message {{
+  data: {!r}
+  attributes: {}
+}}"""
+
+
+def _indent(lines, prefix='  '):
+    """Indent some text.
+
+    Note that this is present as ``textwrap.indent``, but not in Python 2.
+
+    Args:
+        lines (str): The newline delimited string to be indented.
+        prefix (Optional[str]): The prefix to indent each line with. Default
+            to two spaces.
+
+    Returns:
+        str: The newly indented content.
+    """
+    indented = []
+    for line in lines.split('\n'):
+        indented.append(prefix + line)
+    return '\n'.join(indented)
+
+
 class Message(object):
     """A representation of a single Pub/Sub message.
 
@@ -72,14 +99,18 @@ def __repr__(self):
         # Get an abbreviated version of the data.
         abbv_data = self._message.data
         if len(abbv_data) > 50:
-            abbv_data = abbv_data[0:50] + b'...'
-
-        # Return a useful representation.
-        answer = 'Message {\n'
-        answer += '    data: {0!r}\n'.format(abbv_data)
-        answer += '    attributes: {0!r}\n'.format(self.attributes)
-        answer += '}'
-        return answer
+            abbv_data = abbv_data[:50] + b'...'
+
+        pretty_attrs = json.dumps(
+            dict(self.attributes),
+            indent=2,
+            separators=(',', ': '),
+            sort_keys=True,
+        )
+        pretty_attrs = _indent(pretty_attrs)
+        # We don't actually want the first line indented.
+        pretty_attrs = pretty_attrs.lstrip()
+        return _MESSAGE_REPR.format(abbv_data, pretty_attrs)
 
     @property
     def attributes(self):


Test patch:
diff --git a/pubsub/tests/unit/pubsub_v1/subscriber/test_message.py b/pubsub/tests/unit/pubsub_v1/subscriber/test_message.py
--- a/pubsub/tests/unit/pubsub_v1/subscriber/test_message.py
+++ b/pubsub/tests/unit/pubsub_v1/subscriber/test_message.py
@@ -100,3 +100,19 @@ def test_nack():
             'ack_id': 'bogus_id',
             'byte_size': 25,
         }))
+
+
+def test_repr():
+    data = b'foo'
+    msg = create_message(data, snow='cones', orange='juice')
+    data_line = '  data: {!r}'.format(data)
+    expected_repr = '\n'.join((
+        'Message {',
+        data_line,
+        '  attributes: {',
+        '    "orange": "juice",',
+        '    "snow": "cones"',
+        '  }',
+        '}',
+    ))
+    assert repr(msg) == expected_repr
Repository: googleapis/google-cloud-python

Issue:
Pub / Sub `maintain_leases` checks if consumer is active **twice**
[Source][1]:

```python
if self._consumer.stopped.is_set():
    _LOGGER.debug('Consumer inactive, ending lease maintenance.')
    return
...
if ack_ids and not self._consumer.stopped.is_set():
    ...
```

I'm not sure if this is on purpose (since a different thread could update `consumer.stoped`) or if this just reflects an old state of the code.

[1]: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/235ac559531012474d05e0bc48ca730f3962d655/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py#L311-L327


Discussion:

Resolving patch:
diff --git a/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py b/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py
--- a/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py
+++ b/pubsub/google/cloud/pubsub_v1/subscriber/policy/base.py
@@ -324,11 +324,16 @@ def maintain_leases(self):
             # because it is more efficient to make a single request.
             ack_ids = list(self.managed_ack_ids)
             _LOGGER.debug('Renewing lease for %d ack IDs.', len(ack_ids))
-            if ack_ids and not self._consumer.stopped.is_set():
+            if ack_ids:
                 request = types.StreamingPullRequest(
                     modify_deadline_ack_ids=ack_ids,
                     modify_deadline_seconds=[p99] * len(ack_ids),
                 )
+                # NOTE: This may not work as expected if ``consumer.stopped``
+                #       has been set since we checked it. An implementation
+                #       without any sort of race condition would require a
+                #       way for ``send_request`` to fail when the consumer
+                #       is stopped.
                 self._consumer.send_request(request)
 
             # Now wait an appropriate period of time and do this again.


Test patch:
Repository: googleapis/google-cloud-python

Issue:
Bigtable: valid range for RowSampleFilter is incorrect.
This is a documentation bug, here:

[https://github.com/GoogleCloudPlatform/google-cloud-python/blob/master/bigtable/google/cloud/bigtable/row_filters.py#L171][1]

the documentation asserts that the valid range for a RowSampleFilter is `[0.0, 1.0]`.  In fact, it is the open interval `(0.0, 1.0)`.  The server rejects both `0.0` and `1.0`.

[1]: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/017714cf91a310caafc60181c044d7812c7226b7/bigtable/google/cloud/bigtable/row_filters.py#L171


Discussion:

Resolving patch:
diff --git a/bigtable/google/cloud/bigtable/row_filters.py b/bigtable/google/cloud/bigtable/row_filters.py
--- a/bigtable/google/cloud/bigtable/row_filters.py
+++ b/bigtable/google/cloud/bigtable/row_filters.py
@@ -168,7 +168,7 @@ class RowSampleFilter(RowFilter):
 
     :type sample: float
     :param sample: The probability of matching a cell (must be in the
-                   interval ``[0, 1]``).
+                   interval ``(0, 1)``  The end points are excluded).
     """
 
     def __init__(self, sample):


Test patch:
Repository: googleapis/google-cloud-python

Issue:
BigQuery: LoadJobConfig.from_api_repr does not return config
https://github.com/GoogleCloudPlatform/google-cloud-python/blob/63b7245699510259f584bc3ca328a72af8c2120f/bigquery/google/cloud/bigquery/job.py#L677
misses `return config`. Hence cannot build based on a JSON schema file. 



Discussion:

Resolving patch:
diff --git a/bigquery/google/cloud/bigquery/job.py b/bigquery/google/cloud/bigquery/job.py
--- a/bigquery/google/cloud/bigquery/job.py
+++ b/bigquery/google/cloud/bigquery/job.py
@@ -674,6 +674,9 @@ def from_api_repr(cls, resource):
         config._properties = copy.deepcopy(resource)
         config.schema = _parse_schema_resource(schema)
         config.skip_leading_rows = _int_or_none(slr)
+        if config.skip_leading_rows is None:
+            del config.skip_leading_rows
+        return config
 
 
 class LoadJob(_AsyncJob):


Test patch:
diff --git a/bigquery/tests/unit/test_job.py b/bigquery/tests/unit/test_job.py
--- a/bigquery/tests/unit/test_job.py
+++ b/bigquery/tests/unit/test_job.py
@@ -207,6 +207,41 @@ def _verifyReadonlyResourceProperties(self, job, resource):
             self.assertIsNone(job.user_email)
 
 
+class TestLoadJobConfig(unittest.TestCase, _Base):
+    JOB_TYPE = 'load'
+
+    def _make_resource(self, started=False, ended=False):
+        resource = super(TestLoadJobConfig, self)._make_resource(
+            started, ended)
+        config = resource['configuration']['load']
+        config['sourceUris'] = [self.SOURCE1]
+        config['destinationTable'] = {
+            'projectId': self.PROJECT,
+            'datasetId': self.DS_ID,
+            'tableId': self.TABLE_ID,
+        }
+
+        return resource
+
+    @staticmethod
+    def _get_target_class():
+        from google.cloud.bigquery.job import LoadJobConfig
+        return LoadJobConfig
+
+    def test_schema(self):
+        from google.cloud.bigquery.schema import SchemaField
+        config = self._get_target_class()()
+        full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
+        age = SchemaField('age', 'INTEGER', mode='REQUIRED')
+        config.schema = [full_name, age]
+        self.assertEqual(config.schema, [full_name, age])
+
+    def test_api_repr(self):
+        resource = self._make_resource()
+        config = self._get_target_class().from_api_repr(resource)
+        self.assertEqual(config.to_api_repr(), resource)
+
+
 class TestLoadJob(unittest.TestCase, _Base):
     JOB_TYPE = 'load'
Repository: googleapis/google-cloud-python

Issue:
Problems accessing Variables properties
using Cloud SDK, I'm able to construct a Configs resource, a Variables resource in it and set a text value but, when I try to access these properties through Cloud Client Libraries for Python, I'm unable to get the values:

```
gcloud beta runtime-config configs variables get-value my-variable \
--config-name=${CONFIG} \
--project=${PROJECT}
```
Returns:
```
my-value
```
```
gcloud beta runtime-config configs variables describe my-variable  \
--config-name=${CONFIG} \
--project=${PROJECT}
```
Returns:
```
atomicName: projects/$PROJECT/configs/$NAME/variables/my-variable
name: my-variable
text: my-value
updateTime: '2017-08-29T15:03:26.359064036Z'
```
And:
```python
from google.cloud import runtimeconfig

client = runtimeconfig.Client()

config = client.config(CONFIG)
print (config.exists())
print(config.get_variable(VARIABLE).exists())
print(config.get_variable(VARIABLE).full_name)
```
Returns:
```
True
True
projects/$PROJECT/configs/$NAME/variables/$VARIABLE
```
But:
```

print(config.get_variable(VARIABLE).state)
print(config.get_variable(VARIABLE).value)
```
Returns:
```
None
None
```


Discussion:

Resolving patch:
diff --git a/runtimeconfig/google/cloud/runtimeconfig/variable.py b/runtimeconfig/google/cloud/runtimeconfig/variable.py
--- a/runtimeconfig/google/cloud/runtimeconfig/variable.py
+++ b/runtimeconfig/google/cloud/runtimeconfig/variable.py
@@ -139,10 +139,10 @@ def state(self):
 
         :rtype: str
         :returns:
-            If set, one of "UPDATED", "DELETED", or
-            "VARIABLE_STATE_UNSPECIFIED", else ``None``.
+            If set, one of "UPDATED", "DELETED", or defaults to
+            "VARIABLE_STATE_UNSPECIFIED".
         """
-        return self._properties.get('state')
+        return self._properties.get('state', STATE_UNSPECIFIED)
 
     @property
     def update_time(self):


Test patch:
diff --git a/runtimeconfig/tests/unit/test_variable.py b/runtimeconfig/tests/unit/test_variable.py
--- a/runtimeconfig/tests/unit/test_variable.py
+++ b/runtimeconfig/tests/unit/test_variable.py
@@ -56,6 +56,7 @@ def _verifyResourceProperties(self, variable, resource):
 
     def test_ctor(self):
         from google.cloud.runtimeconfig.config import Config
+        from google.cloud.runtimeconfig.variable import STATE_UNSPECIFIED
 
         client = _Client(project=self.PROJECT)
         config = Config(name=self.CONFIG_NAME, client=client)
@@ -63,6 +64,7 @@ def test_ctor(self):
         self.assertEqual(variable.name, self.VARIABLE_NAME)
         self.assertEqual(variable.full_name, self.PATH)
         self.assertEqual(variable.path, '/%s' % (self.PATH,))
+        self.assertEqual(variable.state, STATE_UNSPECIFIED)
         self.assertIs(variable.client, client)
 
     def test_ctor_w_no_name(self):