Skip to content

Fix NullReferenceException in hover when a TypeVar resolves to a dict specialization - #17

Merged
idanbd merged 1 commit into
masterfrom
fix/dict-typevar-hover-nre
Jul 22, 2026
Merged

idanbd merged 1 commit into
masterfrom
fix/dict-typevar-hover-nre

Conversation

@idanbd

@idanbd idanbd commented Jul 22, 2026 •

Copy link
Copy Markdown

Problem

Sibling of #16. That fix guarded PythonClassType.CreateInstance against the null argument set that hover documentation passes down — but PythonDictionaryType overrides CreateInstance and dereferences args directly, so the base-class guard never runs. Hovering a method of a multi-parameter generic class whose TypeVar return resolves to a dict specialization still throws:

at Microsoft.Python.Analysis.Types.Collections.PythonDictionaryType.CreateInstance(IArgumentSet args)
at PythonFunctionOverload.CreateSpecificReturnFromTypeVar(...)
at PythonFunctionOverload.GetSpecificReturnType / GetReturnDocumentation
at MarkdownDocumentationSource.GetHover -> HoverSource.GetHover -> Server.Hover

In production this surfaces as deterministic JSON-RPC -32000 hover errors and degraded class attribution (a customer environment, ~12 per extraction cycle — see LIM-43637, the tracker for this change since GitHub issues are disabled on this fork).

Fix

One line: mirror the null guard PythonCollectionType.CreateInstance already has (args?.Arguments?.Count). Behavior with a non-null argument set is unchanged; the null path returns the same EmptyDictionary contents the existing non-single-argument branch uses.

Survey of the remaining CreateInstance overrides: PythonCollectionType and PythonClassType are guarded, the Typing* collection types ignore args. Typing/Values/Type.cs, Typing/Values/NamedTuple.cs and Typing/Values/TypeVar.cs also dereference args, but are not reachable as a resolved TypeVar target and have no observed fingerprint — left untouched.

Regression test

HoverTests.GenericMethodReturningTypeVarResolvedToDict — a two-TypeVar generic whose TypeVar return resolves to a dict specialization, next to the #16 test. Verified red before the fix with the exact production stack, green after.

Test infra (needed to run the suite on modern machines)

  • PythonVersions now discovers CPython 3.9–3.12; LatestAvailable3X only considered 3.5–3.8, none of which install cleanly on Apple Silicon. On machines with ≤3.8 only, nothing changes.
  • UnixPythonInstallPathResolver skips broken interpreters on PATH (a stale pyenv shim used to crash discovery for the entire suite with IndexOutOfRangeException).
  • FromOsPath hover assertions use prefix matching — newer interpreters include the scraped docstring after the signature.

Testing

No CI on this fork — run locally on an arm64 Mac under Python 3.12 (same interpreter major.minor as the extractor image), built with -p:RuntimeIdentifier= -p:PlatformTarget=AnyCPU (RuntimeIdentifiers lacks osx-arm64, so a default build silently produces x64-only assemblies that the arm64 runtime cannot load).

  • HoverTests: 16 passed, 2 skipped (need Python 2.7).
  • Full Microsoft.Python.LanguageServer.Tests (excluding IndexManagerTests, which hangs on this machine on an environment issue unrelated to this change): failure set identical to master under the same environment (14 pre-existing Python-3.12 drift failures), plus FromOsPath now passes. No new failures.

Ship plan (like #16 / release 1.9): merge, tag release 1.10 from master (note: master already contains #16; the 1.9 tag was cut on the PR branch), then bump docker/Lim.FeaturesExtractor.Unified.Dockerfile in lim from 1.9 to 1.10.

… specialization

PythonDictionaryType overrides CreateInstance and dereferences the argument
set directly, so the null guard added to PythonClassType in #16 never runs on
this path. Hover documentation passes a null argument set down
(GetReturnDocumentation -> GetSpecificReturnType(self, null) ->
CreateSpecificReturnFromTypeVar -> CreateInstance(null)), which crashed hover
for methods of multi-parameter generic classes whose TypeVar return resolves
to a dict specialization. Mirror the null guard PythonCollectionType already
has.

Test infra changes needed to run the suite on modern setups:
- PythonVersions now discovers CPython 3.9-3.12 (LatestAvailable3X only
  considered 3.5-3.8, none of which install cleanly on Apple Silicon).
- UnixPythonInstallPathResolver skips broken interpreters on PATH (a stale
  pyenv shim crashed discovery for the whole suite).
- FromOsPath hover assertions use prefix matching (newer interpreters include
  the scraped docstring after the signature).

Tracked by LIM-43637 (GitHub issues are disabled on this fork).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@idanbd
idanbd requested review from a team and avig-apiiro July 22, 2026 06:59
@idanbd
idanbd merged commit d9bd4df into master Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants