johnfraney / flake8-markdown

Lints Python code blocks in Markdown files using flake8
MIT License
23 stars 8 forks source link

Is there a way to lint doctest compatible code examples. #1

Closed proofit404 closed 5 years ago

proofit404 commented 5 years ago

Hi, thank you for the meaningful package!

I prefer to write my code examples to be compatible with python doctest module.

>>> class broken( object ):
...     def IsThereAReason(notself, **k):
...         pass

It would be great if we will be able to lint this code :)

P.S. Is there a chance you will start to use complete markdown parser and run flake8 through API and not subprocesses?

I understand this approach works right now, but in future, it will case support troubles.

Regards, Artem.

johnfraney commented 5 years ago

@proofit404 You're welcome! That's an interesting idea. It would be possible to add an additional regex rule to lint a pycon block like this:

```pycon
>>> class broken( object ):
...     def IsThereAReason(notself, **k):
...         pass
```

I'd just have to strip the first 4 characters off each line. That sounds like a fun feature to add.

About the subprocess: I was just looking today at flake8-rst, which I did realize existed when I started working on this. I'll see what I can learn from that project.

johnfraney commented 5 years ago

@proofit404 I've got a working version of this feature. Given this file, flake8-markdown outputs these errors:

tests/samples/pycon.md:10:11: F821 undefined name 'characterr'
tests/samples/pycon.md:17:10: E999 SyntaxError: EOL while scanning string literal
tests/samples/pycon.md:25:1: F821 undefined name 'false'

I want to clean up the code some more and add more tests before I publish this. You can try this feature in the lint-pycon-blocks branch.

After this feature release, I'm going to turn this into a proper Flake8 plugin. Thanks again for the suggestions!

proofit404 commented 5 years ago

Good job! Thanks for the time you have invested in it.

johnfraney commented 5 years ago

@proofit404 Thanks, thanks! I should be able to get this published this evening. One quirk that I'll mention in the README is that the code examples should end with a newline. Flake8 complains about the "file" not ending with a newline, and doctest needs it, too. Without the newline, doctest thinks that the three backticks are part of the expected output.

proofit404 commented 5 years ago

:tada: :cake: :tada: