Skip to content

igrek51/wat

Repository files navigation

🙀 WAT

Deep inspection of Python objects.

WAT is a powerful inspection tool designed to help you explore unknown objects and examine them at runtime.

"Wat" is a variant of the English word "what" that is often used to express confusion or disgust

If you ever find yourself in a Python console, feeling lost and confused, and wondering "WAT? What is this thing?", that's where wat inspector comes in handy.

Launch the Python Interpreter and execute wat / object on any object to investigate its type, formatted value, variables, methods, parent types, signature, documentation, and its source code. This makes it particularly useful for debugging or understanding intricate data structures in Python, providing a straightforward way to answer "what" exactly an object represents.

wat-demo.mp4

Alternatively, use wat(object) syntax for the same in-depth inspection.

Loading

Insta-Load

If you want to quickly debug something, you can use this inspector without installing anything, in the same session.

Load it on the fly by pasting this snippet to your Python interpreter:

import base64, zlib
code = b'eJzVW+tu3MYV/u+nINQfJJ3N1orbFNiEbhVbdQM4caEoDQzJILjLWYkxl1yQXEvqYoE8RJ8hD5Yn6bnMneTuOvaPVoAlcmbON+ecOXMuw/GyqVdBnnXZoszaVrRBsVrXTWeaHsmGomrXYtGp11p3NEI9tQ/toyXidQ/rorpRUGfVwyR4ve6KusrKSXD5sBaP/mbw6XfwLcM/r6tlcTN7FMBPewvUs2Be1yW955sqF43VUNV5vWithrKubqzXRZ0L+zUrS00/xsBZ1zXFfNMJ5qHKVgDRdg29vc/KDbyCQPQKYsIbyaPws3kp9rG8bor3WWcPaYubKus2DbQpHV3BfG+Zvl74zblYqrVIl3Wzyrqonv88CR5PWGHJ37OyFRM5tXpjVak31JN6RiXpZ9KQeoMXfoxnUp24NomzUhHPSb/1nPwnqBuE0HPzHzk5/tIDiAX8ZVqYEf4jW2Niot50602XlEXbRelDIco8Vdooi0q0rAxmNWaSYomWOW27HIinRZt13UMkZaJlFM2qAP2md0Xe3SZ1O70RXapb2+LfIoqni7rcrKpWUzEjU5hcNF30ZBL8cPnm1Xn6zasfz4PPgvC3//wSBo89bOi4OP/h/DL2UbL1WlR59MEQjQDLqYLwugqnP9dFFUWohADsIqCHopIzoBK4pYX16ILv60qAetCYDm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Y/n/lmn0xZLnxe+VgclcQCfkRklioIMpLr+4ZlQXWLxwtGPZUCE1WAIP6Ujtd5B1LN6gsA399fzq/erIKH9nnMLL5VDdfnL9QQ796euqNtnpP7V6qWw3ZFz6Z3X9q93MRaSif+pTOgFNnwKsfzw3hn3xCq/vU7pZ3Dw3ln3XX8zdnlhBfWtKdvZHtX3z19C/QrhLD+L8eNHN0'
exec(zlib.decompress(base64.b64decode(code)).decode(), globals())

Now you can use wat object.

Warning

Before executing Insta-Load snippet, it's recommended to verify what you're about to run. You can either:

  • Verify what's inside the extracted code beforehand:
    print(zlib.decompress(base64.b64decode(code)).decode())
  • Paste the content of inspection.py into your interpreter. It has the same effect.
  • Install package with pip and review the code.

Install with pip

Alternatively, install wat-inspector package and import inspection tool from wat module:

pip install wat-inspector
import wat

This package has no external dependencies.

Usage & modifiers

wat can quickly inspect things by using the division operator (for faster typing without parentheses). A short syntax wat / foo is equivalent to wat(foo).

You can call wat.modifiers / foo with the following modifiers:

  • .short or .s to hide the attributes (variables and methods inside the object)
  • .dunder to display dunder attributes (starting with double underscore)
  • .code to reveal the source code of a function, method, or class
  • .long to show non-abbreviated values and docstrings
  • .nodocs to hide documentation for functions and classes
  • .caller to show how and where the inspection was called (works in files, not REPL)
  • .all to include all available information
  • .ret to return the inspected object
  • .str to return the output string instead of printing it
  • .gray to disable colorful output in the console
  • .color to enforce colorful outputs in the console

