Merge lp:~jml/divmod.org/duplicate-class-defs into lp:divmod.org
Status: | Merged |
---|---|
Merged at revision: | 2685 |
Proposed branch: | lp:~jml/divmod.org/duplicate-class-defs |
Merge into: | lp:divmod.org |
Diff against target: |
212 lines (+100/-15) 4 files modified
Pyflakes/pyflakes/checker.py (+16/-7) Pyflakes/pyflakes/messages.py (+2/-2) Pyflakes/pyflakes/test/test_imports.py (+0/-3) Pyflakes/pyflakes/test/test_other.py (+82/-3) |
To merge this branch: | bzr merge lp:~jml/divmod.org/duplicate-class-defs |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Christopher Armstrong (community) | code | Approve | |
Divmod-dev | Pending | ||
Review via email: mp+80813@code.launchpad.net |
Description of the change
This merge proposal lived at https:/
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?
[1] "A binding that that defines a function or class" -> duplicated "that".
[2] test_other.py line 133: there are four lines of whitespace here, between two methods, instead of 2.
Thanks for replacing the dumb test-for- bad-behavior with a TODOed test for the desired behavior.
I grabbed the branch, manually verified some behaviors (like class foo(foo) not emitting a warning), ran the unit tests, and reviewed the code itself.
This looks good. With the above mentioned lexical changes you have my +1.
I don't believe I have commit access to the trunk branch, so you'll have to get someone else who trusts my review to commit it. Sorry :-(