@neralabs/rerail-pack-celery 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,683 @@
1
+ // lib/celery: Celery apps, tasks, dispatches and settings, and the SQLAlchemy sessions and models around them, as the
2
+ // celery-practice rules read them. Names resolve through imports (python/names' sym_of), so `from celery import
3
+ // shared_task` and `import celery; @celery.shared_task` both reach "celery.shared_task", and a task imported from
4
+ // another module (`from proj.tasks import ship`) is the def it names in that module.
5
+ //
6
+ // Per file, answered across files by symbol (decision 0677: no node crosses files). What a node is often depends on
7
+ // what another file binds: `@app.task` is a Celery task only when the module variable `app` that file imports is
8
+ // bound to a Celery app. Each file states what its own nodes would be, and what that depends on, as a need (nq, nk):
9
+ // the symbol nq must be of kind nk. ("", "") is no need. Each file also states, by symbol, what its own definitions
10
+ // and module variables are (cel_is_if); cel_is closes those over the run (a cross_file relation over symbols alone),
11
+ // and a rule's output reads `cel_is(nq, nk)` for each need of the nodes it reports. A class's ancestors come from the
12
+ // phase py_names (python/names), read at the file's own nodes.
13
+ //
14
+ // Interface
15
+ // Needs: cel_is(q, k) symbol q is of kind k in the run (cross_file, over symbols); ("", "")
16
+ // always holds. Kinds: "app" (a module variable bound to a Celery
17
+ // app), "app_factory" (a run function returning one), "task_fn" (a task
18
+ // decorator of the run: a wrapper function, a module variable bound to
19
+ // one), "task" (a task def, or a module variable bound to a task object),
20
+ // "task_cls" (celery's Task or a run class deriving from it), "sa_root",
21
+ // "sa_flask_db", "sa_model", "sa_model_conc" (a model that maps a
22
+ // table), "sa_maker", "sa_scoped", "sa_session_var", "sa_session_fn",
23
+ // "sa_session_alias", and "sa_run" (of q = "": the run imports SQLAlchemy)
24
+ // Names: cel_sym(e, q) sym_of, plus a module variable read in its own module and attributes
25
+ // of one (`db.Model` beside `db = SQLAlchemy()`)
26
+ // cel_run_sym(e, q) q, which e resolves to, is a def, class or module variable of the run
27
+ // cel_run_var(e, q) q, which e resolves to, is a module variable of the run
28
+ // cel_run_def(e, q) q, which e resolves to, is a def or class of the run
29
+ // cel_class_base(c, q) class_base, plus bases cel_sym resolves
30
+ // cel_derives(e, b) e names class b or a class deriving from b (py_names' ancestors)
31
+ // cel_module_var(q, v) a module-level `x = v` binds module attribute q
32
+ // Apps: cel_app_ref(e, nq, nk) expression e is a Celery app: a module variable bound to one ("app"),
33
+ // celery.current_app, a local bound to an app value (`Celery(...)`, a
34
+ // run factory returning one); by spelling (`app`, `celery_app`,
35
+ // `celery`) only for a name that resolves to nothing in the run, and
36
+ // for attributes (`self.app`)
37
+ // cel_app_value(v, nq, nk) v evaluates to a Celery app
38
+ // cel_app_cls_ref(f) f names Celery or a run class deriving from it
39
+ // cel_task(f, nq, nk) def f is a Celery task: decorated by a cel_task_fn, bare or called:
40
+ // `<app>.task` (app a cel_app_ref), celery's `shared_task`/`task`, a
41
+ // run function that applies one of those (a wrapper), a module name
42
+ // bound to one (`task = app.task`), or `@<x>.task` with x resolving to
43
+ // nothing in a document that imports celery
44
+ // cel_task_fn(e, nq, nk) e is a task decorator expression
45
+ // cel_task_decorator(f, m, nq, nk) m is f's task decorator expression, bare or called
46
+ // cel_task_option(f, k, v, nq, nk) task f's decorator passes option k=v (`bind`, `autoretry_for`, ...)
47
+ // cel_task_flag(f, k, nq, nk) task f's option k is the literal True (`acks_late=True`)
48
+ // cel_task_bound(f, nq, nk) task f is `bind=True`: its first parameter is the task itself
49
+ // cel_task_name(f, n, nq, nk) task f is registered as n: its `name="..."`, else <module>.<def>
50
+ // cel_task_named_opt(f) f's task decorator passes `name=`
51
+ // Dispatch: cel_task_ref(t, nq, nk) t is a Celery task: a name resolving to a task def of the run or to a
52
+ // module name bound to a task object ("task": `app.task(fn)`, a
53
+ // signature, an instance of a Task subclass), `<app>.tasks[...]`, or a
54
+ // signature call on one (`t.s(1)`, `t.signature(...)`, `t.set(...)`);
55
+ // any other name (RQ's `@job`, a thread pool) is not
56
+ // cel_send(c, m, nq, nk) call c enqueues a task now: `t.delay(...)`/`t.apply_async(...)` on a
57
+ // cel_task_ref, or `<x>.send_task(...)`; m is the method name
58
+ // cel_task_call(c, q, m) call c is `t.m(...)` with t resolving to the run's def q, m one of
59
+ // delay, apply_async, s, si, signature; c is a task call when
60
+ // cel_is(q, "task")
61
+ // cel_task_arg(c, a) a is an argument the task of call c receives: delay's, s's and
62
+ // si's arguments, apply_async's and send_task's args/kwargs items
63
+ // cel_name_ref(e, n, how, nq, nk) string literal e names a task as n: the first argument of
64
+ // send_task or signature ("send_task", "signature"), or the "task"
65
+ // of a beat schedule entry ("beat")
66
+ // Settings: cel_setting(k, v, s) configuration key k (the lowercase name Celery 4+ documents:
67
+ // "task_serializer", "accept_content", ...) is given value v at site s:
68
+ // `app.conf.k = v`, `app.conf.update(k=v)`, `app.conf.update({"k": v})`,
69
+ // `app.conf["k"] = v`, `Celery(..., k=v)`, a Django setting
70
+ // `CELERY_K = v`, a pre-4.0 name (`CELERYD_TASK_TIME_LIMIT`), or a
71
+ // known key assigned in a class or module named for Celery
72
+ // SQLAlchemy: sa_model(c, nq, nk) class c is a mapped SQLAlchemy model ("sa_model"): it derives
73
+ // (through the run's classes, and module variables: cel_class_base)
74
+ // from a declarative base of the run (a DeclarativeBase subclass, a
75
+ // `declarative_base()` result), Flask-SQLAlchemy's `db.Model` or
76
+ // flask_appbuilder's Model, or is a `SQLModel` class declared
77
+ // `table=True`; `__abstract__` aside
78
+ // sa_model_ref(e, nq, nk) e names a model class that maps a table ("sa_model_conc")
79
+ // sa_instance(e, nq, nk) expression e holds one model instance: `M(...)`, `session.get(M,
80
+ // ...)`, `M.query.get/first/one(...)`, `session.query(M)....first()`,
81
+ // a name bound to one of those, a parameter annotated `M`, or a value
82
+ // the type program proves is a model
83
+ // sa_session(e, nq, nk) e is a SQLAlchemy session: sa_session_proven, else sa_session_guess
84
+ // (need ("", "sa_run"))
85
+ // sa_session_proven(e, nq, nk) proven by what binds it: a session class's constructor (through
86
+ // imports and subclasses), a sessionmaker/scoped_session factory's call,
87
+ // Flask-SQLAlchemy's `db.session`, a run function returning or yielding
88
+ // one, a name or with-alias bound to one, a parameter annotated with a
89
+ // session class (also inside Annotated, or through an alias of one)
90
+ // sa_session_guess(e) the fallback by name, in a run that imports SQLAlchemy (its output
91
+ // reads cel_is("", "sa_run")): a name or attribute ending in "session"
92
+ // whose binding is not known to be something else (an unannotated
93
+ // parameter, an unresolved call, `self.session`)
94
+ // sa_with_alias(e, v) name e is the alias of `with v as e`, read in its body
95
+ // sa_session_call(c, s, m, nq, nk) call c is `s.m(...)` on a session s
96
+ // sa_write(c, nq, nk) c stages a write in a session: add, add_all, delete, merge, flush,
97
+ // bulk_*, or execute of insert/update/delete
98
+ // sa_commit(c, nq, nk) c is `<session>.commit()`
99
+ // sa_tx_with(w, b) with-statement w opens a transaction that commits when b ends:
100
+ // `with <session>.begin():`, `with <x>.begin() as s:` (a sessionmaker
101
+ // or an engine), `async with` too; b is its body
102
+ // Files: cel_test_doc(d) document d is test code (tests/test/testing/unittests directories,
103
+ // test_*.py, *_test.py, *_tests.py, tests.py, conftest.py)
104
+ // cel_django_doc(d) document d imports django (the django pack owns its dispatch rules)
105
+ // cel_celery_doc(d) document d imports celery
106
+ //
107
+ // Celery background
108
+ // - A task's registered name is `name=` when given, else `<module>.<function>` as the worker imports the module.
109
+ // - Settings have two spellings: the lowercase keys of Celery 4+, and the uppercase ones a Django project reads
110
+ // through `config_from_object("django.conf:settings", namespace="CELERY")`, `CELERY_` plus the key in capitals.
111
+ // Pre-4.0 names (CELERYD_*, CELERYBEAT_*, BROKER_*) are mapped by cel_old_key.
112
+ // - `.delay()` is `.apply_async(args, kwargs)` without execution options; `.s()`/`.si()`/`.signature()` build a
113
+ // signature that is sent later, so they are task calls (cel_task_call) but not sends (cel_send).
114
+ // e.g. app = Celery("proj") cel_is("proj.celery.app", "app")
115
+ // @app.task(bind=True, acks_late=True) cel_task(<ship>, "proj.celery.app", "app"), cel_task_bound,
116
+ // cel_task_flag(_, "acks_late", ...)
117
+ // def ship(self, order_id): ... cel_task_name(<ship>, "proj.tasks.ship", ...)
118
+ // ship.delay(order.id) cel_send(<call>, "delay", "proj.tasks.ship", "task"),
119
+ // cel_task_call(<call>, "proj.tasks.ship", "delay"),
120
+ // cel_task_arg(<call>, <order.id>)
121
+ // app.conf.task_serializer = "pickle" cel_setting("task_serializer", <"pickle">, _)
122
+ // CELERY_ACCEPT_CONTENT = ["pickle"] cel_setting("accept_content", <the list>, _)
123
+ // db.session.add(order); db.session.commit() sa_write(<add>, "proj.db.db", "sa_flask_db"), sa_commit(...)
124
+ #include "python/ast.rerail"
125
+ #include "python/calls.rerail"
126
+ #include "python/types.rerail"
127
+
128
+ // ---- needs -------------------------------------------------------------------------------------------------------
129
+ // cel_is_if(q, k, nq, nk): a file states that its own definition or module variable q is of kind k when nq is of
130
+ // kind nk. cel_is(q, k) closes them over the run. Its rows are symbols alone, and it spans files on purpose: a task's
131
+ // decorator names an app another module binds, a model derives from a base another module declares. Declared
132
+ // cross_file; each rule's output reads it for the needs of the nodes it reports, so no node leaves its file.
133
+ .decl cel_is_if(q: symbol, k: symbol, nq: symbol, nk: symbol)
134
+ .decl cel_is(q: symbol, k: symbol) cross_file
135
+ cel_is("", "").
136
+ cel_is(q, k) :- cel_is_if(q, k, nq, nk), cel_is(nq, nk).
137
+ // Some document of the run imports SQLAlchemy, SQLModel or Flask-SQLAlchemy: a repository fact, so a name guess
138
+ // (sa_session_guess) holds through it.
139
+ cel_is("", "sa_run") :- sa_lib_module(_).
140
+
141
+ // ---- names -------------------------------------------------------------------------------------------------------
142
+ // cel_sym(e, q): sym_of, plus a module-level variable read in its own module (`app = Celery()` then `@app.task` in the
143
+ // same file), which sym_of resolves only through an import.
144
+ .decl cel_sym(e: node, q: symbol)
145
+ cel_sym(e, q) :- sym_of(e, q).
146
+ cel_sym(e, @dot(mq, x)) :- kind(e, "identifier"), text(e, x), look(e, x, m), kind(m, "module"), modname(m, mq).
147
+ cel_sym(e, @dot(q, a)) :- attr_of(e, o, a), unparen(o, u), cel_sym(u, q).
148
+ // cel_run_def(e, q): q, which e resolves to, is a def or class of the run (py_names' defined, by the modules e's file
149
+ // names). cel_run_var(e, q): q is a module variable of the run: `m.x` with m a module of the run that neither defines
150
+ // nor imports x (the phase hands on no assignment). cel_run_sym(e, q): either.
151
+ .decl cel_run_def(e: node, q: symbol)
152
+ cel_run_def(e, q) :- cel_sym(e, q), docof(e, d), doc(d, p, _), py_names__reads_at(p, m), py_names__defined(m, q).
153
+ .decl cel_run_var(e: node, q: symbol)
154
+ cel_run_var(e, q) :- cel_sym(e, q), split_dot(q, m, x), docof(e, d), doc(d, p, _), py_names__reads_at(p, m),
155
+ !py_names__defined(m, q), !py_names__exports(m, x, _).
156
+ .decl cel_run_sym(e: node, q: symbol)
157
+ cel_run_sym(e, q) :- cel_run_def(e, q).
158
+ cel_run_sym(e, q) :- cel_run_var(e, q).
159
+ // cel_class_base(c, q): class_base, plus a base that is a module variable of c's own module (`Base =
160
+ // declarative_base()`; `class Order(Base)`) or an attribute of one (`db.Model`).
161
+ .decl cel_class_base(c: node, q: symbol)
162
+ cel_class_base(c, q) :- class_base(c, q).
163
+ cel_class_base(c, q) :- kind(c, "class_definition"), field(c, "superclasses", al), child(al, e, _),
164
+ !kind(e, "keyword_argument"), cel_sym(e, q).
165
+ // cel_derives(e, b): e names class b, or a class of the run deriving from b at any depth (py_names' ancestor, by the
166
+ // modules e's file names; bases resolved as class_base resolves them).
167
+ .decl cel_derives(e: node, b: symbol)
168
+ cel_derives(e, q) :- cel_sym(e, q).
169
+ cel_derives(e, b) :- cel_sym(e, q), docof(e, d), doc(d, p, _), py_names__reads_at(p, m), py_names__ancestor(m, q, b).
170
+ // cel_module_var(q, v): a module-level `x = v` binds the module attribute q.
171
+ .decl cel_module_var(q: symbol, v: node)
172
+ cel_module_var(@dot(mq, x), v) :- modnode(_, m), modname(m, mq), asg(m, x, v, _).
173
+
174
+ // ---- apps --------------------------------------------------------------------------------------------------------
175
+ // cel_app_class(q): celery.Celery under its import paths. cel_app_cls_ref(f): f names one, or a class of the run
176
+ // deriving from one.
177
+ .decl cel_app_class(q: symbol)
178
+ cel_app_class("celery.Celery"). cel_app_class("celery.app.Celery"). cel_app_class("celery.app.base.Celery").
179
+ .decl cel_app_cls_ref(f: node)
180
+ cel_app_cls_ref(f) :- cel_derives(f, b), cel_app_class(b).
181
+ // cel_app_value(v, nq, nk): v evaluates to a Celery app: `Celery(...)` (or a subclass's constructor),
182
+ // `celery.current_app`, a call of a run function that returns one (a factory: `def make_celery(): app =
183
+ // Celery(...); return app`, "app_factory"), or a name bound to one of those.
184
+ .decl cel_app_value(v: node, nq: symbol, nk: symbol)
185
+ cel_app_value(v, "", "") :- cel_app_call(v).
186
+ cel_app_value(v, "", "") :- unparen(v, u), sym_of(u, "celery.current_app").
187
+ cel_app_value(v, fq, "app_factory") :- unparen(v, k), kind(k, "call"), field(k, "function", f), cel_run_def(f, fq).
188
+ .decl cel_app_call(v: node)
189
+ cel_app_call(v) :- unparen(v, k), kind(k, "call"), field(k, "function", f), cel_app_cls_ref(f).
190
+ // A run function returns a Celery app (directly, or a local name bound to one).
191
+ cel_is_if(gq, "app_factory", nq, nk) :- kind(r, "return_statement"), child(r, v, 0), nearest(r, g, fdef),
192
+ cel_app_value(v, nq, nk), qualname(g, gq).
193
+ cel_is_if(gq, "app_factory", nq, nk) :- kind(r, "return_statement"), child(r, v, 0), unparen(v, u),
194
+ kind(u, "identifier"), text(u, x), nearest(r, g, fdef), asg(g, x, w, _), cel_app_value(w, nq, nk), qualname(g, gq).
195
+ // A module-level Celery app: `app = Celery(...)` (or a subclass of Celery), in any module of the run.
196
+ cel_is_if(q, "app", nq, nk) :- cel_module_var(q, v), cel_app_value(v, nq, nk).
197
+ // cel_run_bound(e): name e resolves to a definition of the run (a def, class or module variable, through imports),
198
+ // or to an assignment in its own function: what it holds is known, so its spelling says nothing.
199
+ .decl cel_run_bound(e: node)
200
+ cel_run_bound(e) :- cel_run_sym(e, _).
201
+ cel_run_bound(e) :- kind(e, "identifier"), text(e, x), look(e, x, s), !kind(s, "module"), asg(s, x, _, _).
202
+ // cel_app_name(x): the spellings an app goes by. Used only for a name that resolves to nothing in the run (a
203
+ // parameter, an import from outside the run) and for attributes (`flask_app.celery`), whose value the engine cannot
204
+ // follow.
205
+ .decl cel_app_name(x: symbol)
206
+ cel_app_name("app"). cel_app_name("celery_app"). cel_app_name("celery").
207
+ .decl cel_app_ref(e: node, nq: symbol, nk: symbol)
208
+ cel_app_ref(e, q, "app") :- cel_run_var(e, q).
209
+ cel_app_ref(e, "", "") :- sym_of(e, "celery.current_app").
210
+ // A local name bound to an app in its own function.
211
+ cel_app_ref(e, nq, nk) :- kind(e, "identifier"), text(e, x), look(e, x, s), !kind(s, "module"), asg(s, x, v, _),
212
+ cel_app_value(v, nq, nk).
213
+ // Fallbacks by spelling, where nothing resolves: a name bound nowhere the engine sees (a parameter, a global set
214
+ // elsewhere), and an attribute (`self.app` on a task, `flask_app.celery`), whose value the engine cannot follow.
215
+ cel_app_ref(e, "", "") :- kind(e, "identifier"), text(e, x), cel_app_name(x), !cel_run_bound(e), !sym_of(e, _).
216
+ cel_app_ref(e, "", "") :- attr_of(e, _, x), cel_app_name(x).
217
+
218
+ // ---- tasks -------------------------------------------------------------------------------------------------------
219
+ // cel_task_fn(e, nq, nk): a Celery task decorator: `<app>.task`, celery's `shared_task`/`task`, or a project wrapper
220
+ // of one (a run function that applies one, or a module name bound to one: `task = app.task`; "task_fn"). A `@task`
221
+ // from elsewhere (invoke, prefect, airflow) is not Celery's, unless the module also imports celery (below).
222
+ .decl cel_task_fn(e: node, nq: symbol, nk: symbol)
223
+ cel_task_fn(e, nq, nk) :- attr_of(e, o, "task"), unparen(o, u), cel_app_ref(u, nq, nk).
224
+ // `@<x>.task` where x resolves to nothing in the run, in a document that imports celery (an app built elsewhere).
225
+ cel_task_fn(e, "", "") :- attr_of(e, o, "task"), unparen(o, u), !cel_run_bound(u), decorator(_, d), unparen(d, w),
226
+ (w = e ; kind(w, "call"), field(w, "function", e)), docof(e, doc), cel_celery_doc(doc).
227
+ cel_task_fn(e, "", "") :- attr_of(e, o, "shared_task"), unparen(o, u), sym_of(u, "celery").
228
+ cel_task_fn(e, "", "") :- sym_of(e, q), prefix("celery.", q), (split_dot(q, _, "task") ; split_dot(q, _, "shared_task")).
229
+ cel_task_fn(e, q, "task_fn") :- cel_run_sym(e, q).
230
+ // A `task`/`shared_task` imported from an installed package (outside the run), in a document that imports celery: a
231
+ // Celery wrapper package (breatheco-de/apiv2's `task_manager.django.decorators.task`). Its body cannot be read, so the
232
+ // name and the celery import are all the engine has.
233
+ cel_task_fn(e, "", "") :- sym_of(e, q), !cel_run_sym(e, q), !prefix("celery.", q),
234
+ (split_dot(q, _, "task") ; split_dot(q, _, "shared_task")), docof(e, d), cel_celery_doc(d).
235
+ // A run function that applies a Celery task decorator in its body (`def task(**kw): return app.task(**kw)`), so
236
+ // `@task(...)` registers a Celery task; a module name bound to a task decorator.
237
+ cel_is_if(gq, "task_fn", nq, nk) :- cel_task_wrapper(g, nq, nk), qualname(g, gq).
238
+ cel_is_if(q, "task_fn", nq, nk) :- cel_module_var(q, v), unparen(v, u), cel_task_fn(u, nq, nk).
239
+ .decl cel_task_wrapper(g: node, nq: symbol, nk: symbol)
240
+ cel_task_wrapper(g, nq, nk) :- kind(g, "function_definition"), field(g, "body", b), within(n, b), kind(n, "call"),
241
+ field(n, "function", f), cel_task_fn(f, nq, nk).
242
+ cel_task_wrapper(g, nq, nk) :- kind(g, "function_definition"), field(g, "body", b), within(n, b), kind(n, "call"),
243
+ field(n, "function", c), kind(c, "call"), field(c, "function", f), cel_task_fn(f, nq, nk).
244
+ .decl cel_task_decorator(f: node, m: node, nq: symbol, nk: symbol)
245
+ cel_task_decorator(f, m, nq, nk) :- kind(f, "function_definition"), decorator(f, e), unparen(e, m),
246
+ cel_task_fn(m, nq, nk).
247
+ cel_task_decorator(f, m, nq, nk) :- kind(f, "function_definition"), decorator(f, e), unparen(e, m), kind(m, "call"),
248
+ field(m, "function", fn), cel_task_fn(fn, nq, nk).
249
+ .decl cel_task(f: node, nq: symbol, nk: symbol)
250
+ cel_task(f, nq, nk) :- cel_task_decorator(f, _, nq, nk).
251
+ cel_is_if(fq, "task", nq, nk) :- cel_task(f, nq, nk), qualname(f, fq).
252
+ .decl cel_task_option(f: node, k: symbol, v: node, nq: symbol, nk: symbol)
253
+ cel_task_option(f, k, v, nq, nk) :- cel_task_decorator(f, m, nq, nk), kind(m, "call"), call_arg(m, -1, k, v).
254
+ .decl cel_task_flag(f: node, k: symbol, nq: symbol, nk: symbol)
255
+ cel_task_flag(f, k, nq, nk) :- cel_task_option(f, k, v, nq, nk), kind(v, "true").
256
+ .decl cel_task_bound(f: node, nq: symbol, nk: symbol)
257
+ cel_task_bound(f, nq, nk) :- cel_task_flag(f, "bind", nq, nk).
258
+ // cel_task_named_opt(f): a task decorator of f passes `name=`, whatever it needs (the def is a task by another
259
+ // subgoal where this is read).
260
+ .decl cel_task_named_opt(f: node)
261
+ cel_task_named_opt(f) :- cel_task_option(f, "name", _, _, _).
262
+ .decl cel_task_name(f: node, n: symbol, nq: symbol, nk: symbol)
263
+ cel_task_name(f, n, nq, nk) :- cel_task_option(f, "name", v, nq, nk), str(v, n).
264
+ cel_task_name(f, @dot(mq, x), nq, nk) :- cel_task(f, nq, nk), !cel_task_named_opt(f), def_name(f, x), docof(f, d),
265
+ doc(d, _, mq), mq != "", nearest(f, m, scope), kind(m, "module").
266
+ .decl cel_region_stop(k: symbol)
267
+ cel_region_stop("function_definition"). cel_region_stop("lambda"). cel_region_stop("class_definition").
268
+
269
+ // ---- dispatch ----------------------------------------------------------------------------------------------------
270
+ // cel_task_ref(t, nq, nk): a Celery task, by what the run declares, never by spelling alone: RQ's `@job`, dramatiq's
271
+ // `@actor` and a module-level ThreadPool all have `.delay`/`.apply_async` too. A name of the run is one when its
272
+ // symbol is a "task": a task def, or a module variable bound to a task object.
273
+ .decl cel_task_ref(t: node, nq: symbol, nk: symbol)
274
+ cel_task_ref(t, q, "task") :- cel_run_sym(t, q).
275
+ cel_task_ref(t, nq, nk) :- unparen(t, u), kind(u, "subscript"), field(u, "value", o), attr_of(o, a, "tasks"),
276
+ unparen(a, p), cel_app_ref(p, nq, nk).
277
+ cel_task_ref(t, nq, nk) :- kind(t, "call"), field(t, "function", f), attr_of(f, o, m), cel_sig_method(m),
278
+ unparen(o, u), cel_task_ref(u, nq, nk).
279
+ // A task object built inline (`signature("shop.tasks.notify").delay()`) or bound to a local of its own function
280
+ // (`sig = ship.s(1)`; `sig.delay()`).
281
+ cel_task_ref(t, nq, nk) :- unparen(t, u), kind(u, "call"), cel_task_obj(u, nq, nk).
282
+ cel_task_ref(t, nq, nk) :- kind(t, "identifier"), text(t, x), look(t, x, s), !kind(s, "module"), asg(s, x, v, _),
283
+ cel_task_obj(v, nq, nk).
284
+ .decl cel_sig_method(m: symbol)
285
+ cel_sig_method("s"). cel_sig_method("si"). cel_sig_method("signature"). cel_sig_method("set").
286
+ cel_sig_method("clone").
287
+ // cel_task_obj(v, nq, nk): a task object: `<app>.task(fn)`/`shared_task(fn)` called as a function, a signature
288
+ // (`signature("name")`, `t.s(...)`), or an instance of a Task subclass ("task_cls").
289
+ .decl cel_task_obj(v: node, nq: symbol, nk: symbol)
290
+ cel_task_obj(v, nq, nk) :- unparen(v, k), kind(k, "call"), field(k, "function", f), cel_task_fn(f, nq, nk),
291
+ posarg(k, 0, _).
292
+ cel_task_obj(v, "", "") :- unparen(v, k), kind(k, "call"), field(k, "function", f), sym_of(f, q),
293
+ (q = "celery.signature" ; q = "celery.canvas.signature" ; q = "celery.canvas.Signature" ; q = "celery.Signature").
294
+ cel_task_obj(v, q, "task_cls") :- unparen(v, k), kind(k, "call"), field(k, "function", f), cel_sym(f, q),
295
+ (cel_task_class_q(q) ; cel_run_def(f, q)).
296
+ cel_task_obj(v, nq, nk) :- unparen(v, k), cel_task_ref(k, nq, nk), kind(k, "call").
297
+ // A module name bound to a task object.
298
+ cel_is_if(q, "task", nq, nk) :- cel_module_var(q, v), cel_task_obj(v, nq, nk).
299
+ // cel_task_class_q(q): celery's Task; "task_cls" also holds of a class of the run deriving from it.
300
+ .decl cel_task_class_q(q: symbol)
301
+ cel_task_class_q("celery.Task"). cel_task_class_q("celery.app.task.Task").
302
+ cel_is_if(q, "task_cls", "", "") :- cel_task_class_q(q).
303
+ cel_is_if(q, "task_cls", b, "task_cls") :- class_def(c, q), cel_class_base(c, b).
304
+ .decl cel_send_method(m: symbol)
305
+ cel_send_method("delay"). cel_send_method("apply_async").
306
+ .decl cel_send(c: node, m: symbol, nq: symbol, nk: symbol)
307
+ cel_send(c, m, nq, nk) :- kind(c, "call"), field(c, "function", f), attr_of(f, o, m), cel_send_method(m),
308
+ unparen(o, t), cel_task_ref(t, nq, nk).
309
+ cel_send(c, "send_task", "", "") :- kind(c, "call"), field(c, "function", f), attr_of(f, _, "send_task").
310
+ .decl cel_call_method(m: symbol)
311
+ cel_call_method("delay"). cel_call_method("apply_async"). cel_call_method("s"). cel_call_method("si").
312
+ cel_call_method("signature").
313
+ // cel_task_call(c, q, m): t resolves to the run's def q, whose own file holds its node; a reader asks cel_is(q,
314
+ // "task"), and names the task by q's last segment.
315
+ .decl cel_task_call(c: node, q: symbol, m: symbol)
316
+ cel_task_call(c, q, m) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, o, m), cel_call_method(m),
317
+ unparen(o, t), cel_run_def(t, q).
318
+ // cel_task_arg(c, a): what the task receives. delay/s/si pass their arguments through, also through `*[...]` and
319
+ // `**{...}` spreads of literals; apply_async and send_task take `args` (a list or tuple, positionally after
320
+ // send_task's name) and `kwargs` (a dict literal or a `dict(...)` call).
321
+ .decl cel_task_arg(c: node, a: node)
322
+ cel_task_arg(c, a) :- cel_pass_through(c), call_arg(c, _, _, a).
323
+ cel_task_arg(c, a) :- cel_pass_through(c), field(c, "arguments", al), child(al, sp, _), kind(sp, "list_splat"),
324
+ child(sp, l, 0), unparen(l, s), (kind(s, "list") ; kind(s, "tuple")), child(s, a, _), !kind(a, "comment").
325
+ cel_task_arg(c, a) :- cel_pass_through(c), field(c, "arguments", al), child(al, sp, _), kind(sp, "dictionary_splat"),
326
+ child(sp, d, 0), unparen(d, m), cel_dict_value(m, a).
327
+ cel_task_arg(c, a) :- cel_args_list(c, l), unparen(l, s), (kind(s, "list") ; kind(s, "tuple")), child(s, a, _),
328
+ !kind(a, "comment").
329
+ cel_task_arg(c, a) :- cel_kwargs_dict(c, m), cel_dict_value(m, a).
330
+ .decl cel_pass_through(c: node)
331
+ cel_pass_through(c) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, m), (m = "delay" ; m = "s" ; m = "si").
332
+ // cel_dict_value(m, a): a is a value of dict m: a `{k: a}` literal's pair, or a `dict(k=a)` call's keyword.
333
+ .decl cel_dict_value(m: node, a: node)
334
+ cel_dict_value(m, a) :- kind(m, "dictionary"), child(m, p, _), kind(p, "pair"), field(p, "value", a).
335
+ cel_dict_value(m, a) :- kind(m, "call"), field(m, "function", f), sym_of(f, "builtins.dict"), call_arg(m, -1, _, a).
336
+ .decl cel_args_list(c: node, l: node)
337
+ cel_args_list(c, l) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, "apply_async"),
338
+ (posarg(c, 0, l) ; call_arg(c, -1, "args", l)).
339
+ cel_args_list(c, l) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, "send_task"),
340
+ (posarg(c, 1, l) ; call_arg(c, -1, "args", l)).
341
+ .decl cel_kwargs_dict(c: node, m: node)
342
+ cel_kwargs_dict(c, m) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, "apply_async"),
343
+ (posarg(c, 1, w) ; call_arg(c, -1, "kwargs", w)), unparen(w, m).
344
+ cel_kwargs_dict(c, m) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, "send_task"),
345
+ (posarg(c, 2, w) ; call_arg(c, -1, "kwargs", w)), unparen(w, m).
346
+ // cel_name_ref(e, n, how, nq, nk): a task named by a string.
347
+ .decl cel_name_ref(e: node, n: symbol, how: symbol, nq: symbol, nk: symbol)
348
+ cel_name_ref(e, n, "send_task", "", "") :- kind(c, "call"), field(c, "function", fn), attr_of(fn, _, "send_task"),
349
+ (posarg(c, 0, e) ; call_arg(c, -1, "name", e)), str(e, n).
350
+ cel_name_ref(e, n, "signature", "", "") :- kind(c, "call"), field(c, "function", fn), sym_of(fn, q),
351
+ (q = "celery.signature" ; q = "celery.canvas.signature" ; q = "celery.canvas.Signature" ; q = "celery.Signature"),
352
+ posarg(c, 0, e), str(e, n).
353
+ cel_name_ref(e, n, "signature", nq, nk) :- kind(c, "call"), field(c, "function", fn), attr_of(fn, o, "signature"),
354
+ unparen(o, u), cel_app_ref(u, nq, nk), posarg(c, 0, e), str(e, n).
355
+ // A beat entry is a dict with a "task" and a "schedule" key.
356
+ cel_name_ref(e, n, "beat", "", "") :- kind(p, "pair"), field(p, "key", k), str(k, "task"), field(p, "value", e),
357
+ str(e, n), child(m, p, _), kind(m, "dictionary"), child(m, p2, _), kind(p2, "pair"), field(p2, "key", k2),
358
+ str(k2, "schedule").
359
+
360
+ // ---- settings ----------------------------------------------------------------------------------------------------
361
+ // cel_key(k): the configuration keys the rules read (lowercase, Celery 4+). Add a key here before a rule reads it.
362
+ .decl cel_key(k: symbol)
363
+ cel_key("task_serializer"). cel_key("result_serializer"). cel_key("accept_content"). cel_key("result_accept_content").
364
+ cel_key("event_serializer"). cel_key("task_time_limit"). cel_key("task_soft_time_limit").
365
+ cel_key("broker_url"). cel_key("broker_transport_options"). cel_key("visibility_timeout").
366
+ cel_key("beat_schedule"). cel_key("imports"). cel_key("include"). cel_key("task_acks_late").
367
+ cel_key("task_always_eager"). cel_key("task_routes"). cel_key("timezone"). cel_key("enable_utc").
368
+ // cel_distinct_key(k): keys whose lowercase spelling names nothing but Celery's setting, so a bare assignment in a
369
+ // Celery-named class or module counts (`timezone` or `imports` alone would not).
370
+ .decl cel_distinct_key(k: symbol)
371
+ cel_distinct_key(k) :- cel_key(k), k != "imports", k != "include", k != "timezone", k != "enable_utc".
372
+ // cel_old_key(raw, k): pre-4.0 setting names that do not follow `CELERY_` + the key in capitals.
373
+ .decl cel_old_key(raw: symbol, k: symbol)
374
+ cel_old_key("CELERYD_TASK_TIME_LIMIT", "task_time_limit").
375
+ cel_old_key("CELERYD_TASK_SOFT_TIME_LIMIT", "task_soft_time_limit").
376
+ cel_old_key("CELERYBEAT_SCHEDULE", "beat_schedule"). cel_old_key("BROKER_TRANSPORT_OPTIONS", "broker_transport_options").
377
+ cel_old_key("BROKER_URL", "broker_url"). cel_old_key("CELERY_ALWAYS_EAGER", "task_always_eager").
378
+ cel_old_key("CELERY_ACKS_LATE", "task_acks_late").
379
+ // cel_raw_key(raw, k): an uppercase name raw is setting k: `CELERY_` + k in capitals, or an old name.
380
+ .decl cel_raw_key(raw: symbol, k: symbol)
381
+ cel_raw_key(raw, k) :- cel_key(k), raw = cat("CELERY_", @upper(k)).
382
+ cel_raw_key(raw, k) :- cel_old_key(raw, k).
383
+ .decl cel_setting(k: symbol, v: node, s: node)
384
+ // `CELERY_TASK_SERIALIZER = v` at module level or in a class body (a settings module, a config class).
385
+ cel_setting(k, v, a) :- kind(a, "assignment"), field(a, "left", l), kind(l, "identifier"), text(l, raw),
386
+ cel_raw_key(raw, k), field(a, "right", v).
387
+ // `task_serializer = v` in a class or module named for Celery (`class CeleryConfig:`, celeryconfig.py).
388
+ cel_setting(k, v, a) :- kind(a, "assignment"), field(a, "left", l), kind(l, "identifier"), text(l, k),
389
+ cel_distinct_key(k), field(a, "right", v), cel_config_site(a).
390
+ cel_setting(k, v, a) :- kind(a, "assignment"), field(a, "left", l), kind(l, "identifier"), text(l, k),
391
+ cel_key(k), field(a, "right", v), cel_config_site(a), nearest(a, c, scope), kind(c, "class_definition").
392
+ // `app.conf.k = v`, `app.conf["k"] = v`
393
+ cel_setting(k, v, a) :- kind(a, "assignment"), field(a, "left", l), attr_of(l, o, k), cel_key(k), attr_of(o, _, "conf"),
394
+ field(a, "right", v).
395
+ cel_setting(k, v, a) :- kind(a, "assignment"), field(a, "left", l), kind(l, "subscript"), field(l, "value", o),
396
+ attr_of(o, _, "conf"), field(l, "subscript", s), str(s, k), cel_key(k), field(a, "right", v).
397
+ // `app.conf.update(k=v)`, `app.conf.update({"k": v})`, `Celery(..., k=v)`
398
+ cel_setting(k, v, c) :- kind(c, "call"), field(c, "function", f), attr_of(f, o, "update"), attr_of(o, _, "conf"),
399
+ call_arg(c, -1, k, v), cel_key(k).
400
+ cel_setting(k, v, c) :- kind(c, "call"), field(c, "function", f), attr_of(f, o, "update"), attr_of(o, _, "conf"),
401
+ posarg(c, 0, m0), unparen(m0, m), kind(m, "dictionary"), child(m, p, _), kind(p, "pair"), field(p, "key", kn),
402
+ str(kn, k), cel_key(k), field(p, "value", v).
403
+ cel_setting(k, v, c) :- kind(c, "call"), field(c, "function", f), cel_app_cls_ref(f), call_arg(c, -1, k, v), cel_key(k).
404
+ // cel_config_site(a): a is in a class whose name holds "Celery"/"celery", or in a module whose file name does.
405
+ .decl cel_config_site(a: node)
406
+ cel_config_site(a) :- nearest(a, c, scope), kind(c, "class_definition"), def_name(c, x), icontains("celery", x).
407
+ cel_config_site(a) :- nearest(a, m, scope), kind(m, "module"), docof(a, d), file_name(d, fn), icontains("celery", fn).
408
+
409
+ // ---- SQLAlchemy --------------------------------------------------------------------------------------------------
410
+ // sa_decl_lib(q): declarative base classes outside the run. A class deriving from one is itself a base (`class
411
+ // Base(DeclarativeBase): pass`), not a model; a class deriving from that base is a model.
412
+ .decl sa_decl_lib(q: symbol)
413
+ sa_decl_lib("sqlalchemy.orm.DeclarativeBase"). sa_decl_lib("sqlalchemy.orm.DeclarativeBaseNoMeta").
414
+ sa_decl_lib("sqlalchemy.orm.decl_api.DeclarativeBase"). sa_decl_lib("sqlalchemy.orm.decl_api.DeclarativeBaseNoMeta").
415
+ // sa_model_lib(q): model classes outside the run: a class deriving from one is a model.
416
+ .decl sa_model_lib(q: symbol)
417
+ sa_model_lib("flask_appbuilder.Model"). sa_model_lib("flask_appbuilder.models.sqla.Model").
418
+ // sa_declarative_fn(q): functions that return a mapped base: `Base = declarative_base()`.
419
+ .decl sa_declarative_fn(q: symbol)
420
+ sa_declarative_fn("sqlalchemy.orm.declarative_base"). sa_declarative_fn("sqlalchemy.ext.declarative.declarative_base").
421
+ // "sa_root": a declarative base of the run: a class deriving from a sa_decl_lib class, or a module name bound to
422
+ // `declarative_base()` or to `registry().generate_base()`.
423
+ cel_is_if(q, "sa_root", "", "") :- class_def(c, q), cel_class_base(c, b), sa_decl_lib(b).
424
+ cel_is_if(q, "sa_root", "", "") :- cel_module_var(q, v), unparen(v, k), kind(k, "call"), field(k, "function", f),
425
+ sym_of(f, fq), sa_declarative_fn(fq).
426
+ cel_is_if(q, "sa_root", "", "") :- cel_module_var(q, v), unparen(v, k), kind(k, "call"), field(k, "function", f),
427
+ attr_of(f, _, "generate_base").
428
+ // "sa_flask_db": a module-level Flask-SQLAlchemy object, `db = SQLAlchemy(...)`, whose `db.Model` is a model base.
429
+ cel_is_if(q, "sa_flask_db", "", "") :- cel_module_var(q, v), unparen(v, k), kind(k, "call"), field(k, "function", f),
430
+ sym_of(f, fq), (fq = "flask_sqlalchemy.SQLAlchemy" ; fq = "flask_sqlalchemy.extension.SQLAlchemy").
431
+ // "sa_model": a class of the run with a mapped base among its bases, through the run's classes (a linear closure
432
+ // over symbols): a declarative base of the run, flask_appbuilder's Model, a Flask-SQLAlchemy `db.Model`, or a
433
+ // SQLModel class declared `table=True`. "sa_model_conc": such a class that is not `__abstract__`.
434
+ cel_is_if(q, "sa_model", b, "sa_root") :- class_def(c, q), cel_class_base(c, b).
435
+ cel_is_if(q, "sa_model", "", "") :- class_def(c, q), cel_class_base(c, b), sa_model_lib(b).
436
+ cel_is_if(q, "sa_model", dq, "sa_flask_db") :- class_def(c, q), cel_class_base(c, b), split_dot(b, dq, "Model").
437
+ cel_is_if(q, "sa_model", b, "sa_model") :- class_def(c, q), cel_class_base(c, b).
438
+ cel_is_if(q, "sa_model", "", "") :- class_def(c, q), class_base(c, "sqlmodel.SQLModel"), sa_table_kw(c).
439
+ cel_is_if(q, "sa_model_conc", q, "sa_model") :- class_def(c, q), !sa_abstract(c).
440
+ .decl sa_table_kw(c: node)
441
+ sa_table_kw(c) :- kind(c, "class_definition"), field(c, "superclasses", al), child(al, ka, _),
442
+ kind(ka, "keyword_argument"), field(ka, "name", n), text(n, "table"), field(ka, "value", v), kind(v, "true").
443
+ // sa_model(c, nq, nk): class c maps a table when its symbol is a "sa_model".
444
+ .decl sa_model(c: node, nq: symbol, nk: symbol)
445
+ sa_model(c, q, "sa_model") :- class_def(c, q), !sa_abstract(c).
446
+ // `__abstract__ = True` in the body: a base that maps no table.
447
+ .decl sa_abstract(c: node)
448
+ sa_abstract(c) :- kind(c, "class_definition"), field(c, "body", b), body_stmt(b, s, _), kind(s, "expression_statement"),
449
+ child(s, a, 0), kind(a, "assignment"), field(a, "left", l), text(l, "__abstract__"), field(a, "right", v),
450
+ kind(v, "true").
451
+ // sa_model_ref(e, nq, nk): e names a class of the run that maps a table.
452
+ .decl sa_model_ref(e: node, nq: symbol, nk: symbol)
453
+ sa_model_ref(e, q, "sa_model_conc") :- sym_of(e, q), cel_run_def(e, q).
454
+
455
+ // sa_session(e, nq, nk): e is a SQLAlchemy session, proven (sa_session_proven) or by its name (sa_session_guess). A
456
+ // proven session is a session either way, so the guess need not exclude it.
457
+ .decl sa_session(e: node, nq: symbol, nk: symbol)
458
+ sa_session(e, nq, nk) :- sa_session_proven(e, nq, nk).
459
+ sa_session(e, "", "sa_run") :- sa_session_guess(e).
460
+ // sa_session_class(q): SQLAlchemy's and SQLModel's session classes. sa_session_cls_ref(m): m names one, or a class of
461
+ // the run deriving from one.
462
+ .decl sa_session_class(q: symbol)
463
+ sa_session_class("sqlalchemy.orm.Session"). sa_session_class("sqlalchemy.orm.session.Session").
464
+ sa_session_class("sqlalchemy.ext.asyncio.AsyncSession"). sa_session_class("sqlalchemy.orm.scoped_session").
465
+ sa_session_class("sqlmodel.Session"). sa_session_class("sqlalchemy.ext.asyncio.async_scoped_session").
466
+ sa_session_class("sqlalchemy.ext.asyncio.session.AsyncSession"). sa_session_class("sqlmodel.ext.asyncio.session.AsyncSession").
467
+ sa_session_class("sqlalchemy.orm.scoping.scoped_session").
468
+ .decl sa_session_cls_ref(m: node)
469
+ sa_session_cls_ref(m) :- cel_derives(m, b), sa_session_class(b).
470
+ // sa_maker_fn(q): functions returning a session factory, whose call (or, for scoped_session, the registry itself)
471
+ // gives a session.
472
+ .decl sa_maker_fn(q: symbol)
473
+ sa_maker_fn("sqlalchemy.orm.sessionmaker"). sa_maker_fn("sqlalchemy.orm.session.sessionmaker").
474
+ sa_maker_fn("sqlalchemy.ext.asyncio.async_sessionmaker"). sa_maker_fn("sqlalchemy.orm.scoped_session").
475
+ sa_maker_fn("sqlalchemy.ext.asyncio.async_scoped_session").
476
+ // sa_maker_value(v): v is a session factory: `sessionmaker(...)`, `async_sessionmaker(...)`, `scoped_session(...)`.
477
+ .decl sa_maker_value(v: node)
478
+ sa_maker_value(v) :- unparen(v, k), kind(k, "call"), field(k, "function", f), sym_of(f, q), sa_maker_fn(q).
479
+ // "sa_maker": a module variable bound to a session factory; "sa_scoped": one bound to a scoped_session registry,
480
+ // itself a session.
481
+ cel_is_if(q, "sa_maker", "", "") :- cel_module_var(q, v), sa_maker_value(v).
482
+ cel_is_if(q, "sa_scoped", "", "") :- cel_module_var(q, v), unparen(v, k), field(k, "function", f), sym_of(f, fq),
483
+ (fq = "sqlalchemy.orm.scoped_session" ; fq = "sqlalchemy.ext.asyncio.async_scoped_session").
484
+ // sa_maker_ref(e, nq, nk): e names a session factory: a module variable or a local bound to one.
485
+ .decl sa_maker_ref(e: node, nq: symbol, nk: symbol)
486
+ sa_maker_ref(e, q, "sa_maker") :- cel_run_var(e, q).
487
+ sa_maker_ref(e, "", "") :- kind(e, "identifier"), text(e, x), look(e, x, s), !kind(s, "module"), asg(s, x, v, _),
488
+ sa_maker_value(v).
489
+ // sa_session_value(v, nq, nk): v evaluates to a session: a session class's constructor, a session factory's call, a
490
+ // scoped_session registry, Flask-SQLAlchemy's `db.session`, `<factory>.begin()` (a session in a transaction),
491
+ // `await` of one, or a call of a run function that returns or yields one (a FastAPI dependency `get_db`).
492
+ .decl sa_session_value(v: node, nq: symbol, nk: symbol)
493
+ sa_session_value(v, "", "") :- unparen(v, k), kind(k, "call"), field(k, "function", f), sa_session_cls_ref(f).
494
+ sa_session_value(v, nq, nk) :- unparen(v, k), kind(k, "call"), field(k, "function", f), unparen(f, u),
495
+ sa_maker_ref(u, nq, nk).
496
+ sa_session_value(v, nq, nk) :- unparen(v, k), kind(k, "call"), field(k, "function", f), attr_of(f, o, "begin"),
497
+ unparen(o, u), sa_maker_ref(u, nq, nk).
498
+ sa_session_value(v, q, "sa_scoped") :- unparen(v, u), cel_run_var(u, q).
499
+ sa_session_value(v, q, "sa_flask_db") :- unparen(v, k), attr_of(k, o, "session"), unparen(o, u), cel_run_var(u, q).
500
+ sa_session_value(v, nq, nk) :- unparen(v, k), kind(k, "await"), child(k, c, 0), sa_session_value(c, nq, nk).
501
+ sa_session_value(v, q, "sa_session_fn") :- unparen(v, k), kind(k, "call"), field(k, "function", f), cel_run_def(f, q).
502
+ // "sa_session_fn": a run function that returns or yields a session (or a local name bound to one).
503
+ cel_is_if(gq, "sa_session_fn", nq, nk) :- (kind(r, "return_statement") ; kind(r, "yield")), child(r, v, 0),
504
+ nearest(r, g, fdef), kind(g, "function_definition"), sa_session_value(v, nq, nk), qualname(g, gq).
505
+ cel_is_if(gq, "sa_session_fn", nq, nk) :- (kind(r, "return_statement") ; kind(r, "yield")), child(r, v, 0),
506
+ nearest(r, g, fdef), kind(g, "function_definition"), unparen(v, u), kind(u, "identifier"), text(u, x),
507
+ asg(g, x, w, _), sa_session_value(w, nq, nk), qualname(g, gq).
508
+ // "sa_session_var": a module variable bound to a session (`db_session = scoped_session(...)`); "sa_session_alias":
509
+ // one bound to an annotation naming a session class (`SessionDep = Annotated[Session, Depends(get_db)]`).
510
+ cel_is_if(q, "sa_session_var", nq, nk) :- cel_module_var(q, v), sa_session_value(v, nq, nk).
511
+ cel_is_if(q, "sa_session_alias", "", "") :- cel_module_var(q, v), (n = v ; within(n, v)), sa_session_cls_ref(n).
512
+ // sa_session_proven(e, nq, nk): e is a session by what binds it: a session value; a name its own scope binds to one
513
+ // (an assignment, or `with <value> as name`); a module variable bound to one ("sa_session_var"); a parameter
514
+ // annotated with a session class, directly, inside the annotation (`Annotated[Session, Depends(...)]`) or through a
515
+ // module alias of one ("sa_session_alias").
516
+ .decl sa_session_proven(e: node, nq: symbol, nk: symbol)
517
+ sa_session_proven(e, nq, nk) :- sa_session_value(e, nq, nk).
518
+ sa_session_proven(e, nq, nk) :- kind(e, "identifier"), text(e, x), look(e, x, s), asg(s, x, v, _),
519
+ sa_session_value(v, nq, nk).
520
+ sa_session_proven(e, nq, nk) :- kind(e, "identifier"), sa_with_alias(e, v), sa_session_value(v, nq, nk).
521
+ sa_session_proven(e, nq, nk) :- kind(e, "identifier"), text(e, x), look(e, x, f), sig_param(f, _, nm, _, _, t, _),
522
+ text(nm, x), !unset(t), sa_session_ann(t, nq, nk).
523
+ // An imported module variable bound to one (`from app.db import db_session`).
524
+ sa_session_proven(e, q, "sa_session_var") :- cel_run_var(e, q).
525
+ // `self.x`, where a method of the class assigns `self.x = v` and v is a proven session (an annotated `__init__`
526
+ // parameter, a factory call).
527
+ sa_session_proven(e, nq, nk) :- attr_of(e, o, x), unparen(o, s), text(s, "self"), nearest(e, f, fdef),
528
+ nearest(f, c, scope), kind(c, "class_definition"), within(a, c), kind(a, "assignment"), field(a, "left", l),
529
+ attr_of(l, so, x), text(so, "self"), field(a, "right", v), unparen(v, u), u != e, sa_session_proven(u, nq, nk).
530
+ .decl sa_session_ann(t: node, nq: symbol, nk: symbol)
531
+ sa_session_ann(t, "", "") :- sa_session_ann_here(t).
532
+ sa_session_ann(t, q, "sa_session_alias") :- (m = t ; within(m, t)), cel_run_var(m, q).
533
+ // sa_session_ann_here(t): annotation t names a session class itself.
534
+ .decl sa_session_ann_here(t: node)
535
+ sa_session_ann_here(t) :- (m = t ; within(m, t)), sa_session_cls_ref(m).
536
+ // sa_with_alias(e, v): name e is the alias of `with v as e` and is read in that with-statement's body.
537
+ .decl sa_with_alias(e: node, v: node)
538
+ sa_with_alias(e, v) :- kind(e, "identifier"), text(e, x), kind(wi, "with_item"), field(wi, "value", ap),
539
+ kind(ap, "as_pattern"), child(ap, v, 0), field(ap, "alias", al), text(al, x), nearest(wi, ws, "with_statement"),
540
+ field(ws, "body", b), within(e, b).
541
+ // sa_session_guess(e): the fallback by name, for a value the engine cannot follow: a name ending in
542
+ // "session"/"Session" (`session`, `db_session`; not `session_id`) that its scope binds to nothing known not to be a
543
+ // session (an unannotated parameter, a call of something that does not resolve), or such an attribute
544
+ // (`self.session`). It counts in a run that imports SQLAlchemy (or SQLModel, Flask-SQLAlchemy): sa_session's need
545
+ // ("", "sa_run"). A name is all the engine has here.
546
+ .decl sa_session_guess(e: node)
547
+ sa_session_guess(e) :- kind(e, "identifier"), text(e, x), (suffix("session", x) ; suffix("Session", x)),
548
+ !sa_not_session(e).
549
+ sa_session_guess(e) :- attr_of(e, _, x), (suffix("session", x) ; suffix("Session", x)), !sa_not_session(e).
550
+ .decl sa_lib_module(m: symbol)
551
+ sa_lib_module(m) :- import_alias(_, _, m, _, _), (m = "sqlalchemy" ; prefix("sqlalchemy.", m) ; m = "sqlmodel" ;
552
+ prefix("sqlmodel.", m) ; m = "flask_sqlalchemy" ; prefix("flask_sqlalchemy.", m)).
553
+ sa_lib_module(m) :- kind(i, "import_from_statement"), module_text(i, m), (m = "sqlalchemy" ;
554
+ prefix("sqlalchemy.", m) ; m = "sqlmodel" ; prefix("sqlmodel.", m) ; m = "flask_sqlalchemy" ;
555
+ prefix("flask_sqlalchemy.", m)).
556
+ // sa_not_session(e): what binds e is known and is not a session: a parameter annotated with a type that names no
557
+ // session class, a local or `self.x` assignment, or a with-alias, whose value is not a session (sa_other). An
558
+ // annotation through a module alias of a session class is a proven session (sa_session_proven), whatever this says.
559
+ .decl sa_not_session(e: node)
560
+ sa_not_session(e) :- kind(e, "identifier"), text(e, x), look(e, x, s), asg(s, x, v, _), sa_other(v).
561
+ sa_not_session(e) :- kind(e, "identifier"), sa_with_alias(e, v), sa_other(v).
562
+ sa_not_session(e) :- kind(e, "identifier"), text(e, x), look(e, x, f), sig_param(f, _, nm, _, _, t, _), text(nm, x),
563
+ !unset(t), !sa_session_ann_here(t), !sa_ann_opaque(t).
564
+ sa_not_session(e) :- attr_of(e, o, x), unparen(o, s), text(s, "self"), nearest(e, f, fdef), nearest(f, c, scope),
565
+ kind(c, "class_definition"), within(a, c), kind(a, "assignment"), field(a, "left", l), attr_of(l, so, x),
566
+ text(so, "self"), field(a, "right", v), sa_self_other(v).
567
+ // sa_self_other(v): a `self.x = v` value known not to be a session: sa_other, or a parameter known not to be one.
568
+ .decl sa_self_other(v: node)
569
+ sa_self_other(v) :- sa_other(v).
570
+ sa_self_other(v) :- unparen(v, u), kind(u, "identifier"), sa_not_session(u).
571
+ // sa_ann_opaque(t): an annotation that resolves to nothing (a string, an unresolved name): it says nothing.
572
+ .decl sa_ann_opaque(t: node)
573
+ sa_ann_opaque(t) :- kind(t, "type"), child(t, m, 0), kind(m, "string").
574
+ sa_ann_opaque(t) :- kind(t, "type"), child(t, m, 0), kind(m, "identifier"), !sym_of(m, _), !cel_sym(m, _).
575
+ // sa_other(v): v is known not to be a session: a literal, container, comprehension, subscript or arithmetic, or a
576
+ // call of something that resolves outside the run (a library function or class: no def, class or module variable of
577
+ // the run, cel_run_sym) and is no session class, session factory or SQLAlchemy call returning a session.
578
+ .decl sa_other(v: node)
579
+ sa_other(v) :- unparen(v, u), kind(u, k), sa_value_kind(k).
580
+ sa_other(v) :- unparen(v, u), kind(u, "call"), field(u, "function", f), sym_of(f, q), !cel_run_sym(f, q),
581
+ !sa_session_class(q), !sa_maker_fn(q), !prefix("sqlalchemy.", q), !prefix("sqlmodel.", q),
582
+ !prefix("flask_sqlalchemy.", q).
583
+ .decl sa_value_kind(k: symbol)
584
+ sa_value_kind("string"). sa_value_kind("concatenated_string"). sa_value_kind("integer"). sa_value_kind("float").
585
+ sa_value_kind("dictionary"). sa_value_kind("list"). sa_value_kind("tuple"). sa_value_kind("set").
586
+ sa_value_kind("subscript"). sa_value_kind("dictionary_comprehension"). sa_value_kind("list_comprehension").
587
+ sa_value_kind("set_comprehension"). sa_value_kind("true"). sa_value_kind("false"). sa_value_kind("none").
588
+ sa_value_kind("binary_operator"). sa_value_kind("lambda").
589
+ .decl sa_session_call(c: node, s: node, m: symbol, nq: symbol, nk: symbol)
590
+ sa_session_call(c, s, m, nq, nk) :- kind(c, "call"), field(c, "function", f), attr_of(f, o, m), unparen(o, s),
591
+ sa_session(s, nq, nk).
592
+ // sa_session_call through `await`: `await session.flush()` is the call itself; nothing to add.
593
+ .decl sa_write_method(m: symbol)
594
+ sa_write_method("add"). sa_write_method("add_all"). sa_write_method("delete"). sa_write_method("merge").
595
+ sa_write_method("flush"). sa_write_method("bulk_save_objects"). sa_write_method("bulk_insert_mappings").
596
+ sa_write_method("bulk_update_mappings").
597
+ .decl sa_dml_fn(q: symbol)
598
+ sa_dml_fn("sqlalchemy.insert"). sa_dml_fn("sqlalchemy.update"). sa_dml_fn("sqlalchemy.delete").
599
+ sa_dml_fn("sqlalchemy.dialects.postgresql.insert"). sa_dml_fn("sqlalchemy.sql.expression.insert").
600
+ sa_dml_fn("sqlalchemy.sql.expression.update"). sa_dml_fn("sqlalchemy.sql.expression.delete").
601
+ sa_dml_fn("sqlmodel.insert"). sa_dml_fn("sqlmodel.update"). sa_dml_fn("sqlmodel.delete").
602
+ .decl sa_write(c: node, nq: symbol, nk: symbol)
603
+ sa_write(c, nq, nk) :- sa_session_call(c, _, m, nq, nk), sa_write_method(m).
604
+ sa_write(c, nq, nk) :- sa_session_call(c, _, "execute", nq, nk), posarg(c, 0, q), sa_dml(q).
605
+ // sa_dml(q): an insert/update/delete statement: `update(M).where(...).values(...)`, a chain from one.
606
+ .decl sa_dml(q: node)
607
+ sa_dml(q) :- unparen(q, k), kind(k, "call"), field(k, "function", f), sym_of(f, fq), sa_dml_fn(fq).
608
+ sa_dml(q) :- unparen(q, k), kind(k, "call"), field(k, "function", f), attr_of(f, o, _), sa_dml(o).
609
+ .decl sa_commit(c: node, nq: symbol, nk: symbol)
610
+ sa_commit(c, nq, nk) :- sa_session_call(c, _, "commit", nq, nk).
611
+ // sa_tx_with(w, b): `with x.begin():`/`with x.begin() as s:`, x a session, sessionmaker or engine (any receiver:
612
+ // `.begin()` in a with-item opens a transaction in SQLAlchemy's every API), and `with Session.begin()`.
613
+ .decl sa_tx_with(w: node, b: node)
614
+ sa_tx_with(w, b) :- kind(w, "with_statement"), child(w, wc, _), kind(wc, "with_clause"), child(wc, wi, _),
615
+ kind(wi, "with_item"), field(wi, "value", v), sa_begin_value(v), field(w, "body", b).
616
+ .decl sa_begin_value(v: node)
617
+ sa_begin_value(v) :- unparen(v, k), kind(k, "call"), field(k, "function", f), attr_of(f, _, "begin").
618
+ sa_begin_value(v) :- kind(v, "as_pattern"), child(v, e, 0), sa_begin_value(e).
619
+
620
+ // sa_instance(e, nq, nk): e holds one model instance.
621
+ .decl sa_instance(e: node, nq: symbol, nk: symbol)
622
+ sa_instance(a, nq, nk) :- unparen(a, e), sa_instance_value(e, nq, nk).
623
+ sa_instance(a, nq, nk) :- unparen(a, e), kind(e, "identifier"), text(e, x), look(e, x, f), asg(f, x, v, _),
624
+ unparen(v, w), sa_instance_value(w, nq, nk).
625
+ sa_instance(a, t, "sa_model") :- unparen(a, e), proven_type(e, t).
626
+ sa_instance(a, q, "sa_model") :- unparen(a, e), kind(e, "identifier"), text(e, x), look(e, x, f),
627
+ sig_param(f, _, nm, _, _, t, _), text(nm, x), (m = t ; child(t, m, 0)), sym_of(m, q).
628
+ .decl sa_instance_value(v: node, nq: symbol, nk: symbol)
629
+ // `M(...)`
630
+ sa_instance_value(v, nq, nk) :- kind(v, "call"), field(v, "function", f), sa_model_ref(f, nq, nk).
631
+ // `await x` of one
632
+ sa_instance_value(v, nq, nk) :- kind(v, "await"), child(v, c, 0), unparen(c, w), sa_instance_value(w, nq, nk).
633
+ // `session.get(M, pk)`, `db.get_or_404(M, pk)`, `db.first_or_404(...)` aside
634
+ sa_instance_value(v, nq, nk) :- kind(v, "call"), field(v, "function", f), attr_of(f, _, m),
635
+ (m = "get" ; m = "get_one" ; m = "get_or_404"), posarg(v, 0, r), sa_model_ref(r, nq, nk).
636
+ // `M.query.get(1)`, `M.query.filter_by(...).first()`, `session.query(M).filter(...).one()`
637
+ sa_instance_value(v, nq, nk) :- kind(v, "call"), field(v, "function", f), attr_of(f, o, m), sa_row_method(m),
638
+ sa_query_of(o, nq, nk).
639
+ // `session.scalar(select(M).where(...))`, `session.execute(select(M)).scalar_one()`
640
+ sa_instance_value(v, nq, nk) :- kind(v, "call"), field(v, "function", f), attr_of(f, _, "scalar"), posarg(v, 0, s),
641
+ sa_select_model(s, nq, nk).
642
+ sa_instance_value(v, nq, nk) :- kind(v, "call"), field(v, "function", f), attr_of(f, o, m), sa_scalar_row_method(m),
643
+ unparen(o, x), kind(x, "call"), field(x, "function", xf), attr_of(xf, _, xm), (xm = "execute" ; xm = "scalars"),
644
+ posarg(x, 0, s), sa_select_model(s, nq, nk).
645
+ .decl sa_row_method(m: symbol)
646
+ sa_row_method("get"). sa_row_method("first"). sa_row_method("one"). sa_row_method("one_or_none").
647
+ sa_row_method("get_or_404"). sa_row_method("first_or_404"). sa_row_method("one_or_404").
648
+ .decl sa_scalar_row_method(m: symbol)
649
+ sa_scalar_row_method("scalar_one"). sa_scalar_row_method("scalar_one_or_none"). sa_scalar_row_method("first").
650
+ sa_scalar_row_method("one"). sa_scalar_row_method("one_or_none"). sa_scalar_row_method("scalar").
651
+ // sa_query_of(q, nq, nk): a legacy query over one model: `M.query`, `session.query(M)`, and chains of them.
652
+ .decl sa_query_of(q: node, nq: symbol, nk: symbol)
653
+ sa_query_of(q, nq, nk) :- unparen(q, e), attr_of(e, o, "query"), unparen(o, r), sa_model_ref(r, nq, nk).
654
+ sa_query_of(q, nq, nk) :- unparen(q, e), kind(e, "call"), field(e, "function", f), attr_of(f, _, "query"),
655
+ posarg(e, 0, r), !posarg(e, 1, _), sa_model_ref(r, nq, nk).
656
+ sa_query_of(q, nq, nk) :- unparen(q, e), kind(e, "call"), field(e, "function", f), attr_of(f, o, m),
657
+ sa_query_method(m), sa_query_of(o, nq, nk).
658
+ .decl sa_query_method(m: symbol)
659
+ sa_query_method("filter"). sa_query_method("filter_by"). sa_query_method("order_by"). sa_query_method("where").
660
+ sa_query_method("options"). sa_query_method("join"). sa_query_method("outerjoin"). sa_query_method("limit").
661
+ sa_query_method("offset"). sa_query_method("with_for_update").
662
+ // sa_select_model(s, nq, nk): `select(M)` (one entity) and chains of it.
663
+ .decl sa_select_model(s: node, nq: symbol, nk: symbol)
664
+ sa_select_model(s, nq, nk) :- unparen(s, e), kind(e, "call"), field(e, "function", f), sym_of(f, fq),
665
+ (fq = "sqlalchemy.select" ; fq = "sqlmodel.select" ; fq = "sqlalchemy.sql.expression.select" ;
666
+ fq = "sqlalchemy.future.select"), posarg(e, 0, r), !posarg(e, 1, _), sa_model_ref(r, nq, nk).
667
+ sa_select_model(s, nq, nk) :- unparen(s, e), kind(e, "call"), field(e, "function", f), attr_of(f, o, m),
668
+ sa_query_method(m), sa_select_model(o, nq, nk).
669
+
670
+ // ---- files -------------------------------------------------------------------------------------------------------
671
+ .decl cel_test_doc(d: number)
672
+ cel_test_doc(d) :- doc(d, _, _), (path_part(d, "tests") ; path_part(d, "test") ; path_part(d, "testing") ;
673
+ path_part(d, "unittests")).
674
+ cel_test_doc(d) :- file_name(d, s), (glob("test_*.py", s) ; glob("*_tests.py", s) ; glob("*_test.py", s) ;
675
+ s = "tests.py" ; s = "test.py" ; s = "conftest.py").
676
+ .decl cel_django_doc(d: number)
677
+ cel_django_doc(d) :- import_alias(i, _, m, _, _), (m = "django" ; prefix("django.", m)), docof(i, d).
678
+ cel_django_doc(d) :- kind(i, "import_from_statement"), module_text(i, m), (m = "django" ; prefix("django.", m)),
679
+ docof(i, d).
680
+ .decl cel_celery_doc(d: number)
681
+ cel_celery_doc(d) :- import_alias(i, _, m, _, _), (m = "celery" ; prefix("celery.", m)), docof(i, d).
682
+ cel_celery_doc(d) :- kind(i, "import_from_statement"), module_text(i, m), (m = "celery" ; prefix("celery.", m)),
683
+ docof(i, d).