Python code check tool

Python code check tool

The latest version of PyChecker is 0.8.19

PyChecker is a tool for finding bugs in python source code. It finds problems that are typically caught by a compiler for less dynamic languages, like C and C++. It is similar to lint. Because of the dynamic nature of python, some warnings may be incorrect; however, spurious warnings should be fairly infrequent.

PyChecker works in a combination of ways. First, it imports each module. If there is an import error, the module cannot be processed. The import provides some basic information about the module. The code for each function, class, and method is checked for possible problems.

Types of problems that can be found include:

  • No global found (e.g., using a module without importing it)
  • Passing the wrong number of parameters to functions/methods/constructors
  • Passing the wrong number of parameters to builtin functions & methods
  • Using format strings that don’t match arguments
  • Using class methods and attributes that don’t exist
  • Changing signature when overriding a method
  • Redefining a function/class/method in the same scope
  • Using a variable before setting it
  • self is not the first parameter defined for a method
  • Unused globals and locals (module or variable)
  • Unused function/method arguments (can ignore self)
  • No doc strings in modules, classes, functions, and methods
Читайте также:  ' . $form_subject . '

Using PyChecker

To use PyChecker, pass options and the python source files (or packages) you want to check on the command line:

pychecker [options] file1.py file2.py .

If you want to change the default behaviour, you can pass command line options or define a .pycheckrc file. For an example, look at pycheckrc.

will show the available options. There is a simple GUI which is not maintained much. It is good for showing all the options and also allows you to run pychecker. To run options, you will need to start it manually:

python pychecker/options.py

If you want to suppress warnings on a module/function/class/method, you can define a suppressions dictionary in .pycheckrc. Examples of keys are: ‘module’, ‘module.function’, ‘module.class’, ‘module.class.method’, etc. You can also define suppressions in your code by doing:

__pychecker__ = 'no-namedargs maxreturns=0 unusednames=foo,bar'

The format for __pychecker__ values and values in the suppressions dictionary are the same. Dashes (—) are optional when preceding long option names.

Importing PyChecker

This will allow each module imported after PyChecker to be checked (other than the main module). NOTE: Modules imported before PyChecker will not be checked. Warnings will be displayed on stdout (ie, PyChecker uses print). Since you can’t pass command line parameters, you can do:

os.environ['PYCHECKER'] = 'command line options here'
PYCHECKER='no-namedargs maxreturns=0' /path/to/your/program

If you want to disable the warnings (and processing done by PyChecker), prior to importing PyChecker, do:

os.environ['PYCHECKER_DISABLED'] = 1
PYCHECKER_DISABLED=1 /path/to/your/program

Internal Errors

pychecker myfile.py myfile.py:13 INTERNAL ERROR -- STOPPED PROCESSING FUNCTION -- Traceback (most recent call last): File "./pychecker/warn.py", line 364, in _checkFunction stack, oparg, lastLineNum) File "./pychecker/warn.py", line 195, in _handleFunctionCall kwArgs.append(stack[i].data) IndexError: list index out of range

Please post a bug in the SourceForge Tracker or send mail indicating the version of PyChecker, your source file which broke PyChecker (myfile.py in the example above), and the traceback. It is very helpful to provide a simple test case to demonstrate the problem. It helps to have the entire file and all the dependencies if you cannot produce a simple test case. But if you can’t provide a test case nor the file(s), I may be able to figure out the problem with just the line which broke PyChecker (myfile.py:13 in the example above).

Buildbot

IRC

Projects using PyChecker

The project page for PyChecker can be found on SourceForge.

Good Luck! As always, feedback is greatly appreciated.

Источник

Python syntax checker

Python checker allows to check your Python code syntax (Python 3), and find Python errors. This Python code checker tool highlights and goes to line with a syntax error.

To check your code, you must copy and paste, drag and drop a Python file or directly type in the Online Python editor below, and click on «Check Python syntax» button.

You can see the user guide to help you to use this python checker tool.

User guide

  • First, Drag and drop your Python file or copy / paste your Python text directly into the editor above.
  • Finally, you must click on «Check Python syntax» button to start code checking.

It is quick and easy to analyze python code!

Python code checker tool

Python is a server-side scripting language, but can also be used as a general-purpose programming language.

Python error checker tool allows to find syntax errors (lint). You can test your Python code online directly in your browser.

If a syntax error is detected, then the line in error is highlighted, and it jumps to it to save time (no need to search the line).

It can be useful to make online test to save time (deployment . ).

Note: This tool no longer offers sandbox, it was not good enough.

About Python

Python is an interpreted programming language, it features a dynamic type system and automatic memory management (garbage collector). Python is available for many operating systems.It has a large standard library.

Python formatting is visually uncluttered, and often uses English keywords rather than punctuation. Python uses whitespace indentation instead of curly brackets to delimit blocks.

Python is often used as a programming language in high school and higher education, especially in France (I am French).

Источник

5 Awesome Tools for Python Code Quality

Code quality is critical to keeping large projects healthy and progressing, but it’s hard to maintain consistent quality by hand. However, there are a bunch of tools for Python code that will make this so much easier.

Like several other topics in software development, what tools to use comes down to preference. Do you use a tool not on this list? Feel free to let me know what I am missing! What follows is my personal preference in tools as of 2019.

1. Pytest — Essential Unit Testing

