Ticket #2718: unused-in-function-2718.diff

File unused-in-function-2718.diff, 4.6 kB (added by jml, 1 year ago)
  • pyflakes/checker.py

    old new  
    3535        name = name.split('.')[0] 
    3636        super(Importation, self).__init__(name, source) 
    3737 
     38 
     39class Argument(Binding): 
     40    pass 
     41 
     42 
    3843class Assignment(Binding): 
    3944    pass 
    4045 
     46 
    4147class FunctionDefinition(Binding): 
    4248    pass 
    4349 
     
    316322            self.pushFunctionScope() 
    317323            addArgs(node.argnames) 
    318324            for name in args: 
    319                 self.addBinding(node.lineno, Assignment(name, node), reportRedef=False) 
     325                self.addBinding(node.lineno, Argument(name, node), reportRedef=False) 
    320326            self.handleNode(node.code) 
     327            for name, binding in self.scope.iteritems(): 
     328                if (not binding.used and not name in self.scope.globals 
     329                    and isinstance(binding, Assignment)): 
     330                    self.report(messages.UnusedVariable, 
     331                                binding.source.lineno, name) 
    321332            self.popScope() 
    322333 
    323334        self.defer(runFunction) 
    324335 
    325336    def CLASS(self, node): 
    326         self.addBinding(node.lineno, Assignment(node.name, node)) 
     337        self.addBinding(node.lineno, Binding(node.name, node)) 
    327338        for baseNode in node.bases: 
    328339            self.handleNode(baseNode) 
    329340        self.pushClassScope() 
  • pyflakes/messages.py

    old new  
    7171    def __init__(self, filename, lineno, names): 
    7272        Message.__init__(self, filename, lineno) 
    7373        self.message_args = (names,) 
     74 
     75 
     76class UnusedVariable(Message): 
     77    message = 'local variable %r is assigned to but never used' 
     78    def __init__(self, filename, lineno, names): 
     79        Message.__init__(self, filename, lineno) 
     80        self.message_args = (names,) 
  • pyflakes/test/test_imports.py

    old new  
    401401        import fu 
    402402        def a(): 
    403403            fu = 3 
     404            return fu 
    404405        fu 
    405406        ''') 
    406407 
  • pyflakes/test/test_other.py

    old new  
    7474        self.flakes('+1') 
    7575 
    7676 
     77    def test_unusedVariable(self): 
     78        """ 
     79        Warn when a variable in a function is assigned a value that's never 
     80        used. 
     81        """ 
     82        self.flakes(''' 
     83        def a(): 
     84            b = 1 
     85        ''', m.UnusedVariable) 
     86 
     87 
     88    def test_assignToGlobal(self): 
     89        """ 
     90        Assigning to a global and then not using that global is perfectly 
     91        acceptable. Do not mistake it for an unused local variable. 
     92        """ 
     93        self.flakes(''' 
     94        b = 0 
     95        def a(): 
     96            global b 
     97            b = 1 
     98        ''') 
     99 
     100    def test_assignToMember(self): 
     101        """ 
     102        Assigning to a member of another object and then not using that member 
     103        variable is perfectly acceptable. Do not mistake it for an unused 
     104        local variable. 
     105        """ 
     106        # XXX: Adding this test didn't generate a failure. Maybe not 
     107        # necessary? 
     108        self.flakes(''' 
     109        class b: 
     110            pass 
     111        def a(): 
     112            b.foo = 1 
     113        ''') 
     114 
     115 
    77116 
    78117class Python25Test(harness.Test): 
    79118    """ 
  • pyflakes/test/test_undefined_names.py

    old new  
    9191        def fun(): 
    9292            a 
    9393            a = 2 
     94            return a 
    9495        ''', m.UndefinedLocal) 
    9596 
    9697    def test_laterRedefinedGlobalFromNestedScope2(self): 
     
    106107                def fun2(): 
    107108                    a 
    108109                    a = 2 
     110                    return a 
    109111        ''', m.UndefinedLocal) 
    110112 
    111113 
     
    124126                    def c(): 
    125127                        x 
    126128                        x = 3 
     129                        return x 
     130                    return x 
     131                return x 
    127132        ''', m.UndefinedLocal).messages[0] 
    128133        self.assertEqual(exc.message_args, ('x', 5)) 
    129134 
     
    139144                def fun2(): 
    140145                    a 
    141146                    a = 1 
     147                    return a 
     148                return a 
    142149        ''', m.UndefinedLocal) 
    143150 
    144151    def test_nestedClass(self): 
     
    161168            class C: 
    162169                bar = foo 
    163170            foo = 456 
    164  
     171            return foo 
    165172        f() 
    166173        ''', m.UndefinedName) 
    167174 
jethro@divmod.org