Skip to main content

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:

Release files for mr-proper 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mr-proper 0.0.7
File Size Uploaded
mr_proper-0.0.7.tar.gz 11.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for mr-proper 0.0.7
File Interpreter ABI Platform
mr_proper-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 22.6 kB

Release files / mr_proper-0.0.7.tar.gz

Download URL mr_proper-0.0.7.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
03b517b19e617537f711ce418b125e5f2efd82ec881539cdee83195c78c14a02
BLAKE2b-256 checksum
How to use checksums
e335a50ba9e3097ee0d71232c996c626a17f80424140b365122c8f1bc9933118
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / mr_proper-0.0.7-py3-none-any.whl

Download URL mr_proper-0.0.7-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
74a1b60240c46f10ba518707ef72811a01e5c270da0a78b5dd2dd923d99fdb14
BLAKE2b-256 checksum
How to use checksums
e5b02b19f1c38cc1ff29af21c1851c1160cfb185d4516834a37b7227d7df01b2
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page