Python has a built in unit testing toolkit in the unittest module, but it’s not always the right fit for every project. I prefer using Pytest whenever possible because the experience is so simple.

Why It’s Good

When it’s easier to write unit tests, you’ll write more of them. Pytest makes it a breeze to create new tests. Just add a new Python file that starts with test , write a few functions, and you’re already done.

There are a massive amount of plugins for Pytest that can be configured through a pytest.ini file. It’s also easy and painless to write your own plugins too! And of course, Pytest «just works» with class based tests from unittest , too.

How To Use It

Say you were writing a test to see if addition works, and in this alternate universe it also didn’t bother you this exercise seemed so contrived. You might write a pytest file like this in a tests directory:

"""An Example Pytest Test""" import time import pytest def test_plus_one(): """Test some addition.""" x = 8 + 1 assert x == 9 @pytest.mark.integration def slow_test(): """A really slow integration test.""" time.sleep(5) assert 1 == 1

You would also write up a pytest.ini file to register the «slow» marker, otherwise you will get an unpleasant warning:

[pytest] markers = integration: Integration test.

And. that’s it. That’s all it takes to create new Pytest tests. You can run them by installing Pytest from PyPi and running the pytest command.

pip install pytest pytest

2. Pylint — Style Checking and Error Spotting

Pylint is an intelligent code analysis tool that understands your Python code. It catches common errors like misspelled variable names, to sneakier problems like non-returning branches.

Why It’s Good

Unlike a lot of other tools for linting Python code, Pylint can spot some logical errors or code that will result in exceptions. It also enforces a lot of best practices, such as keeping try . except blocks focused on a few specific exception classes, and avoiding bad default arguments like empty lists or dictionaries.

Pylint is unrivaled in strictness- it has a lot of checks for everything from superfluous warnings to serious errors. The downside to using Pylint is sometimes the check is just wrong; you’ll want to keep your .pylintrc file close when you know you’re right.

How to Use It

Like most Python tools, you can download Pylint from PyPi using pip :

pip install pylint # Lint your project with the module (folder) name pylint my_project

You might want to also create a .pylintrc file for your project. This file should be checked into source control, as well. Pylint can generate a default .pylintrc file for you, too:

pylint --generate-rcfile > .pylintrc

There are also plugins to integrate Pylint with Pytest, if you don’t feel like using a task runner or builder like GNU Make.

3. Black — Code Formatting for Python

Why spend energy beautifying your code, when you can outsource the work to a computer? Black is a code formatter for Python that’s fast and 100% static- it doesn’t import code to format it.

Why It’s Good

Personally, while I love clean and pretty Python code, it is exhausting to format code by hand. It’s so much easier to just throw lines into a source file without worrying about line wrapping, indentation, or long function signatures.

Black takes away all this responsibility from the programmer, instead formatting the code itself. It’s fast and efficient with defaults that reduce «diff» sizes. I have no complaints about Black- I love it.

How To Use It

Just install Black from PyPi and format your files. You’re good to go! 🚀

pip install black # Format everything in my_project/ black my_project

For CI/CD pipelines, you might want to check that all code is formatted with Black. That’s what the check option is for:

—check will fail the tests if the code isn’t all formatted already.

4. Coverage — Easy Code Coverage for Python

If you’re going to the trouble of writing unit tests, you might as well check if you missed a spot. Coverage is an easy way to check code coverage using almost any existing tool.

Why It’s Good

Test coverage, while not a silver bullet, is a great way to maintain a quality code base. It becomes easy to see what the weakest test areas in the project are- just check the reports!

When you are writing new features, a lackluster coverage report can help nudge you in the right direction. I find having a target number helps a lot with my motivation to test.

How To Use It

Like everything else on this list, Coverage is installed from PyPy using pip :

To measure coverage of your source code, pass in the directory to coverage run :

# With Pytest coverage run --source my_project pytest # View the reports! coverage report

There’s also a great plugin for Pytest that will run coverage alongside Pytest. This is great because you can see the coverage numbers every test. 😎

5. MyPy — Type Checking for Python

This is a relatively new addition to my list, but so far I like MyPy for typechecking Python. Sometimes there are «gotchyas» if your code (ab)uses the dynamic features of Python, but for the most part it gets things right.

Why It’s Good

Having good unit test coverage can prevent a lot of TypeErrors , but MyPy can augment a good test suite by double checking how you use your types. Type annotations in Python also serve as a form of documentation, showcasing what exactly each function and class expects.

The only downside to MyPy is mostly the fact that Python is a dynamic language: MyPy needs types to be defined in external modules if you want to check how you use their code too, plus it doesn’t handle things like dynamic class creation.

How To Use It

MyPy is also easy to use. You should create a mypy.ini file, plus install it from PyPi.

Run MyPy against your source code directory using the mypy command.

If you don’t see anything (and the exit code is zero 😉), congrats- it’s working just fine, and your code passed!

Wrapping Up

Code quality in big projects can be a serious pain, but in a lot of areas our tools can help out. Drop a few of these tools into you CI/CD pipeline, it’s easy to get started! A great CI/CD pipeline can save a lot (A LOT) of headache down the road.

PSST: I created a cookiecutter that shows an example of how I integrate all these into my projects. If you are interested, check it out!

Copyright 2018-2019 Madelyn Eriksen

Источник

Оцените статью