Your test passed. Then the program crashed anyway, with the error you've now searched for in six different phrasings: 'AttributeError: 'NoneType' object has no attribute'.
That's the part that stings. The test was green. It said everything was fine, and it was telling the truth, which is the uncomfortable bit.
Here is the one idea: a test checks the inputs you thought of, but a type checker reads your code and asks what could possibly come out of every call, including the paths you never ran. By the end you'll know what None is, what a type hint does, and how to run a checker that finds this crash without executing anything.
What you need
Python (I used 3.11.17) and a terminal, the text window where you type commands, opened in an empty folder. For the checker step you'll also need uv, a tool from Astral that installs and runs Python packages.
The concept: a test is a statement about the inputs you gave it
Here's a small function of the kind that causes this. Save it as lookup.py. It pulls the version number out of a line of text, using 're.match', which tries to match a text pattern at the start of the line, and then a second function takes the first part of it. The 'assert' line is my one test: it crashes the program if the claim is false.
import re
def find_version(line: str) -> str | None:
m = re.match(r"v(\d+\.\d+)", line)
if m:
return m.group(1)
return None
def major(line: str) -> str:
version = find_version(line)
return version.split(".")[0]
# the one test I wrote
assert major("v3.11") == "3"
print("test passed")
I ran this on Python 3.11.17 with 'python lookup.py'. It prints 'test passed'. Now feed it something the test never did. This second snippet imports 'major' from lookup.py and calls it:
from lookup import major major("hello")
AttributeError: 'NoneType' object has no attribute 'split'
Nothing is wrong with the test. It proved that "v3.11" works, and it did. It said nothing about "hello", because "hello" was never handed to it. A passing test is a statement about the inputs you fed it and not a drop more.
What None is
None is Python's way of saying "there is no value here." It's what a function hands back when it has nothing to give: a 'dict.get()' for a key that isn't there, a 're.match()' that didn't match, a function that reaches its end without a 'return'. It is its own type, and I checked: 'type(None)' prints '<class 'NoneType'>'.
The trouble is that None looks fine until you treat it like text. 'None.split(".")' is asking nothing to be split, and that's the crash above.
In my function, 're.match' returns None whenever the line doesn't start with a 'v' and a number. So 'find_version' has two outcomes, a string or None, and 'major' quietly assumed only the first one existed.
A type hint and a type checker
Look at the first function again. The '-> str | None' on 'find_version' is a type hint: a label saying "this returns a string or None." Python reads it and does nothing. It does not enforce it, and it did not stop my crash. I'd annotated the function honestly and it still blew up at runtime.
What a hint does is give a different kind of program something to read. A type checker is a tool that reads your code without running it, follows the hints, and walks every branch, including the ones no test visited. Where a test asks "what happens with this input?", a checker asks "what could possibly come out of this call?"
That's the whole idea, and it transfers to anything you write. A test samples the inputs you thought of. A checker enumerates the outcomes you didn't.
Walkthrough: find the crash without running a line
I ran ty, a new checker from Astral, the people behind uv. I ran version 0.0.99, which is beta software; it isn't a 1.0 release, so treat the output as a good look, not gospel. I ran it with uvx, which comes with uv and fetches the tool for one command. Run this in the folder holding lookup.py:
uvx ty check lookup.py
error[unresolved-attribute]: Attribute 'split' is not defined on 'None' in union 'str | None' --> lookup.py:11:12 | 11 | return version.split(".")[0] | ^^^^^^^^^^^^^ Found 1 diagnostic
Two notes on that output. The real tool wraps the names in backticks, which this site can't print, so I swapped in quotes; and my machine added an unrelated deprecation warning (a notice that a feature is going away) about an environment variable, which I trimmed. Everything else is as it came out.
Read the middle line: 'str | None' — a union, which just means a value that could be either type, a string or None. The checker followed 'find_version', saw the two outcomes you labelled, and noticed that line 11 uses the result without handling the None half. Your program never ran. Your test never ran. It found the crash by reading.
Show the code
<style>
.row{display:flex;flex-wrap:wrap;gap:8px;margin:8px 0;align-items:center}
.row button{min-height:40px;font:inherit;font-size:15px;padding:6px 12px;border:1px solid #999;border-radius:8px;background:#f4f4f6;cursor:pointer}
.row button[aria-pressed="true"]{background:#1d1d1f;color:#fff;border-color:#1d1d1f}
#line{min-height:40px;font:15px ui-monospace,Menlo,Consolas,monospace;padding:6px 10px;border:1px solid #999;border-radius:8px;width:9em}
#src{background:#f6f6f8;border:1px solid #ddd;color:#1d1d1f;min-height:0}
.flow{display:grid;grid-template-columns:1fr 1fr;gap:8px;margin:10px 0;font-size:14px}
.flow .top{grid-column:1 / 3;text-align:center}
.node{border:2px solid #ccc;border-radius:8px;padding:8px;text-align:center;word-break:break-word}
.node code{font:13px ui-monospace,Menlo,Consolas,monospace}
.taken{border-color:#1565c0;background:#e3f2fd}
.read{border-color:#2e7d32;background:#e8f5e9}
.bad{border-color:#c62828;background:#fdecea}
pre{background:#1e1e22;color:#e6e6e6;border-radius:8px;padding:10px;font:13px/1.5 ui-monospace,Menlo,Consolas,monospace;white-space:pre-wrap;word-break:break-word;margin:8px 0;min-height:2.5em}
#say{font-size:15px;font-weight:600}
</style>
<p style="font-size:15px;margin:0">Pick a version of major(), run it on a line of text (the test used v3.11), then ask the checker to read the code.</p>
<div class="row" role="group" aria-label="Version of major">
<button id="v-before" aria-pressed="true">Before: no guard</button>
<button id="v-after" aria-pressed="false">After: if version is None</button>
</div>
<pre id="src"></pre>
<div class="row">
<label for="line" style="font-size:15px">major(</label><input id="line" value="v3.11" aria-label="line passed to major"><span style="font-size:15px">)</span>
<button id="run">Run it</button>
<button id="check">uvx ty check lookup.py</button>
</div>
<div class="flow">
<div class="node top" id="b-top"><code>find_version(line) -> str | None</code></div>
<div class="node" id="b-str">matched: a str<br><code>version.split(".")</code> works</div>
<div class="node" id="b-none"></div>
</div>
<pre id="out" aria-live="polite">$</pre>
<p id="say"></p>
<script>
function q(id) { return document.getElementById(id); }
var guarded = false;
var SRC = {
before: 'def major(line: str) -> str:\n version = find_version(line)\n return version.split(".")[0]',
after: 'def major(line: str) -> str:\n version = find_version(line)\n if version is None:\n return "unknown"\n return version.split(".")[0]'
};
function clear() { ['b-top', 'b-str', 'b-none'].forEach(function (id) { q(id).className = 'node' + (id === 'b-top' ? ' top' : ''); }); }
function reset() { clear(); q('out').textContent = '$'; q('say').textContent = ''; }
function pick(g) {
guarded = g;
q('v-before').setAttribute('aria-pressed', String(!g)); q('v-after').setAttribute('aria-pressed', String(g));
q('src').textContent = g ? SRC.after : SRC.before;
q('b-none').innerHTML = g ? 'no match: None<br><code>if version is None: return "unknown"</code>' : 'no match: None<br><code>None.split(".")</code> crashes';
reset();
}
q('v-before').onclick = function () { pick(false); };
q('v-after').onclick = function () { pick(true); };
q('line').addEventListener('input', reset);
q('run').onclick = function () {
clear();
var line = q('line').value, m = /^v(\d+\.\d+)/.exec(line);
q('b-top').className = 'node top taken';
if (m) {
q('b-str').className = 'node taken';
q('out').textContent = '>>> major(' + JSON.stringify(line) + ')\n' + "'" + m[1].split('.')[0] + "'";
q('say').textContent = 'This run walked the str path only (blue). The None path was never visited, so it told you nothing about it.';
} else if (guarded) {
q('b-none').className = 'node taken';
q('out').textContent = '>>> major(' + JSON.stringify(line) + ")\n'unknown'";
q('say').textContent = 're.match found no match and find_version returned None, but the guard returned early instead of splitting it.';
} else {
q('b-none').className = 'node taken bad';
q('out').textContent = '>>> major(' + JSON.stringify(line) + ')\nAttributeError: \'NoneType\' object has no attribute \'split\'';
q('say').textContent = 're.match found no match, find_version returned None, and line 11 tried to split it.';
}
};
q('check').onclick = function () {
clear();
q('b-top').className = 'node top read';
q('b-str').className = 'node read';
if (guarded) {
q('b-none').className = 'node read';
q('out').textContent = 'All checks passed';
q('say').textContent = 'Nothing ran. After the early return, the checker knows what is left must be a string: that is narrowing.';
} else {
q('b-none').className = 'node bad';
q('out').textContent = 'error[unresolved-attribute]: Attribute \'split\' is not defined on \'None\' in union \'str | None\'\n --> lookup.py:11:12\n |\n11 | return version.split(".")[0]\n | ^^^^^^^^^^^^^\n\nFound 1 diagnostic';
q('say').textContent = 'Nothing ran. The checker read the hint str | None and walked both outcomes, finding the None one unhandled.';
}
};
pick(false);
</script>Other checkers say the same thing
ty is new, and it isn't the only road. mypy is the long-established checker and the one most tutorials name. Pyright, from Microsoft, is another established one. I ran mypy 2.3.1 and Pyright 1.1.414 on the same file, and both flagged the same line:
lookup.py:11: error: Item "None" of "str | None" has no attribute "split" [union-attr]
That mypy line is worth recognizing, because it's the shape of message you'll meet whenever a checker catches this class of bug. Pick whichever your editor already speaks.
Where it goes wrong: half-annotated code
Say you've only partly added hints, which is how most of us start. You labelled 'find_version' because it was the confusing one, and you left 'major' bare:
def major(line):
version = find_version(line)
return version.split(".")[0]
Same bug, same line. Here's what each checker said, on the same machine:
mypy: Success: no issues found in 1 source file ty: error[unresolved-attribute] ... 'str | None' pyright: error: "split" is not a known attribute of "None"
mypy said everything was fine. It only checks the bodies of functions that have annotations; 'major' had none, so mypy skipped the whole function, including the call to the function you took the trouble to label. Your hints sit there looking like a safety net with a hole exactly where you haven't written one yet.
It's a default rather than a bug, and mypy has a switch for it. Running 'mypy --check-untyped-defs lookup2.py' on that same file (the second version of lookup.py, saved under that name) gave the 'union-attr' error back. I only found that switch because the green surprised me, and a clean "Success" is the kind of green that feels like a test passing: true, and about less than you hope.
I should be fair to ty here too. When I stripped the hints from both functions, ty on 0.0.99 reported 'All checks passed' while Pyright still flagged the line. So no checker sees everything. Hints are what you're buying, and the checkers differ in how much they work out for themselves.
The fix is a guard, and the checker agrees
This version of 'major' returns early when there's no version:
def major(line: str) -> str:
version = find_version(line)
if version is None:
return "unknown"
return version.split(".")[0]
After the 'if version is None' check returns early, the checker knows what's left must be a string, and the complaint goes away. I added a second assert for "hello", ran the file ('ok'), and ran both ty and mypy: clean. That check-then-continue pattern is called narrowing, and you'll write it a thousand times.
Recap
- None means "no value"; it crashes the moment you treat it like text.
- A passing test only covers the inputs you fed it.
- A type hint does nothing at runtime, but a checker reads it and walks every branch.
- Checkers differ, and mypy skips functions with no annotations unless you ask it not to.
- An 'if version is None' guard is called narrowing, and the checker understands it.
Next: take the function that crashed on you last. Add hints to everything it touches, and make sure the one that returns None says so with '| None'. Then run one command — 'uvx ty check yourfile.py' (uvx comes with uv, so install uv first if you haven't got it). If it flags a line, you've just seen the thing a test can't show you: the input nobody thought to ask about. Fix it the way you did above, with the 'if version is None' guard — the narrowing that tells the checker what's left — and run it again to watch the complaint disappear.