[Django] #29988: Use f-strings for string formatting once support for Python 3.5 is dropped

51 views
Skip to first unread message

Django

unread,
Nov 26, 2018, 9:05:37 AM11/26/18
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: | Version:
Uncategorized |
Severity: Normal | Keywords: fstrings python3.6
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The [https://devguide.python.org/#branchstatus end of life date for Python
3.5] is 2020-09-13. This means that the next version of Django after this
date (earliest 3.1?) can start using
[https://www.python.org/dev/peps/pep-0498/ Literal String Interpolation
(f-strings)] for string formatting.

Not only are f-strings very programmer friendly, they are also
[https://gist.githubusercontent.com/skabbass1/6afdbbaa2689d2a577b4d76bd77d96a1/raw/6091e9e687aa481b723c3e970ce8091d3f7daa40/fstring_performance.py
very fast] (source: [https://hackernoon.com/a-closer-look-at-how-python-f
-strings-work-f197736b3bdb A Closer Look At How Python f-strings Work]).

--
Ticket URL: <https://code.djangoproject.com/ticket/29988>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Nov 26, 2018, 9:13:06 AM11/26/18
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Uncategorized | Version:
Severity: Normal | Resolution:

Keywords: fstrings python3.6 | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jaap Roes):

Current master has 193 matches for `find . -type f -name "*.py" -exec grep
-n "['\"]\.format(" {} + | wc -l`. (All direct invocations of the `format`
method on a string literal).

There are 3435 matches for `find . -type f -name "*.py" -exec grep -n
"['\"] %" {} + | wc -l` (All uses of the `%` operator on a string
literal).

There's probably many more places where `.format` or `%` formatting is
used on a variable string.

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:1>

Django

unread,
Nov 26, 2018, 9:17:28 AM11/26/18
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution:

Keywords: fstrings python3.6 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* component: Uncategorized => Core (Other)
* stage: Unreviewed => Someday/Maybe


Comment:

See the [https://groups.google.com/d/topic/django-
developers/psUTrFUNlQE/discussion django-developers discussion] for some
concerns (e.g. no i18n support and Florian's comment, "Knowing what
certain members of the core team think about those f-strings, I think
there will be first a big discussion if we will allow them at all in
Django's codebase.").

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:2>

Django

unread,
Nov 26, 2018, 9:52:23 AM11/26/18
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution:
Keywords: fstrings python3.6 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jaap Roes):

That discussion (and the one linked to) seem to be more about (or digress
into) using `.format` instead of `%` formatting. I'm not in favor of going
through and replacing all `%` formatting with `.format` calls. Instead
Django should just start using a f-strings feature ("Literal String
Interpolation") introduced in Python 3.6 when/where possible.

I'm aware of the concern about i18n, but a lot of the string
formatting/concatenation in Django isn't translatable text and therefore
isn't affected by that issue at all.

This is really more about replacing code like this:

{{{ path=handler.__module__ + '.' + handler.__qualname__, }}} with {{{
path=f'{handler.__module__}.{handler.__qualname__}' }}}

{{{ return "{}({}, {})".format(self.__class__.__name__, self.sql,
self.params) }}} with {{{ return f'{self.__class__.__name__}({self.sql},
{self.params})' }}}

and {{{ sql = '%s %s%s ON (%s)' % (self.join_type, qn(self.table_name),
alias_str, on_clause_sql) }}} with {{{ sql = f'{self.join_type}
{qn(self.table_name)}{alias_str} ON ({on_clause_sql})' }}}

Which (arguably) benefits the readability and can have a positive
performance impact on the Django code base.

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:3>

Django

unread,
Dec 4, 2018, 8:22:25 PM12/4/18
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution:
Keywords: fstrings python3.6 | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tom Forbes):

* cc: Tom Forbes (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:4>

Django

unread,
Apr 1, 2020, 10:24:59 PM4/1/20
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution:
Keywords: fstrings python3.6 | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jon Dufresne):

* has_patch: 0 => 1


Comment:

A first pass that only modifies simple uses of `.format()` such that the
string expressions are shorter, are not translated strings, and do not
nest quotes.

https://github.com/django/django/pull/12650

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:5>

Django

unread,
Apr 2, 2020, 3:25:56 AM4/2/20
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution:
Keywords: fstrings python3.6 | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

I'm in favor of this change. However, I'm a bit hesitant for error
messages. They are not translatable now, but they could in the future (and
most should IMHO, but this is another story).

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:6>

Django

unread,
Apr 2, 2020, 4:28:55 AM4/2/20
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution:
Keywords: fstrings python3.6 | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Paolo Melchiorre):

Django 2.2 is the last to support Python 3.5 so I'm in favor of using
f-string in Django 3

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:7>

Django

unread,
Apr 2, 2020, 10:51:10 AM4/2/20
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Core (Other) | Version:
Severity: Normal | Resolution:
Keywords: fstrings python3.6 | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

I am in favor well where it makes sense and has clear readability
benefits. I don't have strong rules to provide but I've already seen this
feature abused in the wild to compose complex formatting string which
makes hard to differentiate placeholders from their delimiters when
function calls are involved.

I also share you Claude's concerns about error messages and logging
message templates.

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:8>

Django

unread,
Oct 14, 2020, 7:31:42 AM10/14/20
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: Carlton
Type: | Gibson
Cleanup/optimization | Status: assigned

Component: Core (Other) | Version:
Severity: Normal | Resolution:
Keywords: fstrings python3.6 | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* owner: nobody => Carlton Gibson
* status: new => assigned
* stage: Someday/Maybe => Ready for checkin


Comment:

We pushed this forwards after [https://groups.google.com/d/topic/django-
developers/XNhpOHam0zE/discussion discussion on the mailing list].
[https://github.com/django/django/pull/13214 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:9>

Django

unread,
Oct 15, 2020, 4:10:13 AM10/15/20
to django-...@googlegroups.com
#29988: Use f-strings for string formatting once support for Python 3.5 is dropped
-------------------------------------+-------------------------------------
Reporter: Jaap Roes | Owner: Carlton
Type: | Gibson
Cleanup/optimization | Status: closed
Component: Core (Other) | Version:
Severity: Normal | Resolution: fixed

Keywords: fstrings python3.6 | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson <carlton@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"411cc0ae18aa9c64447eed229e65e092d23f80e6" 411cc0a]:
{{{
#!CommitTicketReference repository=""
revision="411cc0ae18aa9c64447eed229e65e092d23f80e6"
Fixed #29988 -- Updated coding style to allow f-strings.

Thanks to Nick Pope for review.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/29988#comment:10>

Reply all
Reply to author
Forward
0 new messages