Options for linting Cython code
Asked Answered
S

4

19

I have a Cython module that I would like to lint PEP8 style, however pylint syntax errors on Cython cdef syntax. Does anyone have a suggestion about how to maintain Python coding standards for Cython code?

Savitt answered 27/8, 2014 at 19:35 Comment(2)
I've not heard of an tools which can do this -- Although the Cython community might be grateful if you managed to figure out how to do it yourself ;-)Aluminum
I think you should piggy-back on cython the compiler to detect which blocks of code are C and which are Python. Once that is done, you could probably hack the linter and/or map Python blocks to syntactically-equivalent Python code that can be linted with existing tools.Trixy
M
4

I use Sublime Text editor with SublimeLinter Flake8 package.

My Flake8 config is:

"flake8": {
            "@disable": false,
            "args": [
                "--doctests"
            ],
            "builtins": "",
            "excludes": [],
            "ignore": "",
            "ignore_match": {
                "pyx": [
                    "SyntaxError"
                ]
            },
            "jobs": "1",
            "max-complexity": 7,
            "max-line-length": null,
            "select": "",
            "show-code": false
        },

"ignore_match" key is used to define regular expressions that ignore some reported errors by a linter.

In this case I used it to ignore syntaxis errors in .pyx files. You can define new expressions to meet your needs.

Sorry because it's not a Cython linter, it's just a trick to make Python linter useful.

More info in sublimelinter official docs.

Meliamelic answered 7/4, 2016 at 14:31 Comment(0)
M
5

Although not command-line based, PyCharm claims to have Cython support. Unfortunately, it's only available on the paid-version "Professional Edition".

Moskva answered 15/12, 2014 at 4:2 Comment(0)
M
4

I use Sublime Text editor with SublimeLinter Flake8 package.

My Flake8 config is:

"flake8": {
            "@disable": false,
            "args": [
                "--doctests"
            ],
            "builtins": "",
            "excludes": [],
            "ignore": "",
            "ignore_match": {
                "pyx": [
                    "SyntaxError"
                ]
            },
            "jobs": "1",
            "max-complexity": 7,
            "max-line-length": null,
            "select": "",
            "show-code": false
        },

"ignore_match" key is used to define regular expressions that ignore some reported errors by a linter.

In this case I used it to ignore syntaxis errors in .pyx files. You can define new expressions to meet your needs.

Sorry because it's not a Cython linter, it's just a trick to make Python linter useful.

More info in sublimelinter official docs.

Meliamelic answered 7/4, 2016 at 14:31 Comment(0)
L
1

As of 2022, you can use cython-lint

Install with

$ pip install cython-lint

Or use as pre-commit hook:

-   repo: https://github.com/MarcoGorelli/cython-lint
    rev: v0.10.1
    hooks:
    -   id: cython-lint
Layton answered 5/11, 2022 at 15:8 Comment(0)
E
0

Extending off @j-mulet's answer, a useful example is that Pandas keeps its own cython.cfg file as Cython-specific rules to search for across .pyx files.

[flake8]
filename = *.pyx,*.pxd
select = E501,E302,E203,E111, ...

You can then call tell flake8 where this lives:

$ flake8 -config <path-to-cython.cfg>
Embolus answered 31/12, 2020 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.