← Back to team overview

divmod-dev team mailing list archive

Re: [Merge] lp:~dobey/divmod.org/pyflakes-less-redef into lp:divmod.org

 

Hello everyone,

I took another approach to fix this bug (it is done against kevinw's fork but I hope those projects will soon merge). 

The change:
https://github.com/saper/pyflakes/commit/09c52fcfab742b4f67c9d21d18ae389c300b4ea6

Updated tests:
https://github.com/saper/pyflakes/commit/5d2b7563cb63b080c99f6df24661dd9dcec188e1

The code can be easily cloned from github with

git clone git://github.com/saper/pyflakes.git

My approach, while not perfect and still to be fine-tuned, is different: whenever a RedefinedWhileUnused exception is to be raised, additional check is made whether "existing" (already detected existing name binding) and "loc" (current "redefinition") do not reside on
different "forks" of if: else: or try: except: else: blocks.

First, AST tree is searched upwards for both of them to find a common ancestor. If this is "IF"
or "TRYEXCEPT", there is a good chance that they might be under different blocks (like "if:"
or "else:"). Appropriate lists are checked, and exception is raised in case of:

try:
  import os
  import os
except: 
  pass

but not

try:
  import os
except:
  import os

it does not matter what exception is raised/checked, of course conditions are not evaluated
(as this is not a dynamic analysis), but it seems to work fine for most typical Python
idioms when importing modules using if: or try:

I'd love to hear your feedback on this approach!

-- 
https://code.launchpad.net/~dobey/divmod.org/pyflakes-less-redef/+merge/130183
Your team Divmod-dev is subscribed to branch lp:divmod.org.


References