Skip to content

igrek51/wat

Repository files navigation

🙀 WAT

Deep inspection of Python objects.

WAT is a powerful inspection tool that allows you to delve into and examine unknown objects at runtime.

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

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

Start the Python Interpreter (or attach to one) and execute wat / object on any object to investigate its type, formatted value, variables, methods, parent types, signature, documentation, and even its source code. Alternatively, you can use wat(object) syntax.

wat-demo.mp4

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+nGKg/SDqbrRS3KbAJ3Sq26gZw4kJRGhiSQXCXXIkxl1yQXEvqYoE8RJ8hD5Yn6bnMneSuHPlHK8ASOTPnmzPnnDmX4XjZ1CuRpV26KNO2zVtRrNZ105mmJ7KhqNp1vujUa607mlw9tfftkyXidffrorpWUKfV/US8WXdFXaXlRFzcr/MnfzP49Ft8y/Av6mpZXM+eCPhpb4B6JuZ1XdJ7tqmyvLEaqjqrF63VUNbVtfW6qLPcfk3LUtOPMXDadU0x33Q581ClK4Bou4bePqTlBl5hQfQKy4Q3Wo/CT+dlvo/ldVN8SDt7SFtcV2m3aaBNyegS5nvH9PXCb87ypdJFsqybVdqF9fzniXg6YYHFf0/LNp/IqdUbi0q9oZzUMwpJP5OE1Bu88GM0k+JE3cSOpkKek37rOfmPqBuE0HPzHzk5/tIDiAX8ZVqYEf4jWyNiot50600Xl0Xbhcl9kZdZoqRRFlXesjCY1YhJiiVa5rTtMiCeFm3adfdhJNIqE8miLusmySvUWxbKhZJu82ZVgNCT2yLrbuK6nV7nXaJb2+LfeRhNgXyzqlpNxdxNgaO86cLjifjh4u3rs+Sb1z+eic9E8Nt/fgnEUw8bOs7Pfji7iHyUdL3Oqyz8aIguv+vi4KoKpj/XRRWGKBcBpiLooagkPsqFW1pQUSe+r6vcSAxb9kkHpkjAHos1Dwq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w2G8plP6Qw4cQa8/vHMEP7JJ7S6T+xuedPNUP5Zd714e2ot4ktrdadvZfsXXz37C7SrhCb6L38UG0Q='
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 object 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 __)
  • .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

You can chain modifiers, e.g. wat.long.dunder.nodocs / foo.

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

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.

wat.code / wat.__call__

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