Learning center

Before you add a None check, find what could produce the None

A "this might be null" comment is right only if some caller, default or data source can actually produce the null. Check the declared type, then the code that builds the value. If both exclude it, dismiss the comment, or assert the invariant where it is established. If either admits it, add the guard and a test that sends the null through the real path.

Read the type, then the constructor#

A null check is cheap to suggest and looks harmless to accept, so it is easy to add one without asking the narrower question: where would the None come from?

Work outward from the line the comment names:

  1. Read the declared type of the value. In TypeScript with strictNullChecks on, a type without | null or | undefined is checked at compile time. In Python, a type hint is a promise only: "The Python runtime does not enforce function and variable type annotations" (typing).
  2. Find where the value is constructed. A type that says list[int] is backed by whatever builds the object, and in Python that is where to check.
  3. Look at the defaults. A dataclass field, a class attribute or a library config with = None admits None regardless of what callers usually pass.
  4. Look at the data source. JSON from another service, a nullable database column and an optional config key all admit null whatever the type says.

A guard added where the type excluded None#

In May 2026, a vLLM pull request refactored the pooling endpoints and moved their prompt token count for usage reports into one function, get_pooling_usage. gemini-code-assist's review said: "A high-severity issue was identified in the get_pooling_usage function, where a lack of null-checking for prompt_token_ids could lead to a TypeError and 500 errors in specific engine configurations." (review) The inline thread is resolved and its text hidden. A commit titled "Update vllm/entrypoints/pooling/utils.py", with the bot listed as co-author, followed, and the pull request merged on 13 May 2026 with the guard in place:

num_prompt_tokens = sum(
    len(output.prompt_token_ids) if output.prompt_token_ids is not None else 0
    for output in pooling_outputs
)

Diff

The type says otherwise. In vllm/outputs.py at the merge commit, PoolingRequestOutput.__init__ takes prompt_token_ids: list[int]. The engine's output processor, which builds these objects, fills in placeholder IDs when the request used prompt embeddings and asserts before constructing one:

prompt_token_ids = self.prompt_token_ids
if prompt_token_ids is None and self.prompt_embeds is not None:
    prompt_token_ids = [0] * len(self.prompt_embeds)
assert prompt_token_ids is not None

Output processor at the merge commit

The code before the refactor called len(prompt_token_ids) with no check in each endpoint. Within the files we read, None cannot reach get_pooling_usage from the engine. We did not search every place in vLLM that might construct a PoolingRequestOutput, and the bot did not name the "specific engine configurations" it meant.

The guard is not free. If some new path did pass None, the usage report would quietly count zero prompt tokens for that request, where the old code would have raised an error. A guard that contradicts the type hides the case it claims to handle. The answer that fits this evidence is a dismissal that cites the type and the assertion, or, if the reviewer wants the invariant visible, an assertion at the point of use.

A None the type admitted#

An April 2026 vLLM fix shows the opposite case, described in the pull request itself rather than raised by a review bot. Model loading read getattr(model_config.hf_config, "architectures", []), expecting an empty list when a config had no architectures. In Hugging Face Transformers, PretrainedConfig declares architectures: list[str] | None = None, so the attribute always exists, and a config saved without it holds None. getattr uses its default only when the attribute is missing:

c = SimpleNamespace(architectures=None)
print(getattr(c, "architectures", []))  # None
tuple(getattr(c, "architectures", []))  # TypeError: 'NoneType' object is not iterable

The fix changed both call sites to getattr(..., None) or [] and added tests that build a config with architectures=None (pull request, merged 13 April 2026). Here the declared type and the class default both admitted None, and that is what makes a guard correct.

Decide from the source of the value#

What you findAnswer
The type admits null, or a default, nullable column or external payload can produce itChange the code, and add a test that sends the null through the real caller
The type excludes null and the type checker enforces itDismiss, citing the type
The type excludes null, but nothing enforces it at runtime, and the constructor or an assertion establishes itDismiss, citing the constructor, or add an assertion where the value is used
The type excludes null, and you cannot find what builds the valueWrite the test that would produce the null, or fix the type, before you add a guard
A guard would turn a null into a quiet default such as 0 or ""Prefer an assertion or an error, unless the default is the right answer for that input

If the same comment keeps returning on lines where the type already answers it, a custom review rule that tells the reviewer your type checker's settings costs less than a reply on every pull request.

One month of free code review.

New organizations get one calendar month of hosted code review for private and public GitHub repositories. No card or cloud connection required. Eligibility and fair-use terms.

Start free with GitHub

Related reading