Use GitHub Copilot Chat in VS Code to explain a function and draft tests
For Actuarial Analysts ·
What This Does
You inherit a Python function from a former colleague, with no comments and a name that tells you little. Copilot Chat in your editor can explain what the code does, in plain language, next to the code itself. It can also draft test cases with invented rows. You then run the tests yourself, because an explanation tells you what Copilot thinks the code does and a test run tells you what it does.
Before You Start
- You use Visual Studio Code with the GitHub Copilot extension installed and signed in to GitHub. GitHub's docs describe Copilot Chat in several IDEs, and this guide follows the Visual Studio Code section. Other editors have their own sections, so check GitHub's page for yours.
- You have access to Copilot. GitHub's page says the features you get depend on your Copilot plan, the client you use and your organization's policies. GitHub also lists a free plan with core features, and individual paid plans start at Pro ($10/month). At work, access usually comes through your company, so ask IT which plan and which features are enabled.
- Your company approves Copilot for the code you open. Do not open a repository or file that holds real policy data, a real claims extract or other company data while Copilot is active, unless the company's AI policy and IT have approved it. For this guide, use a function written for synthetic data, stored in a folder with no real data in it.
- You have Python available so you can run the tests Copilot drafts. The same steps work for R, SQL or VBA, with your own test tool.
Steps
1. Open a file that holds only synthetic code
Save the function below as development.py in a new, empty folder. It contains no data.
def age_to_age(cumulative):
"""Return ratios of each cumulative value to the one before it."""
ratios = []
for i in range(1, len(cumulative)):
prior = cumulative[i - 1]
if prior == 0:
raise ValueError("prior value is zero at position %d" % (i - 1))
ratios.append(cumulative[i] / prior)
return ratios
2. Open Copilot Chat in ask mode
GitHub's page says to click the chat icon in the title bar of Visual Studio Code. If the icon is missing, right-click the title bar and make sure Command Center is selected. At the bottom of the chat view, open the agents dropdown and select Ask. The page says ask mode is for questions and explanations that do not change your project, which is what you want here. If you do not see agent options, your administrator may have turned some off. That is a company setting.
3. Select the function and ask for an explanation
Select the function in the editor. Then type your prompt in the chat box. You can also start an inline chat from the editor with Ctrl+I (Command+I on Mac), per the page.
Explain what this function does in plain language, step by step.
Then list the edge cases it handles and the ones it does not.
Read the explanation against the code, line by line. Look for steps the explanation skips and claims the code does not support.
4. Ask for test cases with invented rows
Write pytest tests for this function using small invented lists of cumulative values.
Include a list with one value, an empty list, and a list with a zero in it.
The page notes you can also type / in the chat box to see the slash commands available, and the dropdown at the top of a reply shows which files Copilot used as references.
5. Run the tests and check the expected values yourself
Save the tests and run pytest in your terminal. Read each failure. Before you run them, work out each expected value by hand. Copilot may write a test that passes because it copies the function's own logic. For a list of [1000, 1500, 1650], the ratios are 1500 / 1000 = 1.5 and 1650 / 1500 = 1.1, so the expected result is [1.5, 1.1]. A single-value or empty list has no ratios, so the expected result is []. A list starting with zero, such as [0, 100], should raise the ValueError. If a test expects something else, the test is wrong or the code is, and you find out which.
Real Example
Scenario: A teammate asks what age_to_age does, and you want to be sure before you reuse it in a development exhibit.
What you type: The explanation prompt from step 3, then the test prompt from step 4.
What you get: An explanation that the function walks the list, divides each value by the prior one, and raises an error if a prior value is zero. It does not say what happens when a value is negative or missing, and you note that as an open point. The tests Copilot drafts include [1000, 1500, 1650] with the result [1.5, 1.1]. You confirm the values by hand and run pytest. All three cases pass. You send the function and tests to your reviewer with a note that the tests use invented lists only.
Tips
- Explanations help you read code and never replace running it. Run the code on synthetic data first, then on real data in your approved environment.
- Do not paste a real extract into the chat to "see what happens". Column names and invented rows give the same help.
- Treat the function's output as your own responsibility. The reviewing actuary reads your tested code and its results, not the chat.
Tool interfaces change. If a button has moved, look for similar Copilot or chat options in the same menu area.