Ideally, this would be an IDE feature.
The issue is that IDE by default can only know the obvious places to do this, such as __all__.
Thus, the need for standard way to hint IDE.
Mechanics:
- It wouldn’t evaluate the expression.
- It would do a parsing for valid syntax
So minimal correctness check by parser.
But yes, would not do the same amount of validation as if expression was evaluated.
Possible solutions:
name = <keyword> expr
name = f'{=expr}' # This was my proposal some time ago
name = nameof(var) # This is C# solution (which is only for variable name)
name = e'expr' # another string type...
name = f'{expr!e}'
I like last one most:
- Doesn’t introduce new keyword
- Works with arbitrary expression (as opposed to nameof)
- Does not introduce new string type (too many of those already, reserving whole string type for a feature like this is too much IMO)
- Localises it nicely to be part of
f-stringsas opposed to something completely new (I don’t think it deserves its own thing - string type, keyword, …) - Can be used as part of
f-stringsin combination to all of the other nice features of it