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

Remove warnings in sample code snippets related to using using filter positional args instead of keyword argument #504

Open
parthea opened this issue Dec 3, 2023 · 1 comment
Labels
api: datastore Issues related to the googleapis/python-datastore API. samples Issues that are directly related to samples. type: cleanup An internal cleanup or hygiene concern.

Comments

@parthea
Copy link
Contributor

parthea commented Dec 3, 2023

completed_tasks = client.query(kind="Task").add_filter("done", "=", True)

_____________ TestDatastoreSnippets.test_avg_query_property_filter _____________

self = 
capsys = <_pytest.capture.CaptureFixture object at 0x7fede0934e80>
client = 

    @backoff.on_exception(backoff.expo, AssertionError, max_time=240)
    def test_avg_query_property_filter(self, capsys, client):
>       tasks = snippets.avg_query_property_filter(client)

snippets_test.py:191: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
snippets.py:340: in avg_query_property_filter
    completed_tasks = client.query(kind="Task").add_filter("done", "=", True)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = 
property_name = 'done', operator = '=', value = True

    def add_filter(
        self,
        property_name=None,
        operator=None,
        value=None,
        *,
        filter=None,
    ):
        """Filter the query based on a property name, operator and a value.
    
        Expressions take the form of::
    
          .add_filter(
            filter=PropertyFilter('', '', )
          )
    
        where property is a property stored on the entity in the datastore
        and operator is one of ``OPERATORS``
        (ie, ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``):
    
        Both AND and OR operations are supported by passing in a `CompositeFilter` object to the `filter` parameter::
    
           .add_filter(
               filter=And(
                   [
                       PropertyFilter('', '', ),
                       PropertyFilter('', '', )
    
                   ]
               )
           )
    
           .add_filter(
               filter=Or(
                   [
                       PropertyFilter('', '', ),
                       PropertyFilter('', '', )
                   ]
               )
           )
    
        .. testsetup:: query-filter
    
            import uuid
    
            from google.cloud import datastore
            from google.cloud.datastore.query import PropertyFilter
    
            client = datastore.Client()
    
        .. doctest:: query-filter
    
            >>> query = client.query(kind='Person')
            >>> query = query.add_filter(filter=PropertyFilter('name', '=', 'James'))
            >>> query = query.add_filter(filter=PropertyFilter('age', '>', 50))
    
        :type property_name: str
        :param property_name: A property name.
    
        :type operator: str
        :param operator: One of ``=``, ``<``, ``<=``, ``>``, ``>=``, ``!=``, ``IN``, ``NOT_IN``.
    
        :type value: :class:`int`, :class:`str`, :class:`bool`,
                     :class:`float`, :class:`NoneType`,
                     :class:`datetime.datetime`,
                     :class:`google.cloud.datastore.key.Key`
        :param value: The value to filter on.
    
        :type filter: :class:`CompositeFilter`, :class:`PropertyFiler`
        :param filter: A instance of a `BaseFilter`, either a `CompositeFilter` or `PropertyFilter`.
    
        :rtype: :class:`~google.cloud.datastore.query.Query`
        :returns: A query object.
    
        :raises: :class:`ValueError` if ``operation`` is not one of the
                 specified values, or if a filter names ``'__key__'`` but
                 passes an invalid value (a key is required).
        """
        if isinstance(property_name, PropertyFilter):
            raise ValueError(
                "PropertyFilter object must be passed using keyword argument 'filter'"
            )
        if isinstance(property_name, BaseCompositeFilter):
            raise ValueError(
                "'Or' and 'And' objects must be passed using keyword argument 'filter'"
            )
    
        if property_name is not None and operator is not None:
            if filter is not None:
                raise ValueError(
                    "Can't pass in both the positional arguments and 'filter' at the same time"
                )
    
            if property_name == KEY_PROPERTY_NAME and not isinstance(value, Key):
                raise ValueError('Invalid key: "%s"' % value)
    
            if self.OPERATORS.get(operator) is None:
                error_message = 'Invalid expression: "%s"' % (operator,)
                choices_message = "Please use one of: =, <, <=, >, >=, !=, IN, NOT_IN."
                raise ValueError(error_message, choices_message)
    
>           warnings.warn(
                "Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.",
                UserWarning,
                stacklevel=2,
            )
E           UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.

../../google/cloud/datastore/query.py:430: UserWarning
@parthea parthea added the type: cleanup An internal cleanup or hygiene concern. label Dec 3, 2023
@product-auto-label product-auto-label bot added api: datastore Issues related to the googleapis/python-datastore API. samples Issues that are directly related to samples. labels Dec 3, 2023
@dhimmel
Copy link

dhimmel commented Mar 13, 2024

Noting related issues from firestore:

Based on those issues, it's not clear whether the warning is intentional due to a planned deprecation. Examples of the proposed solution to the warning would be helpful.

dhimmel added a commit to related-sciences/articat that referenced this issue Mar 13, 2024
refs googleapis/python-datastore#504

To get rid of:
UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.
dhimmel added a commit to related-sciences/articat that referenced this issue Mar 14, 2024
refs googleapis/python-datastore#504

To get rid of:
UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.
dhimmel added a commit to related-sciences/articat that referenced this issue Mar 14, 2024
merges #56
refs googleapis/python-datastore#504

To get rid of:
UserWarning: Detected filter using positional arguments. Prefer using the 'filter' keyword argument instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: datastore Issues related to the googleapis/python-datastore API. samples Issues that are directly related to samples. type: cleanup An internal cleanup or hygiene concern.
Projects
None yet
Development

No branches or pull requests

2 participants