Pyflakes does not recognize locals() as a use

Bug #872503 reported by Andrew Schein on 2011-10-11
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Fix Released

Bug Description

See attached program for a trivial example of a common idiom:

"my special string %(with_a_variable)s"%locals()

I wouldn't expect pyflakes to know how to deal with an arbitrary dictionary in the right hand position, but the use of locals() means that certain variables are actually used!

Related branches

Andrew Schein (ato2g-andrew) wrote :
Jean-Paul Calderone (exarkun) wrote :

Pyflakes can't be sure that `locals` refers to the builtin locals function which returns the locals of the scope it is used in. It could have been overridden with another object which behaves differently.

Pyflakes could probably track the name and determine if it has been overridden in a sensible way, but there's no way for it to tell if the intended use is for the name to be injected externally somehow.

Andrew Schein (ato2g-andrew) wrote :

Is Pyflakes goal to be correct every time it points out an error? Or to not miss errors within predefined categories?

In the current behavior, Pyflakes flags "used" variables as "unused". If the user replaces locals() with their own function definition they will not get an error. Assuming that locals() refers to the builtin will produce a "sound" error-finder as defined

Jean-Paul Calderone (exarkun) wrote :

Pyflakes' goal is to be useful. It does not try to fit into any particular formal mathematical framework.

Personally, I avoid the question of what pyflakes should do in this case by not using locals() to interpolate into strings. I don't find that this constrains my ability to write programs easily.

I would say that injecting `locals` into a scope and expecting Pyflakes to behave correctly in that case is more unreasonable than expecting locals() interpolated into a string literal to mark the keys in the string literal as used locals. So, go ahead and submit a branch implementing this feature.

tags: removed: and
Florent (florent.x) on 2013-01-08
summary: - Pyflakes is unable to recognize string substitution as a use
+ Pyflakes does not recognize locals() as a use
Florent (florent.x) on 2013-01-24
Changed in pyflakes:
assignee: nobody → Florent (florent.x)
status: New → Confirmed
Florent (florent.x) on 2013-01-25
Changed in pyflakes:
status: Confirmed → In Progress
Florent (florent.x) on 2013-01-27
Changed in pyflakes:
importance: Undecided → Wishlist
Florent (florent.x) on 2013-01-29
Changed in pyflakes:
status: In Progress → Fix Committed
Florent (florent.x) on 2013-01-29
Changed in pyflakes:
milestone: none → 0.6.1
status: Fix Committed → Fix Released
Jason R. Coombs (jaraco) wrote :

Thanks very much for this change. I also concur with the change.

Question - should 'vars()' also be treated the same way as locals()?

To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Bug attachments