Repository navigation
Closed
Labels
Description
Problem
_app_id_from_name in src/pythonnative/cli/pn.py (lines 130-134) derives the default Android application id / iOS bundle id that pn init writes into pythonnative.toml:
def _app_id_from_name(name: str) -> str:
slug = re.sub(r"[^a-z0-9_]", "", name.lower())
if not slug or not slug[0].isalpha():
slug = "app" + slug
return f"com.example.{slug}"Two gaps:
- No direct tests.
tests/test_cli.pyonly exercises the happy path indirectly, by scaffoldingmy_appand assertingid = "com.example.my_app"appears in the generated TOML (lines 54 and 93), plus one directory-named case (widgets, line 109). The interesting behavior is untested: hyphens are stripped (my-appbecomescom.example.myapp, because Java package segments can't contain-), and a digit-leading name gets anappprefix (3d_viewerbecomescom.example.app3d_viewer). Its neighbor_sanitize_name(lines 107-127) has dedicated unit tests at lines 270-282. - No docstring.
_sanitize_namedirectly above has a full Google-style docstring explaining its rules;_app_id_from_namehas none, so the hyphen-stripping and prefix behavior is only discoverable by reading the regex.
Ruff doesn't flag this because the function is private, but the file otherwise documents its helpers, and the behavior is user-visible in every scaffolded project.
Suggested fix
- Add unit tests in
tests/test_cli.pynext to the_sanitize_nametests that callpn_cli._app_id_from_namedirectly and cover: a plain name, a name with hyphens, a name with uppercase letters, a digit-leading name, and an empty string. Assert each result matches a reverse-DNS pattern such as^com\.example\.[a-z][a-z0-9_]*$. - Add a Google-style docstring to
_app_id_from_namemodeled on_sanitize_name's, withArgs:andReturns:sections and one sentence on why hyphens are dropped. - Don't change the function's behavior; existing scaffolded projects depend on it.
Verification
uv run pytest tests/test_cli.py -qpasses../scripts/check.shpasses.
Suggested commit message
test(cli): cover app id derivation from project names
Activity
Adebowale-Morakinyo commentedon Sep 2, 2026
@owenthcarey third is the charm😁. PS, this is also related to the hyphen behavior I flagged. Thanks!
TheoGoulart333 commentedon Sep 2, 2026
Hi! I'd like to work on this issue. Could you please assign it to me?
Adebowale-Morakinyo commentedon Sep 4, 2026
Some findings from digging in, two of which change the shape of the work.
The headline example can't be typed. The issue names
3d_viewer→com.example.app3d_vieweras the interesting untested behavior, but_NAME_RErejects3d_viewer, sopn init 3d_viewerexits 1 on the name guard and never reaches this function. Theapp-prefix branch is reachable only viapn initwith no name, where the project name comes from the directory and is unvalidated by design. Same for the empty-string and uppercase cases. From a typed name, hyphen removal is the only live transform. The tests will cover both paths and say which reaches what.The suggested assertion can't fail in the way I originally described. I fuzzed
^com\.example\.[a-z][a-z0-9_]*$over ~2.2M inputs — exhaustive ASCII to length 3, every Unicode character whose.lower()changes length, plus random control characters and non-ASCII — with zero failures. It holds structurally for the current derivation:re.subleaves the slug inside[a-z0-9_], so either the first character is[a-z]or theappprefix makes ita. That said, the assertion can still catch a change that admits a character outside the segment grammar, so it is worth keeping alongside exact-value assertions rather than relying on it alone. It does not, however, pin the prefix.Current coverage pins neither behavior this issue exists to cover. Mutating the function to keep hyphens breaks zero tests; changing the prefix from
apptoxbreaks zero tests. Every existing fixture usesmy_app,widgets, orMyProject— no hyphens, nothing digit- or underscore-leading. What the six downstream assertions actually pin is underscore preservation and lowercasing, incidentally. The new tests will be mutation-checked against exactly those two mutations.Separately, and I'd like to open an issue for it:
pn init classexits 0 and writesid = "com.example.class", which_validate_app_idthen rejects — all 53 entries in_JAVA_KEYWORDSpass_NAME_RE. The scaffold is dead on arrival:app-id,deps, andrunall exit 1 afterwards with an error naming a config field the user never wrote.doctordoes flag it. The derived path reaches it from more directions too, since case and punctuation are stripped —Class,CLASS,cla-ssall derive the same id. Out of scope here, and the fix is a product call (reject atpn init, suffix the slug, or fall back). Want me to file it?One more for the record, not something I'd change here: hyphen deletion makes the mapping non-injective, so
my-appandmyappboth givecom.example.myappand would install over each other on a device. Mapping-to_would not solve that:_NAME_REpermits both characters, somy-appandmy_appwould both derivecom.example.my_app. Making the derivation injective would need an escaping scheme, not a simple substitution. So hyphen deletion isn't uniquely problematic; it is simply lossy.@owenthcarey
test(cli): cover app id derivation from project names (#79)