News for hackers. Stuff that matters.
Re:Python's new t-strings by pphysch on Monday April 21, 2025 @10:42AM (#43752552)
How? tstrings and fstrings are literals for completely different types.

CS has survived for decades with 1 and 1.0 being completely different types.

Re:Python's new t-strings by Certhas on Monday April 21, 2025 @11:38AM (#43753177)
I had an extended debugging session last week that centered on 1 and 1. confusion in a library I have to use...
Re:Python's new t-strings by pphysch on Monday April 21, 2025 @05:51PM (#43756918)
Yeah, it's a real bummer when that happens. I wish JSON never tried to do types.
Re:Python's new t-strings by Izkata on Monday April 21, 2025 @11:35AM (#43753130)
Because they're both passed to "execute", which can't tell between the f-string and a non-interpolated query, so it just has to trust you did the right thing. Typoing the "t" as an "f" introduces SQL injection that's hard to spot.
Re:Python's new t-strings by vlovich123 on Monday April 21, 2025 @11:43AM (#43753238)
Assuming `execute` takes both. You could have `execute(template)` and `execute_interpolated(str, ...args)` but yeah if it takes both you'll have challenges discouraging plain-text interpolation.
Re:Python's new t-strings by Izkata on Monday April 21, 2025 @12:23PM (#43753703)
It would have to be the other way around or be a (possibly major) breaking change. Just execute() with strings is already standard python that all the frameworks build on top of, not to mention tutorials:

https://docs.python.org/3/library/sqlite3.html

https://www.psycopg.org/docs/cursor.html

https://dev.mysql.com/doc/connector-python/en/connector-pyth...

Re:Python's new t-strings by dragonwriter on Wednesday April 23, 2025 @02:20AM (#43769181)
> It would have to be the other way around or be a (possibly major) breaking change.

If it is going to reject the currently-accepted unsafe usage, its going to be a major breaking change in any case, so I don't see the problem. I mean, if you are lamenting it can't reject the currently-accepted SQL-interpolated-via-f-string because it can't distinguish it by type from plain strings with no interpolation, you are already saying that you want a major breaking change but are upset because the particular implementation you want is not possible. So you can't turn around and dismiss an alternative solution because it would be a major breaking change, that's what was asked for!

Re:Python's new t-strings by zahlman on Monday April 21, 2025 @05:37PM (#43756782)
`execute` can tell the difference, because `t"..."` does not create the same type of object that `f"..."` does.
Re:Python's new t-strings by MR4D on Monday April 21, 2025 @01:50PM (#43754544)
Reread my comment. It’s about noticing you have an “f” or a “t” and both are very similar characters.
Re:Python's new t-strings by rocha on Monday April 21, 2025 @02:00PM (#43754644)
Yes, but you will get an error since string and templates are different types and have different interfaces.
Re:Python's new t-strings by Izkata on Monday April 21, 2025 @03:48PM (#43755714)
Click "parent" a few times and look at the code example that started this thread. It's using the same function in a way that can't distinguish whether the user intentionally used a string (including an f-string) and a t-string.
Re:Python's new t-strings by zahlman on Monday April 21, 2025 @05:36PM (#43756771)
Yes, and the parent is misguided. As was pointed out in multiple replies, the library can distinguish whether an ordinary string or a t-string is passed because the t-string is not a string instance, but instead creates a separate library type. A user who mistakenly uses an f prefix instead of a t prefix will, with a properly designed library, encounter a `TypeError` at runtime (or a warning earlier, given type annotations and a checker), not SQL injection.
Re:Python's new t-strings by Izkata on Monday April 21, 2025 @08:57PM (#43758123)
In this particular instance it can't, because there are 3 ways in question here, and it can't distinguish between correct intentional usage and accidental usage of an f-string instead of a t-string:

  db.execute("SELECT foo FROM bar;")
  db.execute(f"SELECT foo FROM bar WHERE id = {foo_id};")
  db.execute(t"SELECT foo FROM bar WHERE id = {foo_id};")
The first and second look identical to execute() because all it sees is a string. But the second one is wrong, a hard-to-see typo of the third.

If f-strings didn't exist there'd be no issue because it could distinguish by type as you say. But we have an incorrect SQL-injection-prone usage here that can't be distinguished by type from the correct plain string usage.

Re:Python's new t-strings by Timon3 on Tuesday April 22, 2025 @01:42PM (#43764565)
There is no reason to support the first or second usage. It's totally fine to always require a t-string:

    db.execute(t"SELECT foo FROM bar;")
See? No reason to accept strings, it's absolutely fine to always error if a string is passed.
Re:Python's new t-strings by Izkata on Tuesday April 22, 2025 @03:16PM (#43765327)
My (and their) point is that's the already existing API. You're proposing a big breaking change, with how many frameworks and tutorials are built on top of that.
Re:Python's new t-strings by Timon3 on Tuesday April 22, 2025 @03:30PM (#43765451)
It's not like this is the first time APIs have been improved. There are many tools (e.g. deprecation warnings & hints in editors, linter rules) that can help bridge the gap - even if t-strings are only used for new or refactored code, it's still a big improvement!

There's also simply no hard requirement to overload an `execute` function. We have options beyond "no templates at all" and "execute takes templates and strings", for example by introducing a separate function. Why does perfect have to be the enemy of good here?