Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Framework for writing custom linting-rules in python?
I hope this somewhat open-ended question is allowed, I have used SO a lot and I would not post it there, but I have seen open-ended questions on this site
I have a situation where custom linting-rules for python-code would be very helpful[1].
Some random examples of rules I want to enforce:
-
All imports must be absolute, at top-level and reference existing modules.
-
All classes that define a class-variable
event_mapmust inherit fromproprietaryOrm.Objectandevent_mapmust have a certain structure -
ctx.set_object_resultandctx.urlmust not be called in the same function
So this is not random clean-code rules, this is very specific and I need to code them up using a combination of techniques, mostly AST-inspection and dynamic inspection of live-objects[2]. The question is not how to write these rules but where to put them. If I write a unittest, I put it in a test-framework. Now I don't have to worry about things like the test-runner and IDE-integration. If I write a linting-rule, I put it where?
I have not found any 'linting-framework' online, but maybe I just did not find it. Finding such a thing would be ideal. But if there is nothing like this, is there at least some standard for integrating linters into IDEs? "Output your linter-violations in this specific format and this standard plugin can pick it up". Or can you bend a regular test-framework like pytest to do this?
-
Specifically, my python-code is loaded into a proprietary application, which means simple errors in the code can be very hard to trace. There are also consistency rules that have to do with the framework, not the python-language, and they are painful-to-discover and not well-known. ↩︎
-
I don't know if inspecting live objects is technically still considered 'linting' or 'static code analysis' in the literature since I am importing code and thereby executing something, but anyways. My point is, the tool should validate code without the programmer having to write a test. ↩︎

2 comment threads