Skip to content
This repository was archived by the owner on Oct 9, 2023. It is now read-only.

Commit f3111a3

Browse files
author
Alex Walker
authored
Shorten enum names to SessionType, TransactionType (#212)
## What is the goal of this PR? While aligning Client Python with Client Java, we introduced `GraknTransaction.Type` and `GraknSession.Type`. But in Python these proved cumbersome to type out. So we reverted them to `SessionType` and `TransactionType`. ## What are the changes implemented in this PR? Shorten enum names to SessionType, TransactionType
1 parent 8eaae66 commit f3111a3

File tree

15 files changed

+77
-75
lines changed

15 files changed

+77
-75
lines changed

grakn/api/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from grakn.api.database import DatabaseManager, ClusterDatabaseManager
2222
from grakn.api.options import GraknOptions
23-
from grakn.api.session import GraknSession
23+
from grakn.api.session import GraknSession, SessionType
2424

2525

2626
class GraknClient(ABC):
@@ -34,7 +34,7 @@ def databases(self) -> DatabaseManager:
3434
pass
3535

3636
@abstractmethod
37-
def session(self, database: str, session_type: GraknSession.Type, options: GraknOptions = None) -> GraknSession:
37+
def session(self, database: str, session_type: SessionType, options: GraknOptions = None) -> GraknSession:
3838
pass
3939

4040
@abstractmethod

grakn/api/session.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,21 @@
2323

2424
from grakn.api.database import Database
2525
from grakn.api.options import GraknOptions
26-
from grakn.api.transaction import GraknTransaction
26+
from grakn.api.transaction import GraknTransaction, TransactionType
27+
28+
29+
class SessionType(enum.Enum):
30+
DATA = 0
31+
SCHEMA = 1
32+
33+
def is_data(self):
34+
return self is SessionType.DATA
35+
36+
def is_schema(self):
37+
return self is SessionType.SCHEMA
38+
39+
def proto(self):
40+
return session_proto.Session.Type.Value(self.name)
2741

2842

2943
class GraknSession(ABC):
@@ -33,7 +47,7 @@ def is_open(self) -> bool:
3347
pass
3448

3549
@abstractmethod
36-
def session_type(self) -> "Type":
50+
def session_type(self) -> "SessionType":
3751
pass
3852

3953
@abstractmethod
@@ -45,7 +59,7 @@ def options(self) -> GraknOptions:
4559
pass
4660

4761
@abstractmethod
48-
def transaction(self, transaction_type: GraknTransaction.Type, options: GraknOptions = None) -> GraknTransaction:
62+
def transaction(self, transaction_type: TransactionType, options: GraknOptions = None) -> GraknTransaction:
4963
pass
5064

5165
@abstractmethod
@@ -59,16 +73,3 @@ def __enter__(self):
5973
@abstractmethod
6074
def __exit__(self, exc_type, exc_val, exc_tb):
6175
pass
62-
63-
class Type(enum.Enum):
64-
DATA = 0
65-
SCHEMA = 1
66-
67-
def is_data(self):
68-
return self is GraknSession.Type.DATA
69-
70-
def is_schema(self):
71-
return self is GraknSession.Type.SCHEMA
72-
73-
def proto(self):
74-
return session_proto.Session.Type.Value(self.name)

grakn/api/transaction.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,28 @@
3030
from grakn.api.query.query_manager import QueryManager
3131

3232

33+
class TransactionType(enum.Enum):
34+
READ = 0
35+
WRITE = 1
36+
37+
def is_read(self):
38+
return self is TransactionType.READ
39+
40+
def is_write(self):
41+
return self is TransactionType.WRITE
42+
43+
def proto(self):
44+
return transaction_proto.Transaction.Type.Value(self.name)
45+
46+
3347
class GraknTransaction(ABC):
3448

3549
@abstractmethod
3650
def is_open(self) -> bool:
3751
pass
3852

3953
@abstractmethod
40-
def transaction_type(self) -> "Type":
54+
def transaction_type(self) -> TransactionType:
4155
pass
4256

4357
@abstractmethod
@@ -76,19 +90,6 @@ def __enter__(self):
7690
def __exit__(self, exc_type, exc_val, exc_tb):
7791
pass
7892

79-
class Type(enum.Enum):
80-
READ = 0
81-
WRITE = 1
82-
83-
def is_read(self):
84-
return self is GraknTransaction.Type.READ
85-
86-
def is_write(self):
87-
return self is GraknTransaction.Type.WRITE
88-
89-
def proto(self):
90-
return transaction_proto.Transaction.Type.Value(self.name)
91-
9293

9394
class _GraknTransactionExtended(GraknTransaction, ABC):
9495

grakn/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
# Repackaging these symbols allows them to be imported from "grakn.client"
2626
from grakn.api.options import GraknOptions # noqa # pylint: disable=unused-import
27-
from grakn.api.session import GraknSession # noqa # pylint: disable=unused-import
28-
from grakn.api.transaction import GraknTransaction # noqa # pylint: disable=unused-import
27+
from grakn.api.session import GraknSession, SessionType # noqa # pylint: disable=unused-import
28+
from grakn.api.transaction import GraknTransaction, TransactionType # noqa # pylint: disable=unused-import
2929

3030

3131
class Grakn:

grakn/cluster/client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
from grakn.api.client import GraknClusterClient
2222
from grakn.api.options import GraknOptions, GraknClusterOptions
23-
from grakn.api.session import GraknSession
23+
from grakn.api.session import SessionType
2424
from grakn.cluster.database import _ClusterDatabase
2525
from grakn.cluster.database_manager import _ClusterDatabaseManager
2626
from grakn.cluster.failsafe_task import _FailsafeTask
@@ -64,15 +64,15 @@ def is_open(self) -> bool:
6464
def databases(self) -> _ClusterDatabaseManager:
6565
return self._database_managers
6666

67-
def session(self, database: str, session_type: GraknSession.Type, options=None) -> _ClusterSession:
67+
def session(self, database: str, session_type: SessionType, options=None) -> _ClusterSession:
6868
if not options:
6969
options = GraknOptions.cluster()
7070
return self._session_any_replica(database, session_type, options) if options.read_any_replica else self._session_primary_replica(database, session_type, options)
7171

72-
def _session_primary_replica(self, database: str, session_type: GraknSession.Type, options=None) -> _ClusterSession:
72+
def _session_primary_replica(self, database: str, session_type: SessionType, options=None) -> _ClusterSession:
7373
return _OpenSessionFailsafeTask(database, session_type, options, self).run_primary_replica()
7474

75-
def _session_any_replica(self, database: str, session_type: GraknSession.Type, options=None) -> _ClusterSession:
75+
def _session_any_replica(self, database: str, session_type: SessionType, options=None) -> _ClusterSession:
7676
return _OpenSessionFailsafeTask(database, session_type, options, self).run_any_replica()
7777

7878
def database_by_name(self) -> Dict[str, _ClusterDatabase]:
@@ -109,7 +109,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
109109

110110
class _OpenSessionFailsafeTask(_FailsafeTask):
111111

112-
def __init__(self, database: str, session_type: GraknSession.Type, options: GraknClusterOptions, client: _ClusterClient):
112+
def __init__(self, database: str, session_type: SessionType, options: GraknClusterOptions, client: _ClusterClient):
113113
super().__init__(client, database)
114114
self.session_type = session_type
115115
self.options = options

grakn/cluster/session.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
from typing import TYPE_CHECKING
2020

2121
from grakn.api.options import GraknClusterOptions, GraknOptions
22-
from grakn.api.session import GraknSession
23-
from grakn.api.transaction import GraknTransaction
22+
from grakn.api.session import GraknSession, SessionType
23+
from grakn.api.transaction import TransactionType
2424
from grakn.cluster.database import _ClusterDatabase
2525
from grakn.cluster.failsafe_task import _FailsafeTask
2626
from grakn.core.database import _CoreDatabase
@@ -32,25 +32,25 @@
3232

3333
class _ClusterSession(GraknSession):
3434

35-
def __init__(self, cluster_client: "_ClusterClient", server_address: str, database: str, session_type: GraknSession.Type, options: GraknClusterOptions):
35+
def __init__(self, cluster_client: "_ClusterClient", server_address: str, database: str, session_type: SessionType, options: GraknClusterOptions):
3636
self.cluster_client = cluster_client
3737
self.core_client = cluster_client.core_client(server_address)
3838
print("Opening a session to '%s'" % server_address)
3939
self.core_session = self.core_client.session(database, session_type, options)
4040
self._options = options
4141

42-
def transaction(self, transaction_type: GraknTransaction.Type, options: GraknClusterOptions = None) -> _CoreTransaction:
42+
def transaction(self, transaction_type: TransactionType, options: GraknClusterOptions = None) -> _CoreTransaction:
4343
if not options:
4444
options = GraknOptions.cluster()
4545
return self._transaction_any_replica(transaction_type, options) if options.read_any_replica else self._transaction_primary_replica(transaction_type, options)
4646

47-
def _transaction_primary_replica(self, transaction_type: GraknTransaction.Type, options: GraknClusterOptions) -> _CoreTransaction:
47+
def _transaction_primary_replica(self, transaction_type: TransactionType, options: GraknClusterOptions) -> _CoreTransaction:
4848
return _TransactionFailsafeTask(self, transaction_type, options).run_primary_replica()
4949

50-
def _transaction_any_replica(self, transaction_type: GraknTransaction.Type, options: GraknClusterOptions) -> _CoreTransaction:
50+
def _transaction_any_replica(self, transaction_type: TransactionType, options: GraknClusterOptions) -> _CoreTransaction:
5151
return _TransactionFailsafeTask(self, transaction_type, options).run_any_replica()
5252

53-
def session_type(self) -> GraknSession.Type:
53+
def session_type(self) -> SessionType:
5454
return self.core_session.session_type()
5555

5656
def options(self) -> GraknClusterOptions:
@@ -76,7 +76,7 @@ def __exit__(self, exc_type, exc_val, exc_tb):
7676

7777
class _TransactionFailsafeTask(_FailsafeTask):
7878

79-
def __init__(self, cluster_session: _ClusterSession, transaction_type: GraknTransaction.Type, options: GraknClusterOptions):
79+
def __init__(self, cluster_session: _ClusterSession, transaction_type: TransactionType, options: GraknClusterOptions):
8080
super().__init__(cluster_session.cluster_client, cluster_session.database().name())
8181
self.cluster_session = cluster_session
8282
self.transaction_type = transaction_type

grakn/core/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from grakn.api.client import GraknClient
2424
from grakn.api.options import GraknOptions
25-
from grakn.api.session import GraknSession
25+
from grakn.api.session import SessionType
2626
from grakn.common.rpc.stub import GraknCoreStub
2727
from grakn.core.database_manager import _CoreDatabaseManager
2828
from grakn.core.session import _CoreSession
@@ -41,7 +41,7 @@ def __init__(self, address: str, parallelisation: int = 2):
4141
self._sessions: Dict[bytes, _CoreSession] = {}
4242
self._is_open = True
4343

44-
def session(self, database: str, session_type: GraknSession.Type, options=None) -> _CoreSession:
44+
def session(self, database: str, session_type: SessionType, options=None) -> _CoreSession:
4545
if not options:
4646
options = GraknOptions.core()
4747
session = _CoreSession(self, database, session_type, options)

grakn/core/session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
from grpc import RpcError
2626

2727
from grakn.api.options import GraknOptions
28-
from grakn.api.session import GraknSession
29-
from grakn.api.transaction import GraknTransaction
28+
from grakn.api.session import GraknSession, SessionType
29+
from grakn.api.transaction import GraknTransaction, TransactionType
3030
from grakn.common.concurrent.atomic import AtomicBoolean
3131
from grakn.common.concurrent.lock import ReadWriteLock
3232
from grakn.common.rpc.request_builder import session_open_req
@@ -41,7 +41,7 @@
4141
class _CoreSession(GraknSession):
4242
_PULSE_INTERVAL_SECONDS = 5
4343

44-
def __init__(self, client: "_CoreClient", database: str, session_type: GraknSession.Type, options: GraknOptions = None):
44+
def __init__(self, client: "_CoreClient", database: str, session_type: SessionType, options: GraknOptions = None):
4545
if not options:
4646
options = GraknOptions.core()
4747
self._client = client
@@ -66,7 +66,7 @@ def __init__(self, client: "_CoreClient", database: str, session_type: GraknSess
6666
def is_open(self) -> bool:
6767
return self._is_open.get()
6868

69-
def session_type(self) -> GraknSession.Type:
69+
def session_type(self) -> SessionType:
7070
return self._session_type
7171

7272
def database(self) -> _CoreDatabase:
@@ -75,7 +75,7 @@ def database(self) -> _CoreDatabase:
7575
def options(self) -> GraknOptions:
7676
return self._options
7777

78-
def transaction(self, transaction_type: GraknTransaction.Type, options: GraknOptions = None) -> GraknTransaction:
78+
def transaction(self, transaction_type: TransactionType, options: GraknOptions = None) -> GraknTransaction:
7979
if not options:
8080
options = GraknOptions.core()
8181
try:

grakn/core/transaction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
from grakn.api.options import GraknOptions
2626
from grakn.api.query.future import QueryFuture
27-
from grakn.api.transaction import _GraknTransactionExtended, GraknTransaction
27+
from grakn.api.transaction import _GraknTransactionExtended, TransactionType
2828
from grakn.common.exception import GraknClientException, TRANSACTION_CLOSED
2929
from grakn.common.rpc.request_builder import transaction_commit_req, transaction_rollback_req, transaction_open_req
3030
from grakn.common.rpc.stub import GraknCoreStub
@@ -39,7 +39,7 @@
3939

4040
class _CoreTransaction(_GraknTransactionExtended):
4141

42-
def __init__(self, session: "_CoreSession", transaction_type: GraknTransaction.Type, options: GraknOptions = None):
42+
def __init__(self, session: "_CoreSession", transaction_type: TransactionType, options: GraknOptions = None):
4343
if not options:
4444
options = GraknOptions.core()
4545
self._transaction_type = transaction_type
@@ -58,7 +58,7 @@ def __init__(self, session: "_CoreSession", transaction_type: GraknTransaction.T
5858
except RpcError as e:
5959
raise GraknClientException.of_rpc(e)
6060

61-
def transaction_type(self) -> GraknTransaction.Type:
61+
def transaction_type(self) -> TransactionType:
6262
return self._transaction_type
6363

6464
def options(self) -> GraknOptions:

tests/behaviour/config/parameters.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
# TODO: We aren't consistently using typed parameters in step implementations - we should be.
2828
from grakn.api.concept.type.attribute_type import AttributeType
29-
from grakn.api.transaction import GraknTransaction
29+
from grakn.api.transaction import TransactionType
3030
from grakn.common.label import Label
3131

3232

@@ -117,8 +117,8 @@ def parse_value_type(value: str) -> AttributeType.ValueType:
117117

118118

119119
@parse.with_pattern("read|write")
120-
def parse_transaction_type(value: str) -> GraknTransaction.Type:
121-
return GraknTransaction.Type.READ if value == "read" else GraknTransaction.Type.WRITE
120+
def parse_transaction_type(value: str) -> TransactionType:
121+
return TransactionType.READ if value == "read" else TransactionType.WRITE
122122

123123

124124
register_type(TransactionType=parse_transaction_type)

0 commit comments

Comments
 (0)