Skip to content

refactor: drop the dead initialiser in gen_utf_char - #69

Merged
cigamit merged 1 commit into
ctrliq:mainfrom
blaipr:refactor/drop-the-dead-gen-utf-char-initialiser
Sep 13, 2026
Merged

cigamit merged 1 commit into
ctrliq:mainfrom
blaipr:refactor/drop-the-dead-gen-utf-char-initialiser

Conversation

@blaipr

@blaipr blaipr commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

b = 'b' in gen_utf_char is never read. is_char starts False, so the loop below always runs at least once and rebinds b to the integer chr() is given, on every path that reaches the return.

 def gen_utf_char():
     is_char = False
-    b = 'b'
     while not is_char:
         b = random.randint(32, 0x10FFFF)
         is_char = chr(b).isprintable()
     return chr(b)

It was not harmless. It is the only reason the type checker believed chr() could be handed a string, reporting Expected SupportsIndex, found Literal["b"] | int for a call that cannot receive one. ty goes from 35 diagnostics to 34.

Verification

  • 2000 draws of gen_utf_char() all return a single printable character within the range the randint call specifies, so the loop's contract is unchanged.
  • random_utf8 and random_title(non_ascii=True), the two callers that reach it, still produce what they did.
  • ruff format --check, ruff check and the unit suite (366 passing) are unchanged.

The is_char sentinel is left alone. It is an ordinary loop variable rather than dead code, and rewriting the loop is a larger change than this one is.

`b = 'b'` is never read. `is_char` starts False, so the loop below always runs at
least once and rebinds `b` to the integer `chr()` is given, on every path that
reaches the return.

It was not harmless. It is the only reason the type checker believed `chr()`
could be handed a string, reporting `Expected SupportsIndex, found
Literal["b"] | int` for a call that cannot receive one.

Behaviour is unchanged: 2000 draws still return a single printable character in
the range the randint call specifies.
@ciq-it-service-account

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 merged commit 1c0a61b into ctrliq:main Sep 13, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants