Skip to content

Fix NullReferenceException in hover for generic methods returning a TypeVar#16

Open
idanbd wants to merge 1 commit into
masterfrom
fix/generic-typevar-return-hover-nre
Open

Fix NullReferenceException in hover for generic methods returning a TypeVar#16
idanbd wants to merge 1 commit into
masterfrom
fix/generic-typevar-return-hover-nre

Conversation

@idanbd

@idanbd idanbd commented Jul 7, 2026

Copy link
Copy Markdown

Problem

Hovering a call to a generic method whose return type is a bare TypeVar (e.g. def get(self) -> T on a Generic[T] class), where the type parameter resolves to a collection type, throws a NullReferenceException while the server builds the return-type documentation:

at ArgumentSetExtensions.Values[T](IArgumentSet args)
at PythonClassType.CreateInstance(IArgumentSet args)
at PythonFunctionOverload.CreateSpecificReturnFromTypeVar(...)
at PythonFunctionOverload.GetSpecificReturnType(...)
at PythonFunctionOverload.GetReturnDocumentation(IPythonType self)
at HoverSource.GetHover(...)

Cause

GetReturnDocumentation calls GetSpecificReturnType(self, args: null) — there are no call arguments when producing documentation. For a TypeVar return on a generic instance this reaches CreateSpecificReturnFromTypeVar, which resolves the type parameter and calls specificType.CreateInstance(null). For the list/dict/tuple specializations, CreateInstance dereferences the null argument set (ArgumentSetExtensions.Values<T> / CreateList) and throws.

Fix

Guard PythonClassType.CreateInstance against a null argument set, returning a plain instance of the type. The normal call-resolution path (non-null args) is unchanged, and non-collection type parameters were already unaffected (they fall through to the same new PythonInstance(this)).

Test

Adds HoverTests.GenericMethodReturningTypeVar: hovering a Generic[T] method returning T resolved to a collection type no longer throws and returns the method's signature. (Complements the existing HoverGenericClass, which covers a non-collection type parameter.)

…ypeVar

GetReturnDocumentation calls GetSpecificReturnType with a null argument set;
for a TypeVar return this reaches CreateSpecificReturnFromTypeVar and calls
CreateInstance(null). For the list/dict/tuple specializations CreateInstance
dereferences the null argument set and throws. Guard CreateInstance against a
null argument set by returning a plain instance; the call-resolution path
(non-null args) is unchanged.
@idanbd idanbd requested review from a team and avig-apiiro July 7, 2026 10:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant