Sub topic 5
Sharing a selection
This chapter assumes you have used an AI chat or coding assistant, can run a Python file from a terminal, and know what a function and a traceback are. It builds on the previous chapter on sharing whole files. Here the question is narrower: when is a few lines of a file better than the whole file, and how do you share those lines so the assistant can use them?
What a selection is
A selection is a contiguous range of text you highlight in your editor, or copy out of a file, and hand to the assistant as the focus of the request. It can be one line, one function, or a block of a few dozen lines.
A selection does two jobs:
- It points. It tells the assistant "the part I care about is here."
- It filters. Everything outside the selection is either omitted or demoted to background.
Most assistants and editor integrations treat a selection as a signal of attention, not as a hard boundary. If the whole file is also attached, the assistant can still read it. If only the selection is sent, the assistant sees nothing else. That difference drives most of the decisions in this chapter.
Why use a selection instead of a file
Consider the small file used throughout this tutorial.
def average(values):
return sum(values) / len(values)
print(average([]))Running it produces this traceback.
Traceback (most recent call last):
File "stats.py", line 4, in <module>
print(average([]))
File "stats.py", line 2, in average
return sum(values) / len(values)
ZeroDivisionError: division by zeroThe file is only four lines, so sharing all of it is cheap. In a real project, stats.py might hold 800 lines, and the bug lives in two of them. A selection has these advantages over the whole file:
- Budget. Fewer tokens are spent on code that cannot affect the answer. The context window is finite, as covered earlier in this tutorial.
- Focus. The assistant is less likely to "improve" unrelated functions or answer about a different part of the file.
- Precision of edits. A request scoped to lines 1-2 produces a change to lines 1-2. Whole-file requests tend to produce wider rewrites.
- Privacy. A selection exposes less. If a file contains a section you should not share, a selection keeps it out (see the chapter on sensitive data).
A selection also has costs:
- It hides the surroundings. The assistant cannot see imports, helper functions, class definitions, or callers unless you include them.
- It can be silently wrong. If you select the wrong lines, the assistant will confidently work on the wrong thing.
Choosing between a file, a selection, and a description
Use this table as a starting point.
| Situation | Best choice | Reason |
|---|---|---|
| Bug is inside one function with no outside dependencies | Selection | The function is the whole problem |
| Bug depends on how several functions interact | File | The interaction is the problem |
| You want a new function that follows existing patterns | Selection of one similar function, plus the task | A good example beats a long file |
| You want a rename or change across a file | File | The assistant needs every occurrence |
| The code is confidential, but the question is general | Description | Nothing sensitive leaves your machine |
| You want a design opinion, not a code change | Description, with a short selection if needed | Prose carries intent better than code |
A useful test: ask yourself, "If a colleague read only this selection, could they answer my question?" If yes, the selection is enough. If they would immediately ask "what does load_config do?" then the selection is too narrow.
Choosing the boundaries
Where you start and stop the selection matters. Three guidelines cover most cases.
- Select whole syntactic units. Start at
defand end at the last line of the body. A selection that begins in the middle of anifblock, or ends before areturn, invites guesses about the missing part. - Include the signature. The function name, parameters, and any type hints tell the assistant how the code is called. Leaving them out is a common mistake.
- Add the minimum surroundings. If the function uses a constant defined at the top of the file, include that line too, or mention it in the request.
Here is a slightly larger example where the boundary matters.
DEFAULT_PRECISION = 2
def average(values):
return sum(values) / len(values)
def format_average(values):
return f"{average(values):.{DEFAULT_PRECISION}f}"If the bug report is "formataverage crashes on an empty list", selecting only `formataverage is not enough to locate the crash. The assistant would have to guess what average` does. Selecting both functions is the right boundary, and the constant can be left out because it does not affect the failure.
Telling the assistant where the selection came from
A selection copied into a chat loses its location. The assistant does not know which file it came from, what lines it occupied, or what surrounds it. Add that information in words. A good request states:
- the file name,
- the line range,
- what the selection is (for example "the
averagefunction"), - how it relates to the rest of the file, when that matters.
Compare two ways of phrasing the same request. The first leaves out the origin.
Fix this so it doesn't crash:
def average(values):
return sum(values) / len(values)The second names the file, the lines, and the failure, and then states the task and its constraints.
Context: stats.py (attached), selection: the average function (lines 1-2).
Error: ZeroDivisionError: division by zero when average([]) is called.
Task: Change average so an empty list returns None instead of raising. Keep the function signature and do not change other code.The second version is longer, but it removes three guesses: where the code lives, what the failure is, and what "fix" should mean. The assistant could otherwise choose to return 0, raise a ValueError, or add a default parameter.
How selection-aware tools behave
Different tools handle selections in different ways. Check which of these applies to the one you use, because it changes what you must write yourself.
- Selection only. Only the highlighted text is sent. You must mention any outside facts yourself.
- Selection plus file. The highlighted text is marked as the focus, and the file is attached as background. This is the most forgiving mode.
- Selection plus line numbers. The tool records the range. Line numbers in your request then refer to real positions.
- Pasted text. You copy the text into the chat. No location information is kept, so you must add it.
If you are not sure what your tool sends, test it. Select one line, ask "What file is this from, and what comes before it?", and read the reply. If the assistant cannot say, only the selection was sent.
Common errors
These problems appear repeatedly when people share selections.
- Too narrow. The selection omits a helper, import, or type that the logic depends on. The assistant invents a plausible version of it. Fix: add the missing definition or describe it.
- Too wide. The selection contains several functions and the request does not say which one matters. The assistant spreads its attention. Fix: select less, or name the target function explicitly.
- Wrong selection. The highlighted text is not the code that failed. Check the line numbers in the traceback against the selection. In the traceback above, the failing line is line 2 in
average, so the selection must include line 2. - Selection without the error. The assistant sees code that looks fine and reports no problem. Add the error text so it knows what is failing.
- Selection without the task. The assistant explains the code when you wanted a change. State the action: fix, rewrite, explain, test, or review.
- Mixed instructions. Text inside the selection, such as a comment saying "TODO: remove this function," can be confused with your request. Keep instructions in your own message, and say which text is code.
A worked iteration
Suppose you send only the selection and the message "make this safe."
def average(values):
return sum(values) / len(values)The assistant might reply by adding type hints and a docstring. That is reasonable, but it is not what you wanted, because "safe" was ambiguous and no error was attached. Your next message adds the missing pieces:
That is not what I meant. The failure is ZeroDivisionError when values is empty.
Return None for an empty list. Do not add a docstring or type hints.A correct answer looks like this.
def average(values):
if not values:
return None
return sum(values) / len(values)
print(average([]))Running it prints None. Note that the call site print(average([])) now prints None, which is a behavior change the caller may or may not expect. A selection of only the function would never have shown you that. This is the main limit of selections: the assistant can fix the code it sees, but it cannot warn you about callers it does not see.
A checklist before you send
- Does the selection start and end at natural boundaries?
- Does it include the signature or heading of the code?
- Did you name the file and the line range or function?
- Did you say what is failing, and attach the error if there is one?
- Did you state the task and any constraints?
- Does the selection contain anything that should not leave your machine?
- Did you check that the selection matches the lines named in the traceback?
Exercises
Exercise
A traceback points to line 2 of average in stats.py. You select only line 1, def average(values):, and ask the assistant to fix the crash. What is wrong, and what would you change?
Show answer
The selection does not contain the failing line. The crash is on line 2, return sum(values) / len(values), so the assistant has no code that can raise the error. Select lines 1-2 (the whole function), name the file and range, attach the ZeroDivisionError text, and state the task, for example "return None for an empty list".
Exercise
You want to add a median function that matches the style of an existing average function in a 600-line file. Do you share the file, a selection, or a description? Give one reason.
Show answer
Share a selection of average (including its signature) and describe the new function in the task. The selection works as a style example, and it uses far less of the context budget than 600 lines. If median needs helpers or imports from elsewhere in the file, mention them or add those lines.
Exercise
After you send a selection, the assistant changes average to return None. Why might this break other code, and what should you have included to find out?
Show answer
Callers that use the result in arithmetic, such as average(x) + 1, will now raise a TypeError when the list is empty. The selection showed only the function, so the assistant could not see callers. Include the call sites, share the file, or ask the assistant to list what could break when the return type changes.