Skip to main content

un

Analyzing classes

To install: pip install un

The un package provides a suite of tools for analyzing Python classes, particularly focusing on method resolution order (MRO), method overrides, and interactions with superclass methods. It allows users to inspect how methods are resolved across class hierarchies, identify which classes implement specific methods, and trace method calls to super. This can be particularly useful for debugging complex inheritance structures and ensuring that method overrides behave as expected.

Main Features

  • Method Resolution Analysis: Trace the resolution path of any method through the MRO of a class.
  • Super Method Calls: Identify calls to super() within methods, helping to understand how subclasses interact with superclass methods.
  • Class and Method Identifiers: Customize how classes and methods are identified in outputs, with options for full module paths, class names, or direct class objects.
  • DataFrame Outputs: Generate Pandas DataFrame objects that neatly represent method resolutions and class methods for easier analysis.
  • MRO Visualization: Print or visualize the method resolution order with options to include indents for better readability.

Usage Examples

Analyzing Method Resolutions

You can analyze how methods are resolved within a class hierarchy using the method_resolutions function. This function returns a dictionary where keys are method names and values are lists of classes that resolve these methods, in order.

from un import method_resolutions

class A:
    def foo(self):
        return 42

class B(A):
    def foo(self):
        super().foo()

resolutions = method_resolutions(B, ['foo'], cls_identifier='name')
print(resolutions)

DataFrame of Method Resolutions

For a more visual representation, you can convert the method resolutions into a Pandas DataFrame:

from un import df_of_method_resolutions

df = df_of_method_resolutions(B, ['foo'], cls_identifier='name')
print(df)

Printing the MRO with Indents

To visualize the MRO of a class with indents highlighting the inheritance structure:

from un import print_mro

print_mro(B)

Finding Methods Calling Super

To find out if a method in a class calls a super method of the same name:

from un import method_calls_super_method_of_same_name

print(method_calls_super_method_of_same_name(B.foo))

Documentation

Below is a brief documentation of some key functions and classes:

  • method_resolutions(cls, methods=None, cls_identifier='name', method_not_found_error=True, include_overridden_methods=False): Analyze method resolutions for a given class.
  • df_of_method_resolutions(cls, methods=None, cls_identifier='name', method_not_found_error=True, include_overridden_methods=False): Returns a DataFrame representing the method resolutions of a class.
  • print_mro(cls, indent=4): Print the MRO of a class, visually indented to show the hierarchy.
  • MethodNotFoundInMro: Exception raised when a method is not found in the MRO of a class during analysis.

These tools are designed to help developers understand and debug class hierarchies in Python, making it easier to work with complex object-oriented code.

Metadata

Release files for un 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 un 0.0.7
File Size Uploaded
un-0.0.7.tar.gz 9.9 kB Details

Built distribution (wheel)

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

Total release size: 19.6 kB

Release files / un-0.0.7.tar.gz

Download URL un-0.0.7.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
31c058abcd3b6d7f0c18672a8481e1a9956c1136263487889a0441d0e22d50e2
BLAKE2b-256 checksum
How to use checksums
8593a8031bd5a0550fbadafe803475eac37487df9bee522cd5c22b38adc7ce48
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

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

Download URL un-0.0.7-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8030a5d6eca48d24870f6568136145da25ad560235502c2316de9f2acd1a32fb
BLAKE2b-256 checksum
How to use checksums
f1606ce4965c71916179041fe9b98418606ce3383c00b3a1593f987f6a3309fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.0.7 This release

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

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