News for hackers. Stuff that matters.
Re:Python's new t-strings by masklinn on Monday April 21, 2025 @10:21AM (#43752331)
> Aren't there other benefits to server-side parameter binding besides just SQL-injection safety? For instance, using PG's extended protocol (binary) instead of just raw SQL strings. Caching parameterized prepared statements, etc.

All of which can be implemented on top of template strings.

> A single character difference and now you've just made yourself trivially injectible.

It's not just a one character difference, it's a different type. So `db.execute` can reject strings both statically and dynamically.

> I don't think

Definitely true.

> this new format specifier is in any way applicable to SQL queries.

It's literally one of PEP 750's motivations.

Re:Python's new t-strings by tczMUFlmoNk on Monday April 21, 2025 @11:02AM (#43752752)
> > I don't think

> Definitely true.

The rest of your comment is valuable, but this is just mean-spirited and unnecessary.

Re:Python's new t-strings by woodrowbarlow on Monday April 21, 2025 @10:42AM (#43752558)
nitpicking:

> It's not just a one character difference, it's a different type. So `db.execute` can reject strings both statically and dynamically.

in this case, that's not actually helpful because SQL statements don't need to have parameters, so db.execute will always need to accept a string.

Re:Python's new t-strings by anamexis on Monday April 21, 2025 @11:26AM (#43753027)
You can just pass it a template with no substitutions.
Re:Python's new t-strings by masklinn on Monday April 21, 2025 @02:12PM (#43754776)
> db.execute will always need to accept a string.

No. A t-string with no placeholders is perfectly fine. You can use that even if you have no parameters.

Re:Python's new t-strings by willcipriano on Monday April 21, 2025 @10:26AM (#43752395)

    from string.templatelib import Template

    def execute(query: Template)
Should allow for static analysis to prevent this issue if you run mypy as part of your pr process.

That would be in addition to doing any runtime checks.

Re:Python's new t-strings by benwilber0 on Monday April 21, 2025 @10:42AM (#43752563)
The first mistake we're going to see a library developer make is:

    def execute(query: Union[str, Template]):

Maybe because they want their execute function to be backwards compatible, or just because they really do want to allow either raw strings are a template string.
Re:Python's new t-strings by masklinn on Monday April 21, 2025 @02:15PM (#43754807)
> they really do want to allow either raw strings are a template string.

I’d consider that an invalid use case:

1. You can create a template string without placeholders.

2. Even if the caller does need to pass in a string (because they’re executing from a file, or t-strings don’t support e.g. facetting) then they can just… wrap the string in a template explicitly.

Re:Python's new t-strings by VWWHFSfQ on Monday April 21, 2025 @10:26AM (#43752391)
> It's literally one of PEP 750's motivations.

Python is notorious for misguided motivations. We're not "appealing to authority" here. We're free to point out when things are goofy.

Re:Python's new t-strings by rangerelf on Monday April 21, 2025 @01:38PM (#43754441)
>> I don't think >Definitely true.

I thought we left middle-school playground tactics behind.

Re:Python's new t-strings by davepeck on Monday April 21, 2025 @03:44PM (#43755673)
> Caching parameterized prepared statements, etc.

I didn’t explicitly mention this in my post but, yes, the Template type is designed with caching in mind. In particular, the .strings tuple is likely to be useful as a cache key in many cases.