Static Python code analyzer, that tries to check if functions in code are pure or not and why.
Project description
mr. Proper
Static Python code analyzer, that tries to check if functions in code are pure or not and why.
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
- that function has no blacklisted calls (like
print
) and blacklisted attributes access (likesmth.count
); - that function not uses global objects (only local vars and function arguments);
- that function has al least one return;
- that function not mutates it's arguments;
- that function has no local imports;
- that function has no arguments of forbidden types (like ORM objects);
- that function not uses
self
,class
orsuper
; - 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
- Python 3.7+;
- Functions are fully type-annotated;
- No dynamic calls (like
getattr(sender, 'send_' + message_type)(message)
).
Contributing
We would love you to contribute to our project. It's simple:
- Create an issue with bug you found or proposal you have. Wait for approve from maintainer.
- Create a pull request. Make sure all checks are green.
- Fix review comments if any.
- Be awesome.
Here are useful tips:
- You can run all checks and tests with
make check
. Please do it before TravisCI does. - We use BestDoctor python styleguide.
- We respect Django CoC. Make soft, not bullshit.
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
Built Distribution
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03b517b19e617537f711ce418b125e5f2efd82ec881539cdee83195c78c14a02 |
|
MD5 | a74978c0e0059ee1c2582cf9884d5cd8 |
|
BLAKE2b-256 | e335a50ba9e3097ee0d71232c996c626a17f80424140b365122c8f1bc9933118 |
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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 74a1b60240c46f10ba518707ef72811a01e5c270da0a78b5dd2dd923d99fdb14 |
|
MD5 | 2d55f8fa5a3e495d140a1a4f103797bd |
|
BLAKE2b-256 | e5b02b19f1c38cc1ff29af21c1851c1160cfb185d4516834a37b7227d7df01b2 |