PyCQA / pycodestyle

Simple Python style checker in one Python file
https://pycodestyle.pycqa.org
Other
5.05k stars 751 forks source link

Detecting unassigned triple quoted strings #37

Closed unode closed 12 years ago

unode commented 13 years ago

Python doesn't provide a way to do block quotes other than prefixing all lines in a block with # . Some people find this "too complicated" and have started to use triple quotes (""" or ''') to achieve a block quote like behavior.

Although not explicitly stated in PEP8, this is considered by many bad practice and in some occasions confusion about how docstrings work.

In light of this, I propose that triple quotes that are not docstrings, are not assigned to variables, nor contained in (), [] or {}, are treated as errors or raise a warning.

myint commented 12 years ago

I'd like something like this, but it seems like something that should be added to pylint.

florentx commented 12 years ago

They are not discouraged by the official PEP.

You have mention of them in PEP 257 where they just say they are allowed. http://www.python.org/dev/peps/pep-0257/#what-is-a-docstring

However I agree with you it's not something I recommend.

I close this issue, probably it will fit better in a larger tool like pylint. If someone contributes a simple checker for this, we might consider merging it in the library (and putting it in the DEFAULT_IGNORE list).

unode commented 12 years ago

I would say that PEPs have never touched the subject outside its use for docstrings. I haven't read anywhere in a PEP that you "can use triple quote for block commenting" nor that you shouldn't.

I'll consider transferring the request to pylint and pyflakes. Thanks

florentx commented 12 years ago

If you follow the link I posted above, you might read that:

String literals occurring elsewhere in Python code may also act as documentation. They are not recognized by the Python bytecode compiler and are not accessible as runtime object attributes (i.e. not assigned to __doc__) ...

This is the thing I was referring to.

myint commented 12 years ago

It turns out that pylint already supports this.


"""Real docstring"""

def testing():
    """Real docstring"""
    """
    "commented" out stuff
    x = 1
    """

    """More
    commented
    out stuff."""
$ pylint --report=no  blah.py 
************* Module blah
W:  8,-1:testing: String statement has no effect
W: 12,-1:testing: String statement has no effect
unode commented 12 years ago

That's neat, I didn't know that. Thanks for the info.

sigmavirus24 commented 12 years ago

syntastic(vim) + pyflakes will catch it too. I would expect that syntastic + pylint would too. And I agree with @florentx, that it shouldn't raise an error but I think pyflakes/pylint were just designed for PEP8 compliance but also for improving performance by cleaning up unnecessary items, e.g., unused imports, unused variable declarations, etc.

Edit see my comment below Unode's. virtualenv tricked me.

unode commented 12 years ago

@sigmavirus24 Which version of pyflakes are you using? I've started using flake8 recently and at least on pyflakes 0.5.0 it doesn't complain.

sigmavirus24 commented 12 years ago

@Unode seems that syntastic saw pylint first since I was using virtualenv, I was mistaken about that. Sorry for the confusion.