Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mr-proper

Package Overview
Dependencies
Maintainers
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mr-proper

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

  • 0.0.7
  • PyPI
  • Socket score

Maintainers
3

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:

Keywords

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc