Merge lp:~jml/pyflakes/duplicate-class-defs into lp:~vcs-imports/pyflakes/main
| Status: | Merged |
|---|---|
| Merge reported by: | Jonathan Lange |
| Merged at revision: | not available |
| Proposed branch: | lp:~jml/pyflakes/duplicate-class-defs |
| Merge into: | lp:~vcs-imports/pyflakes/main |
| Diff against target: |
214 lines (+102/-15) 4 files modified
pyflakes/checker.py (+16/-7) pyflakes/messages.py (+2/-2) pyflakes/test/test_imports.py (+0/-3) pyflakes/test/test_other.py (+84/-3) |
| To merge this branch: | bzr merge lp:~jml/pyflakes/duplicate-class-defs |
| Related bugs: |
| Reviewer | Review Type | Date Requested | Status |
|---|---|---|---|
| VCS imports | 2010-12-23 | Pending | |
|
Review via email:
|
|||
Description of the Change
This branch changes pyflakes to warn about more kinds duplicated definitions:
* reassigning to a variable without using it first
* redefining a class with the same name as an existing class / function
* redefining a function with the same name as an existing class / function
And so forth.
In the branch, I deleted one existing test that seemed to run counter to this desired behaviour.
I've simplified the "check for redefinition" logic so that it checks for any rebindings. I had to be careful not to tread on the special Importation checks/warnings and the Argument re-use warnings. The way I did this was to make a special kind of binding type called Definition, and make Assignment, FunctionDefinition and ClassDefinition inherit from this.
Note that the new code will *not* catch things like this::
def f(x):
x = 1
Perhaps it should.
Below is the review discussion from the divmod ticket, which may one day be available on http://
Agreed. I'll fix this up & resubmit a patch.
===
Okay. If this bothers people, I guess they can rename the class they're smashing to something else.
The special handling of ClassDefinition (mirroring the existing special handling of FunctionDefinition) seems weird though. What about this?
$ cat | pyflakes
def foo(): pass
class foo: pass
$
Seems as warning-worthy as two functions or two classes. I wonder why it doesn't just check bindings. Is this not worth a warning?
$ cat | pyflakes
x = 10
x = 10
$
These all seem like they should be handled like imports are handled:
$ cat | pyflakes
from twisted import foo
from twisted import foo
<stdin>:2: redefinition of unused 'foo' from line 1
<stdin>:2: 'foo' imported but unused
===
Warning on class re-definitions is consistent with warning about function re-definitions:
cat | ./bin/pyflakes
def f():
pass
x = f()
def f():
pass
<stdin>:4: redefinition of function 'f' from line 1
With class redefinitions, the only times I've ever used the facility to redefine a class in a Python module, it has been by mistake.
Both observations lend weight to including my patch, although I'll admit it's a matter of judgment.
I *almost* wish pyflakes had some kind of configuration system.
===
This looks like a good thing to fix. I don't know about this though:
$ cat | pyflakes
class foo: pass
class foo(foo): pass
<stdin>:2: redefinition of class 'foo' from line 1
The class being redefined was used before its name was rebound, at least. Worth a warning? Or will someone do this intentionally and be upset at the warning?
It's even less clear in other equivalent cases:
$ cat | pyflakes
class foo: pass
x = foo()
class foo: pass
<stdin>:3: redefinition of class 'foo' from line 1
Any thoughts?
| Robert Collins (lifeless) wrote : | # |
| Jonathan Lange (jml) wrote : | # |
Don't worry about that Rob, you won't get nagged, even with this branch as it stands.
- 48. By Jonathan Lange on 2010-12-24
-
Add a test allaying Rob's fears
| Jonathan Lange (jml) wrote : | # |
Oops. Got the test wrong. Accidentally asserted that it generated a warning. Fixing now.
- 49. By Jonathan Lange on 2010-12-24
-
Fixinate

On Fri, Dec 24, 2010 at 6:25 AM, Jonathan Lange <email address hidden> wrote: /code.launchpad .net/~jml/ pyflakes/ duplicate- class-defs/ +merge/ 44601
> Jonathan Lange has proposed merging lp:~jml/pyflakes/duplicate-class-defs into lp:pyflakes.
>
> Requested reviews:
> VCS imports (vcs-imports)
>
> For more details, see:
> https:/
>
> This branch changes pyflakes to warn about more kinds duplicated definitions:
> * reassigning to a variable without using it first
I would hate to be nagged about this. I do the following a lot: eofcondition
foo = sanedefault
if condition:
foo = somethingbecaus
> * redefining a class with the same name as an existing class / function
> * redefining a function with the same name as an existing class / function
Woo.