Skip to content

fix: generate readable ASCII names by default - #45

Open
blaipr wants to merge 1 commit into
ctrliq:mainfrom
blaipr:fix/random-title-suffix
Open

fix: generate readable ASCII names by default#45
blaipr wants to merge 1 commit into
ctrliq:mainfrom
blaipr:fix/random-title-suffix

Conversation

@blaipr

@blaipr blaipr commented Sep 12, 2026

Copy link
Copy Markdown
Contributor

Closes #43.

random_title() supplies the default name for a resource created through the HasCreate API without one. It had three branches and only one of them produced a name anybody would want, and that one was unreachable without knowing an environment variable nothing documents.

def random_title(num_words=2, non_ascii=True):
    base = ''.join([random.choice(words) for word in range(num_words)])
    if os.getenv('ASCENDERKIT_FORCE_ONLY_ASCII', False):
        title = ''.join([base, ''.join(str(random_int(99)))])
    else:
        if non_ascii:
            title = ''.join([base, random_utf8(1)])
        else:
            title = ''.join([base, ''.join([str(random_int()) for _ in range(3)])])
    return title

The default appended one random BMP character. random_utf8 scrubs anything outside the BMP and substitutes U+FFFD, so a good share of the output ended in a replacement character: Inventory - MorningSample<?>, rendered as a black diamond or an empty box depending on the font.

non_ascii=False was worse. It appended three random_int() calls, each random.randint(0, sys.maxsize), concatenated: Inventory - ArtIllegal646628978901380143791055131131464064421635873256955087439.

ASCENDERKIT_FORCE_ONLY_ASCII gave the sensible two-digit suffix, and appears exactly once in the repository, in the line above.

The call sites had already worked this out, one failure at a time. Six of the forty-three passed non_ascii=False, and they are precisely the places where a stray character breaks something rather than merely looking wrong: a username and an email address in users.py, the /tmp/inventory<char>.ini path in ascender/inventory.py, and the Ansible group and host names in api/pages/inventory.py. Everywhere it was merely ugly, it stayed on.

So this keeps the branch that works and deletes the two that do not:

Inventory - SquarePen56
Inventory - PrivatePeople0
Inventory - FootResolution44
Inventory - ExplanationGrand57

non_ascii survives as an opt-in parameter defaulting to False, because deliberately exercising the API's Unicode handling is a legitimate thing to want, it is just a poor default. ASCENDERKIT_FORCE_ONLY_ASCII goes, since what it produced is now the normal output. The six explicit non_ascii=False arguments go with it, being the default now. random_title gains the docstring it never had.

words.py is untouched. 1525 nouns with no duplicates is a lot of file, and it is also what makes these names readable and greppable in an activity stream when you are looking at what a test run left behind. The word list was never the problem.

Scope. The CLI cannot reach any of this: ascender <resource> create posts the kwargs it was given through self.page.post(kwargs), and nothing under cli/ calls create() or payload(). Every random_title call sits inside payload(), add_survey(), copy(), add_schedule() or make_approval_node(), the fixture API inherited from awxkit. It is still a behaviour change for library callers, so it is in the changelog as one.

Tests. The two existing tests still pass unchanged and still cover both branches. Two more pin the shape the old code had no assertion for: that the default is ASCII over 50 draws, and that the result is words followed by at most two digits, for one, two and five words. Both would have failed before this change, on either branch. The skipif(sys.version_info < (3, 6)) markers on the existing pair are also gone, the floor being 3.11.

Verified with black --check, flake8 and the unit suite, 359 passing.

@ciq-it-service-account

ciq-it-service-account commented Sep 12, 2026

Copy link
Copy Markdown

Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@cigamit cigamit self-assigned this Sep 13, 2026
@cigamit cigamit added the enhancement New feature or request label Sep 13, 2026
cigamit
cigamit previously approved these changes Sep 13, 2026
Closes ctrliq#43.

`random_title()` supplies the default name for a resource created through the `HasCreate` API without one. It had three branches and only one of them produced a name anybody would want, and that one was unreachable without knowing an environment variable nothing documents.

```python
def random_title(num_words=2, non_ascii=True):
    base = ''.join([random.choice(words) for word in range(num_words)])
    if os.getenv('ASCENDERKIT_FORCE_ONLY_ASCII', False):
        title = ''.join([base, ''.join(str(random_int(99)))])
    else:
        if non_ascii:
            title = ''.join([base, random_utf8(1)])
        else:
            title = ''.join([base, ''.join([str(random_int()) for _ in range(3)])])
    return title
```

The default appended one random BMP character. `random_utf8` scrubs anything outside the BMP and substitutes U+FFFD, so a good share of the output ended in a replacement character: `Inventory - MorningSample<?>`, rendered as a black diamond or an empty box depending on the font.

`non_ascii=False` was worse. It appended three `random_int()` calls, each `random.randint(0, sys.maxsize)`, concatenated: `Inventory - ArtIllegal646628978901380143791055131131464064421635873256955087439`.

`ASCENDERKIT_FORCE_ONLY_ASCII` gave the sensible two-digit suffix, and appears exactly once in the repository, in the line above.

**The call sites had already worked this out, one failure at a time.** Six of the forty-three passed `non_ascii=False`, and they are precisely the places where a stray character breaks something rather than merely looking wrong: a username and an email address in `users.py`, the `/tmp/inventory<char>.ini` path in `ascender/inventory.py`, and the Ansible group and host names in `api/pages/inventory.py`. Everywhere it was merely ugly, it stayed on.

So this keeps the branch that works and deletes the two that do not:

```
Inventory - SquarePen56
Inventory - PrivatePeople0
Inventory - FootResolution44
Inventory - ExplanationGrand57
```

`non_ascii` survives as an opt-in parameter defaulting to `False`, because deliberately exercising the API's Unicode handling is a legitimate thing to want, it is just a poor default. `ASCENDERKIT_FORCE_ONLY_ASCII` goes, since what it produced is now the normal output. The six explicit `non_ascii=False` arguments go with it, being the default now. `random_title` gains the docstring it never had.

`words.py` is untouched. 1525 nouns with no duplicates is a lot of file, and it is also what makes these names readable and greppable in an activity stream when you are looking at what a test run left behind. The word list was never the problem.

**Scope.** The CLI cannot reach any of this: `ascender <resource> create` posts the kwargs it was given through `self.page.post(kwargs)`, and nothing under `cli/` calls `create()` or `payload()`. Every `random_title` call sits inside `payload()`, `add_survey()`, `copy()`, `add_schedule()` or `make_approval_node()`, the fixture API inherited from `awxkit`. It is still a behaviour change for library callers, so it is in the changelog as one.

**Tests.** The two existing tests still pass unchanged and still cover both branches. Two more pin the shape the old code had no assertion for: that the default is ASCII over 50 draws, and that the result is words followed by at most two digits, for one, two and five words. Both would have failed before this change, on either branch. The `skipif(sys.version_info < (3, 6))` markers on the existing pair are also gone, the floor being 3.11.

Verified with `black --check`, `flake8` and the unit suite, 359 passing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request Needs Triage

Development

Successfully merging this pull request may close these issues.

random_title generates unusable names by default

3 participants