Pyflakes does not recognize locals() as a use

Bug #872503 reported by Andrew Schein
16
This bug affects 2 people
Affects Status Importance Assigned to Milestone
Pyflakes
Fix Released
Wishlist
Florent

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

Revision history for this message
Andrew Schein (ato2g-andrew) wrote :
Revision history for this message
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.

Revision history for this message
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
http://en.wikipedia.org/wiki/Soundness

Revision history for this message
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)
summary: - Pyflakes is unable to recognize string substitution as a use
+ Pyflakes does not recognize locals() as a use
Revision history for this message
Domen Kožar (ielectric+) wrote :
Florent (florent.x)
Changed in pyflakes:
assignee: nobody → Florent (florent.x)
status: New → Confirmed
Florent (florent.x)
Changed in pyflakes:
status: Confirmed → In Progress
Florent (florent.x)
Changed in pyflakes:
importance: Undecided → Wishlist
Florent (florent.x)
Changed in pyflakes:
status: In Progress → Fix Committed
Florent (florent.x)
Changed in pyflakes:
milestone: none → 0.6.1
status: Fix Committed → Fix Released
Revision history for this message
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  
Everyone can see this information.

Duplicates of this bug

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.