def count(self, limit=NOT_PROVIDED):
        if self.included_pks is not None:
            return len(self.get_matching_pk(0, limit))
        if self.excluded_pks:
            return len(list(self.fetch(0, 2000)))
        # The datastore's Count() method has a 'limit' kwarg, which has
        # a default value (obviously).  This value can be overridden to
        # anything you like, and importantly can be overridden to
        # unlimited by passing a value of None.  Hence *this* method
        # has a default value of NOT_PROVIDED, rather than a default
        # value of None
        kw = {}
        if limit is not NOT_PROVIDED:
            kw['limit'] = limit
        return self._build_query().Count(**kw)