Skip to main content

Static Python code analyzer, that tries to check if functions in code are pure or not and why.

Project description

mr. Proper

Build Status PyPI version Maintainability Test Coverage

Static Python code analyzer, that tries to check if functions in code are pure or not and why.

Have fun with mr Clean

DISCLAIMER: this library is very experimental and has a lot of edge cases. Functions that mr. Proper marks as pure can be not pure, but they are usually cleaner than other functions.

Installation

pip install mr_proper

What mr. Proper check

  1. that function has no blacklisted calls (like print) and blacklisted attributes access (like smth.count);
  2. that function not uses global objects (only local vars and function arguments);
  3. that function has al least one return;
  4. that function not mutates it's arguments;
  5. that function has no local imports;
  6. that function has no arguments of forbidden types (like ORM objects);
  7. that function not uses self, class or super;
  8. that function has calls of only pure functions.

This list is not enought to say that function is pure and some points are quite controversial, but it's a nice start.

Example

Console usage:

    # test.py
    def add_one(n: int) -> int:
        return n + 1

    def print_amount_of_users(users_qs: QuerySet) -> None:
        print(f'Current amount of users is {users_qs.count()}')
$ mr_propper test.py
add_one is pure!
print_amount_of_users is not pure because of:
    it uses forbidden argument types (QuerySet)
    it calls not pure functions (print)
    it has no return

Usage inside Python code sample:

>>> import ast
>>> from mr_propper.utils import is_function_pure
>>> funcdef = ast.parse('''
    def add_one(n: int) -> int:
        return n + 1
''').body[0]
>>> is_function_pure(funcdef)
True
>>> is_function_pure(funcdef, with_errors=True)
(True, [])

Parameters

CLI interface:

  • filepath: path to .py file to check (directories are not supported for now);
  • --recursive: require inner calls to be pure for function pureness.

Code prerequisites

  1. Python 3.7+;
  2. Functions are fully type-annotated;
  3. No dynamic calls (like getattr(sender, 'send_' + message_type)(message)).

Contributing

We would love you to contribute to our project. It's simple:

  1. Create an issue with bug you found or proposal you have. Wait for approve from maintainer.
  2. Create a pull request. Make sure all checks are green.
  3. Fix review comments if any.
  4. Be awesome.

Here are useful tips:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mr_proper-0.0.7.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

mr_proper-0.0.7-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file mr_proper-0.0.7.tar.gz.

File metadata

  • Download URL: mr_proper-0.0.7.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for mr_proper-0.0.7.tar.gz
Algorithm Hash digest
SHA256 03b517b19e617537f711ce418b125e5f2efd82ec881539cdee83195c78c14a02
MD5 a74978c0e0059ee1c2582cf9884d5cd8
BLAKE2b-256 e335a50ba9e3097ee0d71232c996c626a17f80424140b365122c8f1bc9933118

See more details on using hashes here.

File details

Details for the file mr_proper-0.0.7-py3-none-any.whl.

File metadata

  • Download URL: mr_proper-0.0.7-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for mr_proper-0.0.7-py3-none-any.whl
Algorithm Hash digest
SHA256 74a1b60240c46f10ba518707ef72811a01e5c270da0a78b5dd2dd923d99fdb14
MD5 2d55f8fa5a3e495d140a1a4f103797bd
BLAKE2b-256 e5b02b19f1c38cc1ff29af21c1851c1160cfb185d4516834a37b7227d7df01b2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page