Skip to main content

Analyzing classes

Project description

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.

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

un-0.0.6.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

un-0.0.6-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file un-0.0.6.tar.gz.

File metadata

  • Download URL: un-0.0.6.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for un-0.0.6.tar.gz
Algorithm Hash digest
SHA256 fbe827f4c15028efaf36ce385b8c6a5c961f650050054b46e9ff990ab31bcf76
MD5 18eb929e728dc8e4f3382170900cebd7
BLAKE2b-256 1e46db5055a29b1435483d33dc7ed90177a9d64f15b1d4148b75e4643c1920e2

See more details on using hashes here.

File details

Details for the file un-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: un-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.13

File hashes

Hashes for un-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 721e6369387b2be2cd2624926066934c58548282f789f34f8e6874481070d574
MD5 50169dfa818da86c138b44b0474d4191
BLAKE2b-256 25cf3349bea100dec57f84f05f85770d330a83dbf2bd63c1f73019541bad93fc

See more details on using hashes here.

Supported by

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