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

Redo edgedb basic types to inherit from builtin types #366

Merged
merged 14 commits into from
Sep 28, 2022
Merged
93 changes: 44 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 0.25.0.


.. _edgedb-python-types-object:
Expand All @@ -106,6 +93,10 @@ Objects

An immutable representation of an object instance returned from a query.

``edgedb.Object`` instances are dataclass-compatible since version 0.25.0.
for example, ``dataclasses.is_dataclass()`` will return ``True``, and
``dataclasses.asdict()`` will work 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 +172,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 0.25.0.


Named Tuples
Expand All @@ -208,6 +182,11 @@ Named Tuples

An immutable value representing an EdgeDB named tuple value.

``edgedb.NamedTuple`` is a subclass of :py:class:`tuple <python:tuple>`
since version 0.25.0. Actual named tuple values are instances of ad-hoc
classes created by the codec to represent the specific names, but the
ad-hoc classes are direct subclasses of ``edgedb.NamedTuple``.
fantix marked this conversation as resolved.
Show resolved Hide resolved

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

Expand All @@ -224,35 +203,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.

.. code-block:: pycon
This is :py:class:`list <python:list>` since version 0.25.0.

>>> 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 +242,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 +255,30 @@ DateDuration
12
>>> r.days
2


EnumValue
=========

.. py:class:: EnumValue()

An immutable value representing an EdgeDB enum value.

``edgedb.EnumValue`` is a subclass of :py:class:`enum.Enum <python:enum.Enum>`
since version 0.25.0. Actual enum values are instances of ad-hoc classes
created by the codec to represent the specific enums, but the ad-hoc
classes are direct subclasses of ``edgedb.EnumValue``.
fantix marked this conversation as resolved.
Show resolved Hide resolved

.. 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 0.25.0
'red'
>>> r.name # added in 0.25.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