Repository navigation
Replies: 3 comments 1 reply
|
I'm trying to understand what exactly you're proposing. So in that scenario, |
|
I'm not suggesting the type hints need to change. I'm suggesting Typer's contract guarantee Right now str coercion happens on every path but for This is admittedly an esoteric use case. I only suggest the contract because it would make things generally tidier unlike most esoterica. (while also allowing me to remove a monkey patch 😉) |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
First Check
Example Code
Description
Trying to clean up this old issue in django-typer
Django uses lazy translation wrappers to defer translation until after the translation system is bootstrapped. This means you often find devs passing gettext_lazy into command help strings.
By accident this works for options because inspect.cleandoc() coerces help strings and is called on construction:
typer/typer/core.py
Lines 479 to 480 in 82b8395
The TyperArgument constructor does not do this:
typer/typer/core.py
Lines 279 to 280 in 82b8395
I've been allowing gettext_lazy in Argument strings in django-typer by directly monkey patching the offending call. The actual crash only occurs when
rich_markup_mode=Nonebecause help formatting goes through the vendored click formatter instead. That path does write_dl → measure_table → term_len → strip_ansi, which runs a compiled regex directly on the object:I don't think coercing in this strip_ansi call is is the correct solution though, nor do I think cleandoc symmetry with Option in Argument is correct either. First, resolving the translation at TyperOption instantiation time means at command module import time. Usually this will happen after Django's bootstrapped the translation infra, but in weird edge cases like when pulling in code to sphinx or whatnot it might not resulting in unexpected crashes.
I think the deeper fix is to recognize that lazy str objects are a potential input to helps and add something like this to the Typer contract:
I wouldn't be surprised if this issue surfaces elsewhere and a statement like this is clear and verifiable in CI. Thoughts? Happy to create a PR.
One way to fix this with TyperOption and TyperArgument might be to convert help into a lazy property:
Operating System
macOS
Operating System Details
No response
Project Version
0.27.2
Python Version
3.14
Additional Context
No response
All reactions