Skip to content

Commit 6158564

Browse files
committed
refactor: fallback to pyi file generation
Signed-off-by: Gabor Boros <gabor.brs@gmail.com>
1 parent 6937705 commit 6158564

File tree

12 files changed

+964
-234
lines changed

12 files changed

+964
-234
lines changed

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ docs: ## generate Sphinx HTML documentation, including API docs
9494
poetry run $(MAKE) -C docs html
9595
$(BROWSER) docs/_build/html/index.html
9696

97+
.PHONY: generate-init-pyi
98+
generate-init-pyi: ## generate the __init__.pyi file
99+
poetry run python scripts/generate_init_pyi.py && \
100+
poetry run isort rethinkdb/__init__.pyi && \
101+
poetry run black rethinkdb/__init__.pyi
102+
97103
.PHONY: format
98104
format: ## run formatters on the package
99105
poetry run isort rethinkdb tests

rethinkdb/__init__.py

Lines changed: 30 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -15,224 +15,10 @@
1515
# This file incorporates work covered by the following copyright:
1616
# Copyright 2010-2016 RethinkDB, all rights reserved.
1717

18-
# pylint: disable=redefined-builtin, unused-import
1918

20-
from types import SimpleNamespace
21-
import warnings
22-
23-
from rethinkdb import net
24-
from rethinkdb.ast import expr
25-
from rethinkdb.query import (
26-
add,
27-
and_,
28-
april,
29-
args,
30-
asc,
31-
august,
32-
avg,
33-
binary,
34-
bit_and,
35-
bit_not,
36-
bit_or,
37-
bit_sal,
38-
bit_sar,
39-
bit_xor,
40-
branch,
41-
ceil,
42-
circle,
43-
contains,
44-
count,
45-
db,
46-
db_create,
47-
db_drop,
48-
db_list,
49-
december,
50-
desc,
51-
distance,
52-
distinct,
53-
div,
54-
do,
55-
epoch_time,
56-
eq,
57-
error,
58-
february,
59-
floor,
60-
format,
61-
friday,
62-
ge,
63-
geojson,
64-
grant,
65-
group,
66-
gt,
67-
http,
68-
info,
69-
intersects,
70-
iso8601,
71-
january,
72-
js,
73-
json,
74-
july,
75-
june,
76-
le,
77-
line,
78-
literal,
79-
lt,
80-
make_timezone,
81-
map,
82-
march,
83-
max,
84-
maxval,
85-
may,
86-
min,
87-
minval,
88-
mod,
89-
monday,
90-
mul,
91-
ne,
92-
not_,
93-
november,
94-
now,
95-
object,
96-
october,
97-
or_,
98-
point,
99-
polygon,
100-
random,
101-
range,
102-
reduce,
103-
round,
104-
row,
105-
saturday,
106-
september,
107-
sub,
108-
sum,
109-
sunday,
110-
table,
111-
table_create,
112-
table_drop,
113-
table_list,
114-
thursday,
115-
time,
116-
tuesday,
117-
type_of,
118-
union,
119-
uuid,
120-
wednesday,
121-
)
122-
123-
# pylint: enable=redefined-builtin, unused-import
124-
125-
__version__ = "2.5.0"
126-
127-
# Create the r namespace object containing all query functions
128-
r = SimpleNamespace()
129-
130-
query_functions = {
131-
"add": add,
132-
"and_": and_,
133-
"april": april,
134-
"args": args,
135-
"asc": asc,
136-
"august": august,
137-
"avg": avg,
138-
"binary": binary,
139-
"bit_and": bit_and,
140-
"bit_not": bit_not,
141-
"bit_or": bit_or,
142-
"bit_sal": bit_sal,
143-
"bit_sar": bit_sar,
144-
"bit_xor": bit_xor,
145-
"branch": branch,
146-
"ceil": ceil,
147-
"circle": circle,
148-
"contains": contains,
149-
"count": count,
150-
"db": db,
151-
"db_create": db_create,
152-
"db_drop": db_drop,
153-
"db_list": db_list,
154-
"december": december,
155-
"desc": desc,
156-
"distance": distance,
157-
"distinct": distinct,
158-
"div": div,
159-
"do": do,
160-
"epoch_time": epoch_time,
161-
"eq": eq,
162-
"expr": expr,
163-
"error": error,
164-
"february": february,
165-
"floor": floor,
166-
"format": format,
167-
"friday": friday,
168-
"ge": ge,
169-
"geojson": geojson,
170-
"grant": grant,
171-
"group": group,
172-
"gt": gt,
173-
"http": http,
174-
"info": info,
175-
"intersects": intersects,
176-
"iso8601": iso8601,
177-
"january": january,
178-
"json": json,
179-
"july": july,
180-
"june": june,
181-
"le": le,
182-
"line": line,
183-
"literal": literal,
184-
"lt": lt,
185-
"make_timezone": make_timezone,
186-
"map": map,
187-
"march": march,
188-
"max": max,
189-
"maxval": maxval,
190-
"may": may,
191-
"min": min,
192-
"minval": minval,
193-
"mod": mod,
194-
"monday": monday,
195-
"mul": mul,
196-
"ne": ne,
197-
"not_": not_,
198-
"november": november,
199-
"now": now,
200-
"object": object,
201-
"october": october,
202-
"or_": or_,
203-
"point": point,
204-
"polygon": polygon,
205-
"random": random,
206-
"range": range,
207-
"reduce": reduce,
208-
"round": round,
209-
"row": row,
210-
"saturday": saturday,
211-
"september": september,
212-
"sub": sub,
213-
"sum": sum,
214-
"sunday": sunday,
215-
"table": table,
216-
"table_create": table_create,
217-
"table_drop": table_drop,
218-
"table_list": table_list,
219-
"thursday": thursday,
220-
"time": time,
221-
"tuesday": tuesday,
222-
"type_of": type_of,
223-
"union": union,
224-
"uuid": uuid,
225-
"wednesday": wednesday,
226-
"js": js,
227-
}
228-
229-
for name, func in query_functions.items():
230-
setattr(r, name, func)
231-
232-
233-
class Client:
19+
class RethinkDB:
23420
"""
235-
Client is a wrapper around RethinkDB connection handling.
21+
RethinkDB is a wrapper around RethinkDB connection handling.
23622
23723
It constructs the connection handlers and event loops, re-exports internal modules for easier
23824
use, and sets the event loop.
@@ -241,9 +27,22 @@ class Client:
24127
def __init__(self):
24228
super().__init__()
24329

30+
# pylint: disable=import-outside-toplevel
31+
from rethinkdb import ast, errors, net, query
32+
33+
self.ast = ast
34+
self.errors = errors
35+
self.net = net
36+
self.query = query
37+
24438
net.Connection._r = self
24539
self.connection_type = None
24640

41+
# Dynamically assign every re-exported internal module's function to self
42+
for module in (self.net, self.query, self.ast, self.errors):
43+
for function_name in module.__all__:
44+
setattr(self, function_name, getattr(module, function_name))
45+
24746
self.make_connection = net.make_connection
24847
self.set_loop_type(None)
24948

@@ -253,8 +52,10 @@ def set_loop_type(self, library=None) -> None:
25352
"""
25453

25554
if library == "asyncio":
256-
warnings.warn(f"{library} is not yet supported, using the default one")
257-
library = None
55+
# pylint: disable=import-outside-toplevel
56+
from rethinkdb.net_asyncio import Connection as AsyncioConnection
57+
58+
self.connection_type = AsyncioConnection
25859

25960
if library == "gevent":
26061
# pylint: disable=import-outside-toplevel
@@ -269,8 +70,10 @@ def set_loop_type(self, library=None) -> None:
26970
self.connection_type = TornadoConnection
27071

27172
if library == "trio":
272-
warnings.warn(f"{library} is not yet supported, using the default one")
273-
library = None
73+
# pylint: disable=import-outside-toplevel
74+
from rethinkdb.net_trio import Connection as TrioConnection
75+
76+
self.connection_type = TrioConnection
27477

27578
if library == "twisted":
27679
# pylint: disable=import-outside-toplevel
@@ -279,11 +82,17 @@ def set_loop_type(self, library=None) -> None:
27982
self.connection_type = TwistedConnection
28083

28184
if library is None or self.connection_type is None:
282-
self.connection_type = net.DefaultConnection
85+
# pylint: disable=import-outside-toplevel
86+
from rethinkdb.net import DefaultConnection
87+
88+
self.connection_type = DefaultConnection
28389

28490
def connect(self, *connect_args, **kwargs):
28591
"""
28692
Make a connection to the database.
28793
"""
28894

28995
return self.make_connection(self.connection_type, *connect_args, **kwargs)
96+
97+
98+
r = RethinkDB()

0 commit comments

Comments
 (0)