Skip to main content

License: MIT Python 3.6 and Up Tests Status

🧙 repromancy 🧟

repromancy utilizes dark magic to make better reprs, especially when dynamically creating classes and functions.

What?

In certain circumstances Python can give ugly reprs. For example, if we had a package spam that had a submodule ham that defined the Ham class. The repr is:

<class 'spam.ham.Ham'>

All good, but let's say we actually import Ham directly in the spam package, such that it is accessible as spam.Ham. In fact, this is the interface we advertise to users, but when they do reprs on their Hams they get this extra ham module that they don't really care about! What we would like to see is:

<class 'spam.Ham'>

Since that is how we define our public API, the underlying modules aren't really relevant.

repromancy fixes this, and more. at a price...

How?

repromancy.cast is used to perform the dark magic required to fix your reprs. For a module, it's as simple as adding the following to the bottom of your module:

if __debug__:
    import repromancy
    repromancy.cast(repromancy.CallingModule)
    del repromancy

This will perform repromancy on everything within current module namespace. Note that cast is recursive, it will follow everything it can, but will only modify things in the same package as what was cast on.

repromancy.cast can be used on most python objects (not builtins).

Dynamic Classes and Functions

Let's say we have a function called factory, that returns a newly created class (or it could be another function, but we'll use a class in this example):

>>> def factory():
...     class Class:
...         pass
...     return Class
...
>>> factory()
<class '__main__.factory.<locals>.Class'>

The default repr is not terribly useful, especially because all *classes generated by the factory will have the same repr unless explicitly defined by the factory function itself. Let's say that in our module we call this factory multiple times and give the results local names, then cast repromancy on the module:

>>> ClassA = factory()
>>> ClassB = factory()
>>> import repromancy
>>> repromancy.cast(repromancy.CallingModule)
>>> ClassA
<class '__main__.ClassA'>
>>> ClassB
<class '__main__.ClassB'>

repromancy makes the reprs more clear.

🐐 Dark magic? ☣️

repromancy, as its name implies, make use of some questionable methods to get the results it does. Specifically, it monkey patches some cpython builtin types, altering their __repr__ methods. This is a global change to the python interpreter which affects all modules and subinterpreters. As such, it is recommended to only perform repromancy when __debug__ is defined.

The specific changes are as follows:

  • Instances of type will not display their package in their repr if the fully qualified name starts with '<' (another repr)
  • functions include the module in which they were defined in their repr, this is to be consistent with object and type reprs
  • methods show the repr for their function, rather than just the name

Metadata

Release files for repromancy 0.0.1

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

Source distribution (sdist)

Source distribution for repromancy 0.0.1
File Size Uploaded Provenance Actions
repromancy-0.0.1.tar.gz 12.6 kB Without provenance Details for repromancy-0.0.1.tar.gz

Release files / repromancy-0.0.1.tar.gz

Download URL repromancy-0.0.1.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8e8fdebc3060e45d449b8280b67f928eb559e60f18e4836041dc5a77fcf14795
BLAKE2b-256 checksum
How to use checksums
feb11e705604a183477d456a640917c6f995a7409949f011c08b3af0228fea42
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.9.0a6

Security for repromancy 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file No provenance

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