That would require changes to all existing static analysis tools (again).
I was definitely not proposing "spooky action at a distance". My proposal is to existing f-strings as the `__setattr__` protocol is to the `__getattr__` protocol. The template would only ever be hard-coded inline during the f-string assignment. This is the same restriction that existing f-strings have. I am suggesting a syntactic construct where f-strings can be assignment targets, not suggesting to keep track of which strings were f-strings or overriding assignment or something silly like that.
Existing f-strings do not aim to replace str.format in situations where the format string is re-used.
Likewise, the proposal would not aim to replace regular expression in situations where the pattern is re-used.
The change would amount a syntax change for assignments: whenever an f-string is on the LHS of an assignment, the interpreter would do a parsing operation, and there would be no change to code where the f-string is in any other situation. This is again analogous to the behavior of `__getattr__` and `__setattr__`.
For example,
f"""<a href="{url}">{text}</a>""" = html
Would be roughly equivalent to
url, text = re.fullmatch(r"""<a href="(.*)">(.*)</a>""", html).groups()
Would this ever raise an exception on assignment?
The shortest match / greedy/non-greedy aspect of this is something that regex is designed to handle.
>>> (a, b, c) = 1, 2
<<< ValueError: not enough values to unpack (expected 3, got 2)
The LHS is never evaluated, it's only assigned to.
Why aren't either of these syntaxes for setting local variables supported?
locals().update(**kwargs)
locals()['url'] = '...'
Maybe it's just that every existing linter will complain about test assertions that reference local variables that are only defined in an f-string,
and I assume that f'{str:d{2}}' (r'(?P<str>\d{2})') doesn't work without extending the f-string grammar,
but I also cringe at this syntax.
I also prefer the way that re, regex, and parse return a dict and don't overload assignment for parser evaluation or variable allocation and initialization)
(Though I do find myself reaching for the more advanced destructuring assignment features of ECMAscript