Skip to content
David Moore edited this page Feb 28, 2025 · 7 revisions

Python code for computing Federal and CA state taxes

Introduction

The repository contains a subdirectory each tax year with code for computing tax returns for those years.

Disclaimer: This code comes with no warranty. It will probably not work for your tax return or specific circumstances without modification. I am not a tax accountant and this is not tax advice.

As of 2024, I have implemented the following tax forms:

  • Form 1040 and Schedules 1, 2, 3, A, D, and SE
    • Schedule B (Interest and Ordinary Dividends) should be filled in manually and its totals entered as taxable_interest and dividends in the inputs dictionary.
    • Schedule C (Profit or Loss From Business), if necessary, should be filled in manually and its net profit or loss entered as business_income in the inputs dictionary.
  • Form 2441, Child and Dependent Care Expenses
  • Form 6251, Alternative Minimum Tax
  • Form 8606, Nondeductible IRAs
  • Form 8801, Credit for Prior Year Minimum Tax (this has two parts, one for the previous year and one for the current year)
  • Form 8812, Credits for Qualifying Children and Other Dependents
  • Form 8959, Additional Medicare Tax
  • Form 8960, Net Investment Income Tax
  • Form 8995-A (partial support), Qualified Business Income Deduction
  • California Form 540 and Schedules CA and P

This code does not provide you a lot of help in knowing what inputs you need to provide. In some cases, parts of a form may be left unimplemented. I try to assert() in these cases when possible so you'll know when you hit it. For example, a lot of the logic for "married filing separately" is not fully implemented.

One neat thing you can do with this is compute the real marginal tax rates for a range of incomes. See my wiki page with some examples of that:

A Simple Example

Let's look at 2013/example_single.py.

This computes the tax return for a single person with wages of 100k along with some investment and self-employment income.

#!/usr/bin/env python
#
# Example of a single person with wage income of 100k. Also some investment
# income and business (Schedule C) income.
#

from f1040 import F1040
from ca540 import CA540
from form import FilingStatus

inputs = {
    'status': FilingStatus.SINGLE,
    'exemptions': 1,
    'wages':             100000.00, # W2 box 1
    'withholding':        20000.00, # W2 box 2
    'wages_ss':          100000.00, # W2 box 3
    'ss_withheld':         6200.00, # W2 box 4
    'wages_medicare':    100000.00, # W2 box 5
    'medicare_withheld':   1450.00, # W2 box 6
    'state_withholding':  10000.00, # W2 box 17

    # These are other state tax payments made in 2013 not included in
    # 'state_withholding' that are deductible on schedule A:
    'extra_state_tax_payments': 1000.00,

    'taxable_interest':    1500.00,
    'tax_exempt_interest':  700.00,
    'dividends':           3000.00,
    'qualified_dividends': 2000.00,
    'capital_gain_dist':    500.00,
    'capital_gain_long':   5000.00,
    'business_income':     5000.00,

    # Extra items for schedule A
    'F1040sa' : {
                 '16' : 500, # charitable contributions
                },
}

f = F1040(inputs)
f.printAllForms()
print('')
ca = CA540(inputs, f)
ca.printAllForms()

The structure of this is pretty simple: You list relevant inputs to your tax return in a dictionary. The keys have predefined meanings for various types of income and withholding. You can also enter a row directly in a form, as we do here for Schedule A line 16 for charitable contributions. This method is used for items without a dedicated dictionary key (e.g. IRA distributions, itemized deductions, IRA conversions, dependent care expenses, etc.). You'll need to know the form and row number. However, the software will do the remaining math for you.

The tax return itself is computed by instantiating the F1040 class with the inputs. The results are printed, and then we can do the same thing for the state taxes by instantiating CA540. Note that the state taxes use both the inputs dictionary and Form 1040 as its input.

After running this, we get a copy of the tax return as plain text which can be transcribed into the real tax forms:

$ ./example_single.py 
Form 1040:
    6d           1
     7     100,000
    8a       1,500
    8b         700
    9a       3,000
    9b       2,000
    12       5,000
    13       5,500
    22     115,000
    27         354
    36         354
    37     114,646  AGI
    38     114,646
    40      11,500  Itemized deductions
    41     103,146
    42       3,900  Exemptions
    43      99,246  Taxable income
    44      20,107  Tax
    46      20,107
    55      20,107
    56         707
    61      20,814  Total tax
    62      20,000
    63           0
    72      20,000  Total payments
    76         814  Amount you owe
Schedule SE:
    A2       5,000
    A3       5,000
    A4       4,618
    A5         707
    A6         354
Schedule D:
     8       5,000
    13         500
    15       5,500
    16       5,500
Schedule A:
     5      11,000
     9      11,000
    16         500
    19         500
    29      11,500

CA Form 540:
     7         106
    10           0
    11         106
    12     100,000
    13     114,646  Federal AGI
    14           0
    15     114,646
    16           0
    17     114,646  CA AGI
    18       3,906
    19     110,740  Taxable income
    31       7,861  Tax
    32         106
    33       7,755
    35       7,755
    48       7,755
    64       7,755  Total tax
    71      10,000
    75      10,000  Total payments
    91       2,245  Refund

Clone this wiki locally