Opened 6 years ago

Closed 6 years ago

#28810 closed Cleanup/optimization (fixed)

Use str.format()-style logging formatting instead of '%'

Reported by: Chris Lamb Owned by: nobody
Component: Core (Other) Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

We currently use the %(foo)s style in Django and in docs. This "smells" quite Python 2.x when there is a str.format variety that uses, eg. {foo}.

Change History (5)

comment:2 by Tim Graham, 6 years ago

Description: modified (diff)

I'm not sure why you say "Python 3-esque str.format" in the description. str.format() was added in Python 2.6. I see that logging support for that style (by using style='{') was added in Python 3.2.

I'm not opposed to the change, as it seems like a slight readability enhancement, although generally I think there's a consensus against using mandating str.format() usage everwhere in Django and prohibiting the %s syntax. I think there was a ticket but I can't find it at the moment.

My only concern with the proposal is whether it could break backwards compatibility. I see an update to ServerFormatter.uses_server_time() was required. I'm not sure if it's likely that user code may make similar assumptions about the format string.

in reply to:  2 comment:3 by Chris Lamb, 6 years ago

Replying to Tim Graham:

I'm not sure why you say "Python 3-esque str.format" in the description. str.format() was added in Python 2.6.

I should have used a better suffix than "esque" — it's more that Python 3 code does tend to *prefer* str.format over %-style interpolation simply as a correlation with it being more modern/newer in general. ie. if it's "new enough" for Python 3, it's probably going to be using newer/nicer features. It's therefore somewhat surprising to see the reverse, hence this proposed change.

there's a consensus against using mandating str.format() usage everwhere in Django and prohibiting the %s syntax.

As it happens, I would be against such a total mandate and a widespread change *within* Django, but this is more of a user-exposed thing so a different, separate argument IMHO.

comment:4 by Tim Graham, 6 years ago

Summary: Use the Python 3-esque str.format logging formatting style over '%'Use str.format()-style logging formatting instead of '%'
Triage Stage: UnreviewedReady for checkin

comment:5 by Tim Graham <timograham@…>, 6 years ago

Resolution: fixed
Status: newclosed

In 3af305e8:

Fixed #28810 -- Replaced '%' style logging formatting with str.format() style.

Note: See TracTickets for help on using tickets.
Back to Top