~ubuntu-branches/ubuntu/raring/pyflakes/raring-proposed

« back to all changes in this revision

Viewing changes to pyflakes/test/test_undefined_names.py

  • Committer: Package Import Robot
  • Author(s): Dmitrijs Ledkovs
  • Date: 2013-02-13 19:07:52 UTC
  • mfrom: (3.1.9 experimental)
  • Revision ID: package-import@ubuntu.com-20130213190752-pr06y05rz8n06tmg
Tags: 0.6.1-1~exp1
* New upstream release:
  + rebase add_main_function.diff.
  - drop always_close_fd.diff, merged upstream.
  - drop check_encoding_errors.diff, merged upstream.
* Bump standards version
* Switch to dh_python2
* Bump debhelper to 9
* Adjust Vcs URL to canonical format.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
from _ast import PyCF_ONLY_AST
 
3
from sys import version_info
3
4
 
4
 
from twisted.trial.unittest import TestCase
 
5
from unittest2 import skip, skipIf, TestCase
5
6
 
6
7
from pyflakes import messages as m, checker
7
8
from pyflakes.test import harness
27
28
        self.flakes('range(10)')
28
29
 
29
30
 
 
31
    def test_builtinWindowsError(self):
 
32
        """
 
33
        C{WindowsError} is sometimes a builtin name, so no warning is emitted
 
34
        for using it.
 
35
        """
 
36
        self.flakes('WindowsError')
 
37
 
 
38
 
30
39
    def test_magicGlobalsFile(self):
31
40
        """
32
41
        Use of the C{__file__} magic global should not emit an undefined name
72
81
        bar
73
82
        ''', m.ImportStarUsed, m.UndefinedName)
74
83
 
 
84
    @skipIf(version_info >= (3,), 'obsolete syntax')
75
85
    def test_unpackedParameter(self):
76
86
        '''Unpacked function parameters create bindings'''
77
87
        self.flakes('''
79
89
            bar; baz
80
90
        ''')
81
91
 
 
92
    @skip("todo")
82
93
    def test_definedByGlobal(self):
83
94
        '''"global" can make an otherwise undefined name in another function defined'''
84
95
        self.flakes('''
85
96
        def a(): global fu; fu = 1
86
97
        def b(): fu
87
98
        ''')
88
 
    test_definedByGlobal.todo = ''
89
99
 
90
100
    def test_globalInGlobalScope(self):
91
101
        """
94
104
        self.flakes('''
95
105
        global x
96
106
        def foo():
97
 
            print x
 
107
            print(x)
98
108
        ''', m.UndefinedName)
99
109
 
100
110
    def test_del(self):
168
178
                def h(self):
169
179
                    a = x
170
180
                    x = None
171
 
                    print x, a
172
 
            print x
 
181
                    print(x, a)
 
182
            print(x)
173
183
        ''', m.UndefinedLocal)
174
184
 
175
185
 
210
220
                return a
211
221
        ''', m.UndefinedLocal)
212
222
 
 
223
    def test_undefinedAugmentedAssignment(self):
 
224
        self.flakes(
 
225
            '''
 
226
            def f(seq):
 
227
                a = 0
 
228
                seq[a] += 1
 
229
                seq[b] /= 2
 
230
                c[0] *= 2
 
231
                a -= 3
 
232
                d += 4
 
233
                e[any] = 5
 
234
            ''',
 
235
            m.UndefinedName,    # b
 
236
            m.UndefinedName,    # c
 
237
            m.UndefinedName, m.UnusedVariable,  # d
 
238
            m.UndefinedName,    # e
 
239
        )
 
240
 
213
241
    def test_nestedClass(self):
214
242
        '''nested classes can access enclosing scope'''
215
243
        self.flakes('''
238
266
        '''star and double-star arg names are defined'''
239
267
        self.flakes('''
240
268
        def f(a, *b, **c):
241
 
            print a, b, c
 
269
            print(a, b, c)
 
270
        ''')
 
271
 
 
272
    @skipIf(version_info < (3,), 'new in Python 3')
 
273
    def test_definedAsStarUnpack(self):
 
274
        '''star names in unpack are defined'''
 
275
        self.flakes('''
 
276
        a, *b = range(10)
 
277
        print(a, b)
 
278
        ''')
 
279
        self.flakes('''
 
280
        *a, b = range(10)
 
281
        print(a, b)
 
282
        ''')
 
283
        self.flakes('''
 
284
        a, *b, c = range(10)
 
285
        print(a, b, c)
 
286
        ''')
 
287
 
 
288
    @skipIf(version_info < (3,), 'new in Python 3')
 
289
    def test_keywordOnlyArgs(self):
 
290
        '''kwonly arg names are defined'''
 
291
        self.flakes('''
 
292
        def f(*, a, b=None):
 
293
            print(a, b)
 
294
        ''')
 
295
 
 
296
        self.flakes('''
 
297
        import default_b
 
298
        def f(*, a, b=default_b):
 
299
            print(a, b)
 
300
        ''')
 
301
 
 
302
    @skipIf(version_info < (3,), 'new in Python 3')
 
303
    def test_keywordOnlyArgsUndefined(self):
 
304
        '''typo in kwonly name'''
 
305
        self.flakes('''
 
306
        def f(*, a, b=default_c):
 
307
            print(a, b)
 
308
        ''', m.UndefinedName)
 
309
 
 
310
    @skipIf(version_info < (3,), 'new in Python 3')
 
311
    def test_annotationUndefined(self):
 
312
        """Undefined annotations"""
 
313
        self.flakes('''
 
314
        from abc import note1, note2, note3, note4, note5
 
315
        def func(a: note1, *args: note2,
 
316
                 b: note3=12, **kw: note4) -> note5: pass
 
317
        ''')
 
318
 
 
319
        self.flakes('''
 
320
        def func():
 
321
            d = e = 42
 
322
            def func(a: {1, d}) -> (lambda c: e): pass
 
323
        ''')
 
324
 
 
325
    @skipIf(version_info < (3,), 'new in Python 3')
 
326
    def test_metaClassUndefined(self):
 
327
        self.flakes('''
 
328
        from abc import ABCMeta
 
329
        class A(metaclass=ABCMeta): pass
242
330
        ''')
243
331
 
244
332
    def test_definedInGenExp(self):
246
334
        Using the loop variable of a generator expression results in no
247
335
        warnings.
248
336
        """
249
 
        self.flakes('(a for a in xrange(10) if a)')
 
337
        self.flakes('(a for a in %srange(10) if a)' %
 
338
                    ('x' if version_info < (3,) else ''))
250
339
 
251
340
 
252
341