\( \newcommand{\NOT}{\neg} \newcommand{\AND}{\wedge} \newcommand{\OR}{\vee} \newcommand{\XOR}{\oplus} \newcommand{\IMP}{\Rightarrow} \newcommand{\IFF}{\Leftrightarrow} \newcommand{\TRUE}{\text{True}\xspace} \newcommand{\FALSE}{\text{False}\xspace} \newcommand{\IN}{\,{\in}\,} \newcommand{\NOTIN}{\,{\notin}\,} \newcommand{\TO}{\rightarrow} \newcommand{\DIV}{\mid} \newcommand{\NDIV}{\nmid} \newcommand{\MOD}[1]{\pmod{#1}} \newcommand{\MODS}[1]{\ (\text{mod}\ #1)} \newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\Q}{\mathbb Q} \newcommand{\R}{\mathbb R} \newcommand{\C}{\mathbb C} \newcommand{\cA}{\mathcal A} \newcommand{\cB}{\mathcal B} \newcommand{\cC}{\mathcal C} \newcommand{\cD}{\mathcal D} \newcommand{\cE}{\mathcal E} \newcommand{\cF}{\mathcal F} \newcommand{\cG}{\mathcal G} \newcommand{\cH}{\mathcal H} \newcommand{\cI}{\mathcal I} \newcommand{\cJ}{\mathcal J} \newcommand{\cL}{\mathcal L} \newcommand{\cK}{\mathcal K} \newcommand{\cN}{\mathcal N} \newcommand{\cO}{\mathcal O} \newcommand{\cP}{\mathcal P} \newcommand{\cQ}{\mathcal Q} \newcommand{\cS}{\mathcal S} \newcommand{\cT}{\mathcal T} \newcommand{\cV}{\mathcal V} \newcommand{\cW}{\mathcal W} \newcommand{\cZ}{\mathcal Z} \newcommand{\emp}{\emptyset} \newcommand{\bs}{\backslash} \newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor} \newcommand{\ceil}[1]{\left \lceil #1 \right \rceil} \newcommand{\abs}[1]{\left | #1 \right |} \newcommand{\xspace}{} \newcommand{\proofheader}[1]{\underline{\textbf{#1}}} \)

B.3 python_ta

PythonTA is a Python program that analyses Python code to help students find and fix common coding and style errors. Unlike testing libraries like doctest or pytest, PythonTA does not actually run your code. Instead, it analyzes the program text directly, looking for common patterns of code that oftne lead to errors.PyCharm does something very similar, which is why you’ll see red or yellow highlighted text in your Python files as you’re working, before running the file.

Running PythonTA

To run PythonTA on a Python file, put the following code at the bottom of the file you want to check:

if __name__ == '__main__':
    import python_ta
    python_ta.check_all()

When you run this file, you’ll see a report open up in your web browser that shows any errors that PythonTA detected. These errors are divided into two broad categories:

We recommend running PythonTA regularly as you’re working on an assignment, as it can be a useful way to check your work and improve the quality of your code. If you’re ever stuck, try taking a break and running PythonTA and fixing any errors it finds for you! This is a way to develop good programming habits and style, which will come in handy in this course (and all future courses).

Cleaning up

When you run PythonTA, it generates a new report file called pyta_report.html in the same folder as the file you’re checking. After you’re done running PythonTA, you can safely delete this report file.

Checking contracts using PythonTA