You can chain modifiers, e.g. wat.short.str.gray / 'foo'.

Call wat.locals or wat() to inspect local variables.
Call wat.globals to inspect global variables.

You can explore any object. In Python, an "object" refers to not only to data structures, but also to functions, classes, modules, built-in types, and more.

Type wat in the interpreter to learn more about this object itself.

There are several alternative syntaxes that are equivalent. Choose the one that works best for you:

wat.short / 'foo'  # fast typing
wat.short('foo')
wat('foo', short=True)  # natural Python syntax
'foo' | wat.short  # Unix piping

Use Case Examples

Determine type

In a dynamic typing language like Python, it's often hard to determine the type of an object. WAT Inspector can help you with that by showing the name of the type with the module it comes from.

>>> wat.short / (1,)
value: (1,)
type: tuple
len: 1
>>> wat.short / {None}
value: {None}
type: set
len: 1
>>> wat.short / user
str: admin
repr: <User: admin>
type: django.contrib.auth.models.User
parents: django.contrib.auth.models.AbstractUser, django.contrib.auth.base_user.AbstractBaseUser, django.contrib.auth.models.PermissionsMixin, django.db.models.base.Model, django.db.models.utils.AltersData

Now that you've identified the actual type, you can put the type annotations in your code to reduce further confusion.

Look up methods

By listing out methods with their signatures and docstrings, you can easily grasp how to use the unknown object.

wat / ['foo']


Use wat.long if you want to see full doscstrings.

Discover function's signature

See the docstrings and the signature of a function to learn how to use it.

wat / str.split

Look up attributes

List the attribues and their types to see what's really inside the inspected object.

wat / re.match('(\d)_(.*)', '1_title')

Explore modules

Another use case is to explore modules. You can list the functions, classes and sub-modules of a selected module.

import pathlib
wat / pathlib

Then, you can navigate further, e.g. wat / pathlib.fnmatch.

Explore dunder attributes

By default, WAT Inspector hides attributes starting with __. Use wat.dunder to see them.

wat.dunder / {}

Review the code

Look up the source code of a function to see how it really works.

import colorsys
wat.code / colorsys.hsv_to_rgb

Prettify unreadable collections

Nested dictionaries and lists get nicely formatted, indented output:

Debug with breakpoint

You can use Python's breakpoint() keyword to launch an interactive debugger in your program. Attach to the interpreter and inspect things on the spot.

(Pdb) import wat  # or paste Insta-Load snippet
(Pdb) wat / foo  # inspect local variables
...
(Pdb) c  # continue execution

Look up local variables

Use wat.locals or wat.globals to look up the local and global variables respectively.

Learn Python

With these snippets you can better understand Python internals.

reversed([]) == reversed([])
# False
wat.s / reversed([])
# value: <list_reverseiterator object at 0x76749d1cb400>
# type: list_reverseiterator
wat / type('ObjectCreator', (), {})
# value: <class '__main__.ObjectCreator'>
# type: type
# signature: class ObjectCreator()

wat / type
# value: <class 'type'>
# type: type
# signature: class type(…)
# """
# type(object) -> the object's type
# type(name, bases, dict, **kwds) -> a new type
# """
# 
# Public attributes:
#   def mro(self, /) # Return a type's method resolution order.
from typing import List
wat.s / List[str]
# value: typing.List[str]
# type: typing._GenericAlias
# parents: typing._BaseGenericAlias, typing._Final
# signature: def List(*args, **kwargs)

wat(str | None)
# value: str | None
# type: types.UnionType

Explore Python built-ins:

wat / __builtins__
wat / ...

Inspect WAT itself

wat.dunder / wat
wat.code / wat.__truediv__

Environment variables

  • WAT_COLOR="false" to disable colorful output in the console.
  • WAT_COLOR="true" to enforce colorful outputs even in non-tty environment.

References