Skip to content

Commit 35598e6

Browse files
committed
test(python): docstrings and __future__ imports keep the package namespace
A string-literal statement has no effect and from __future__ import x binds x, but the namespace facts recorded both as unknown statements: a triple-quoted string as an EFFECT, a future import as UNSUPPORTED. sqlalchemy's sql/expression.py lost its 149 re-exports to a docstring after nullsfirst = nulls_first. The extraction test pins every string form (module, class and attribute docstrings, single and triple quotes, r/b/rb/u prefixes, implicit concatenation, an f-string without a replacement field) and the future import forms (alias, parentheses); controls: an f-string with a field (also inside a concatenation), a call, and a star import from __future__ stay barriers. The pipeline fixture adds the expression.py shape end to end and an f-string control package. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 7b5e680 commit 35598e6

2 files changed

Lines changed: 109 additions & 1 deletion

File tree

‎tests/test_extraction.c‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12242,6 +12242,95 @@ TEST(python_namespace_facts_type_checking_alternatives) {
1224212242
PASS();
1224312243
}
1224412244

12245+
/* A string-literal statement (a module, class or attribute docstring) has no
12246+
* effect, and `from __future__ import x` binds x like any from-import. Neither
12247+
* may withdraw the names bound before it: sqlalchemy's sql/expression.py lost
12248+
* its 149 re-exports to `nullsfirst = nulls_first` followed by a triple-quoted
12249+
* docstring. An f-string with a replacement field can run code
12250+
* (__format__ / __str__) and stays an effect. */
12251+
static bool py_facts_any_unknown(const CBMFileResult *r, CBMPyNamespaceUnknownReason reason) {
12252+
for (int i = 0; r && i < r->py_namespace.count; i++) {
12253+
if (r->py_namespace.items[i].kind == CBM_PY_NS_UNKNOWN &&
12254+
r->py_namespace.items[i].reason == reason)
12255+
return true;
12256+
}
12257+
return false;
12258+
}
12259+
12260+
TEST(python_namespace_facts_string_statements_and_future_imports) {
12261+
static const char source[] = "\"\"\"Module docstring.\"\"\"\n"
12262+
"from __future__ import annotations\n"
12263+
"from __future__ import (division as div, generator_stop)\n"
12264+
"from .impl import a\n"
12265+
"\"\"\"A triple-quoted string after an import.\"\"\"\n"
12266+
"'''Single-quoted triple.'''\n"
12267+
"r\"raw \\d\"\n"
12268+
"b\"bytes\"\n"
12269+
"rb'raw bytes'\n"
12270+
"u\"unicode\"\n"
12271+
"\"implicit\" 'concatenation'\n"
12272+
"f\"no replacement field {{escaped}}\"\n"
12273+
"from .impl import b\n"
12274+
"class Documented:\n"
12275+
" \"\"\"Class docstring.\"\"\"\n"
12276+
" def method(self):\n"
12277+
" pass\n"
12278+
"x = a\n"
12279+
"\"\"\"Attribute docstring.\"\"\"\n";
12280+
CBMFileResult *r = extract(source, CBM_LANG_PYTHON, "t", "pkg/__init__.py");
12281+
bool ok = r && !r->has_error && r->py_namespace.status == CBM_PY_NS_COMPLETE &&
12282+
cbm_py_namespace_facts_valid(&r->py_namespace) && r->py_namespace.count == 7;
12283+
if (ok) {
12284+
const CBMPyNamespaceFact *f = r->py_namespace.items;
12285+
ok = f[0].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[0].module_name, "__future__") == 0 &&
12286+
f[0].relative_level == 0 && strcmp(f[0].member_name, "annotations") == 0 &&
12287+
strcmp(f[0].local_name, "annotations") == 0 && f[1].kind == CBM_PY_NS_IMPORT_NAME &&
12288+
strcmp(f[1].member_name, "division") == 0 && strcmp(f[1].local_name, "div") == 0 &&
12289+
f[2].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[2].local_name, "generator_stop") == 0 &&
12290+
f[3].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[3].local_name, "a") == 0 &&
12291+
f[4].kind == CBM_PY_NS_IMPORT_NAME && strcmp(f[4].local_name, "b") == 0 &&
12292+
f[5].kind == CBM_PY_NS_OWN_DEF && f[5].def_kind == CBM_PY_NS_DEF_CLASS &&
12293+
strcmp(f[5].local_name, "Documented") == 0 && f[6].kind == CBM_PY_NS_SHADOW &&
12294+
strcmp(f[6].local_name, "x") == 0;
12295+
}
12296+
if (!ok && r) {
12297+
for (int i = 0; i < r->py_namespace.count; i++) {
12298+
const CBMPyNamespaceFact *f = &r->py_namespace.items[i];
12299+
printf(" string/future fact %d: kind %d reason %d name %s\n", i, (int)f->kind,
12300+
(int)f->reason, f->local_name ? f->local_name : "-");
12301+
}
12302+
}
12303+
if (r)
12304+
cbm_free_result(r);
12305+
ASSERT_TRUE(ok); /* RED while a docstring is an effect and __future__ is unsupported */
12306+
12307+
/* Controls: each stays a namespace barrier. */
12308+
static const struct {
12309+
const char *name;
12310+
const char *source;
12311+
CBMPyNamespaceUnknownReason reason;
12312+
} barriers[] = {
12313+
{"f-string field", "from .impl import a\nf\"{a}\"\n", CBM_PY_NS_REASON_EFFECT},
12314+
{"concatenated f-string field", "from .impl import a\n\"x\" f\"{a!r}\"\n",
12315+
CBM_PY_NS_REASON_EFFECT},
12316+
{"call", "from .impl import a\nprint(a)\n", CBM_PY_NS_REASON_EFFECT},
12317+
{"star from __future__", "from .impl import a\nfrom __future__ import *\n",
12318+
CBM_PY_NS_REASON_UNSUPPORTED},
12319+
};
12320+
for (size_t i = 0; i < sizeof(barriers) / sizeof(barriers[0]); i++) {
12321+
r = extract(barriers[i].source, CBM_LANG_PYTHON, "t", "pkg/__init__.py");
12322+
ok = r && r->py_namespace.status == CBM_PY_NS_COMPLETE &&
12323+
cbm_py_namespace_facts_valid(&r->py_namespace) &&
12324+
py_facts_any_unknown(r, barriers[i].reason);
12325+
if (!ok)
12326+
printf(" control '%s' is no namespace barrier\n", barriers[i].name);
12327+
if (r)
12328+
cbm_free_result(r);
12329+
ASSERT_TRUE(ok);
12330+
}
12331+
PASS();
12332+
}
12333+
1224512334
TEST(python_namespace_facts_distinguish_all_operations_and_unknowns) {
1224612335
static const char source[] = "__all__ = ['old']; __all__ = []\n"
1224712336
"__all__ = dynamic\n"
@@ -12629,6 +12718,7 @@ SUITE(extraction) {
1262912718
RUN_TEST(python_namespace_facts_unknown_effects_and_body_stability);
1263012719
RUN_TEST(python_namespace_facts_parse_errors_are_complete_unknown);
1263112720
RUN_TEST(python_namespace_facts_type_checking_alternatives);
12721+
RUN_TEST(python_namespace_facts_string_statements_and_future_imports);
1263212722
RUN_TEST(python_namespace_facts_capture_and_copy_fail_atomically);
1263312723
RUN_TEST(python_namespace_facts_validation_rejects_partial_and_unknown_schema);
1263412724
RUN_TEST(python_namespace_facts_compact_spill_and_copy_keep_owned_payload);

‎tests/test_pipeline.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14308,6 +14308,7 @@ static const char *const py_reexport_calls[][2] = {
1430814308
{"ctl_from_cls", "direct.impl.Direct"},
1430914309
{"ctl_from_fn", "direct.impl.direct_fn"},
1431014310
{"lazy_tc", "lz.lazy_mod.Lazy"},
14311+
{"doc_reexport", "docp.impl.Doc"},
1431114312
};
1431214313
enum { PY_REEXPORT_N_CALLS = sizeof(py_reexport_calls) / sizeof(py_reexport_calls[0]) };
1431314314

@@ -14317,7 +14318,7 @@ enum { PY_REEXPORT_N_CALLS = sizeof(py_reexport_calls) / sizeof(py_reexport_call
1431714318
static const char *const py_reexport_excluded[][2] = {
1431814319
{"excl_unlisted", "pkg.all_mod.Unlisted"}, {"excl_private", "pkg.star_mod._private_fn"},
1431914320
{"excl_shadowed", "shad.impl.Gone"}, {"excl_barrier", "barr.impl.Before"},
14320-
{"excl_compat", "cmp.types_only.Shim"},
14321+
{"excl_compat", "cmp.types_only.Shim"}, {"excl_fstring", "fsp.impl.Kept"},
1432114322
};
1432214323
enum { PY_REEXPORT_N_EXCLUDED = sizeof(py_reexport_excluded) / sizeof(py_reexport_excluded[0]) };
1432314324

@@ -14377,13 +14378,28 @@ static int py_reexport_run(bool parallel, bool incremental, bool *lsp, bool *exc
1437714378
" except ImportError:\n Shim = None\n");
1437814379
write_temp_file(tmp, "app/cmp/types_only.py", "class Shim:\n pass\n");
1437914380
write_temp_file(tmp, "app/cmp/runtime_impl.py", "class Shim:\n pass\n");
14381+
/* The sql/expression.py shape of sqlalchemy: docstrings and a __future__
14382+
* import around the re-exports; an f-string with a field stays a barrier. */
14383+
write_temp_file(tmp, "app/docp/__init__.py",
14384+
"\"\"\"Package docstring.\"\"\"\n"
14385+
"from __future__ import annotations\n"
14386+
"from .impl import Doc\n"
14387+
"alias = Doc\n"
14388+
"\"\"\"Attribute docstring.\"\"\"\n");
14389+
write_temp_file(tmp, "app/docp/impl.py", "class Doc:\n pass\n");
14390+
write_temp_file(tmp, "app/fsp/__init__.py",
14391+
"from .impl import Kept\n"
14392+
"f\"{Kept}\"\n");
14393+
write_temp_file(tmp, "app/fsp/impl.py", "class Kept:\n pass\n");
1438014394
write_temp_file(tmp, "app/main.py",
1438114395
"from app import pkg\n"
1438214396
"from app import cyc\n"
1438314397
"from app import shad\n"
1438414398
"from app import barr\n"
1438514399
"from app import lz\n"
1438614400
"from app import cmp\n"
14401+
"from app import docp\n"
14402+
"from app import fsp\n"
1438714403
"from app.db import models\n"
1438814404
"from app.pkg import Explicit, Listed, Deep, star_fn, Renamed\n"
1438914405
"from app.direct.impl import Direct, direct_fn\n\n\n"
@@ -14409,6 +14425,8 @@ static int py_reexport_run(bool parallel, bool incremental, bool *lsp, bool *exc
1440914425
"def excl_barrier():\n barr.Before()\n\n\n"
1441014426
"def excl_compat():\n cmp.Shim()\n\n\n"
1441114427
"def lazy_tc():\n lz.Lazy()\n\n\n"
14428+
"def doc_reexport():\n docp.Doc()\n\n\n"
14429+
"def excl_fstring():\n fsp.Kept()\n\n\n"
1441214430
"def ctl_from_cls():\n Direct()\n\n\n"
1441314431
"def ctl_from_fn():\n direct_fn()\n");
1441414432
for (int i = 0; parallel && i < 52; i++) {

0 commit comments

Comments
 (0)