[go: up one dir, main page]

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/SDqbrRS3KbAJ3Sq26gZw4kJRGhiSQXCXXIkxl1yQXEvqYoE8RJ8hD5Yn6bnMneSuHPlHK8ASOTPnm3POnDmX4XjZ1CuRpV26KNO2zVtRrNZ105mmJ7KhqNp1vujUa607mlw9tfftkyXidffrorpWUKfV/US8WXdFXaXlRFzcr/MnfzP49Ft8y/Av6mpZXM+eCPhpb4B6JuZ1XdJ7tqmyvLEaqjqrF63VUNbVtfW6qLPcfk3LUtOPMXDadU0x33Q581ClK4Bou4bePqTlBl5BIHoFMeGN5FH46bzM97G8booPaWcPaYvrKu02DbQpHV3CfO+Yvl74zVm+VGuRLOtmlXZhPf95Ip5OWGHx39OyzSdyavXGqlJvqCf1jErSz6Qh9QYv/BjNpDpxbWJnpUKek37rOfmPqBuE0HPzHzk5/tIDiAX8ZVqYEf4jWyNiot50600Xl0Xbhcl9kZdZorRRFlXesjKY1YhJiiVa5rTtMiCeFm3adfdhJNIqE8miLusmyStctyyUgtLa5s2qAKUnt0XW3cR1O73Ou0S3tsW/8zCaAvlmVbWairmbAkd504XHE/HDxdvXZ8k3r388E5+J4Lf//BKIpx42dJyf/XB2Efko6XqdV1n40RBdftfFwVUVTH+uiyoMUS8CTEXQQ1FJfNQLt7SwRJ34vq5yozFs2acdmCIBeyzWPCjEF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17HYSFUDuOm7k/nl8Sp4Ylf3svlEN5+fvVRDv3p24o22ek/sXiqqDNkXPpndf2L3c41jKJ/5lM6AE2fA6x/PDOGffEKr+8TulvfyDOWfddeLt6eWEF9a0p2+le1ffPXsL9CuspjovzEkUFo='
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.short.str.gray / 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.

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