Skip to content

Commit

Permalink
Redo edgedb basic types to inherit from builtin types (#366)
Browse files Browse the repository at this point in the history
* edgedb.Tuple is now a subclass of tuple
* Drop edgedb.Tuple custom implementation
* edgedb.NamedTuple is now a subclass of tuple
* Drop custom impl of Set and Array
* edgedb.EnumValue is now a subclass of enum.Enum
* Drop hash and richcompare impl of edgedb.Object
* Update docs and mention 1.0 version

Co-authored-by: Yury Selivanov <yury@edgedb.com>
  • Loading branch information
fantix and 1st1 committed Sep 28, 2022
1 parent dfb8c8b commit b11b991
Show file tree
Hide file tree
Showing 33 changed files with 556 additions and 1,537 deletions.
105 changes: 56 additions & 49 deletions docs/api/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ The table below shows the correspondence between EdgeDB and Python types.
| ``anytuple`` | :py:class:`edgedb.Tuple` or |
| | :py:class:`edgedb.NamedTuple` |
+----------------------------+-----------------------------------------------------+
| ``anyenum`` | :py:class:`str <python:str>` |
| ``anyenum`` | :py:class:`edgedb.EnumValue` |
+----------------------------+-----------------------------------------------------+
| ``Object`` | :py:class:`edgedb.Object` |
+----------------------------+-----------------------------------------------------+
Expand Down Expand Up @@ -81,20 +81,7 @@ Sets

.. py:class:: Set()
A representation of an immutable set of values returned by a query.

The :py:meth:`AsyncIOClient.query() <edgedb.AsyncIOClient.query>` and
:py:meth:`Client.query() <edgedb.Client.query>`
methods return an instance of this type. Nested sets in the result are
also returned as ``Set`` objects.

.. describe:: len(s)

Return the number of fields in set *s*.

.. describe:: iter(s)

Return an iterator over the *values* of the set *s*.
This is :py:class:`list <python:list>` since version 1.0.


.. _edgedb-python-types-object:
Expand All @@ -106,6 +93,19 @@ Objects
An immutable representation of an object instance returned from a query.

.. versionchanged:: 1.0

``edgedb.Object`` instances are dataclass-compatible since version 1.0,
for example, ``dataclasses.is_dataclass()`` will return ``True``, and
``dataclasses.asdict()`` will work on ``edgedb.Object`` instances.

.. versionchanged:: 1.0

``edgedb.Object.__hash__`` is just ``object.__hash__` in version 1.0.
Similarly, ``==`` is equivalent to the ``is`` operator comparing
``edgedb.Object`` instances, and ``<``, ``<=``, ``>``, ``>=`` are not
allowed on ``edgedb.Object`` instances.

The value of an object property or a link can be accessed through
a corresponding attribute:

Expand Down Expand Up @@ -181,24 +181,7 @@ Tuples

.. py:class:: Tuple()
An immutable value representing an EdgeDB tuple value.

Instances of ``edgedb.Tuple`` generally behave exactly like
standard Python tuples:

.. code-block:: pycon
>>> import edgedb
>>> client = edgedb.create_client()
>>> r = client.query_single('''SELECT (1, 'a', [3])''')
>>> r
(1, 'a', [3])
>>> len(r)
3
>>> r[1]
'a'
>>> r == (1, 'a', [3])
True
This is :py:class:`tuple <python:tuple>` since version 1.0.


Named Tuples
Expand All @@ -208,6 +191,12 @@ Named Tuples
An immutable value representing an EdgeDB named tuple value.

.. versionchanged:: 1.0

``edgedb.NamedTuple`` is a subclass of :py:class:`tuple <python:tuple>`
and is duck-type compatible with ``collections.namedtuple`` since
version 1.0.

Instances of ``edgedb.NamedTuple`` generally behave similarly to
:py:func:`namedtuple <python:collections.namedtuple>`:

Expand All @@ -224,35 +213,24 @@ Named Tuples
1
>>> r == (1, 'a', [3])
True
>>> r._fields
('a', 'b', 'c')
Arrays
======

.. py:class:: Array()
An immutable value representing an EdgeDB array value.
This is :py:class:`list <python:list>` since version 1.0.

.. code-block:: pycon
>>> import edgedb
>>> client = edgedb.create_client()
>>> r = client.query_single('''SELECT [1, 2, 3]''')
>>> r
[1, 2, 3]
>>> len(r)
3
>>> r[1]
2
>>> r == [1, 2, 3]
True

RelativeDuration
================

.. py:class:: RelativeDuration()
An immutable value represeting an EdgeDB ``cal::relative_duration`` value.
An immutable value representing an EdgeDB ``cal::relative_duration`` value.

.. code-block:: pycon
Expand All @@ -274,7 +252,7 @@ DateDuration

.. py:class:: DateDuration()
An immutable value represeting an EdgeDB ``cal::date_duration`` value.
An immutable value representing an EdgeDB ``cal::date_duration`` value.

.. code-block:: pycon
Expand All @@ -287,3 +265,32 @@ DateDuration
12
>>> r.days
2
EnumValue
=========

.. py:class:: EnumValue()
An immutable value representing an EdgeDB enum value.

.. versionchanged:: 1.0

Since version 1.0, ``edgedb.EnumValue`` is a subclass of
:py:class:`enum.Enum <python:enum.Enum>`. Actual enum values are
instances of ad-hoc enum classes created by the codecs to represent
the actual members defined in your EdgeDB schema.

.. code-block:: pycon
>>> import edgedb
>>> client = edgedb.create_client()
>>> r = client.query_single("""SELECT <Color>'red'""")
>>> r
<edgedb.EnumValue 'red'>
>>> str(r)
'red'
>>> r.value # added in 1.0
'red'
>>> r.name # added in 1.0, simply str.upper() of r.value
'RED'
2 changes: 1 addition & 1 deletion edgedb/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

from ._version import __version__

from edgedb.datatypes import Range
from edgedb.datatypes.datatypes import (
Tuple, NamedTuple, EnumValue, RelativeDuration, DateDuration, ConfigMemory
)
from edgedb.datatypes.datatypes import Set, Object, Array, Link, LinkSet
from edgedb.datatypes.range import Range

from .abstract import (
Executor, AsyncIOExecutor, ReadOnlyExecutor, AsyncIOReadOnlyExecutor
Expand Down
5 changes: 2 additions & 3 deletions edgedb/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import typing

from . import options
from .datatypes import datatypes
from .protocol import protocol

__all__ = (
Expand Down Expand Up @@ -103,7 +102,7 @@ class ReadOnlyExecutor(BaseReadOnlyExecutor):
def _query(self, query_context: QueryContext):
...

def query(self, query: str, *args, **kwargs) -> datatypes.Set:
def query(self, query: str, *args, **kwargs) -> list:
return self._query(QueryContext(
query=QueryWithArgs(query, args, kwargs),
cache=self._get_query_cache(),
Expand Down Expand Up @@ -186,7 +185,7 @@ class AsyncIOReadOnlyExecutor(BaseReadOnlyExecutor):
async def _query(self, query_context: QueryContext):
...

async def query(self, query: str, *args, **kwargs) -> datatypes.Set:
async def query(self, query: str, *args, **kwargs) -> list:
return await self._query(QueryContext(
query=QueryWithArgs(query, args, kwargs),
cache=self._get_query_cache(),
Expand Down
3 changes: 0 additions & 3 deletions edgedb/datatypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#


from .range import Range # noqa
Loading

0 comments on commit b11b991

Please sign in to comment.