Skip to content

Add TypeVar variance, bounds, and constraints to type information#11

Merged
knutwannheden merged 2 commits intoopenrewrite:mainfrom
knutwannheden:claude/type-variable-bounds-variance-sbNQz
Feb 24, 2026
Merged

Add TypeVar variance, bounds, and constraints to type information#11
knutwannheden merged 2 commits intoopenrewrite:mainfrom
knutwannheden:claude/type-variable-bounds-variance-sbNQz

Conversation

@knutwannheden
Copy link
Contributor

@knutwannheden knutwannheden commented Feb 24, 2026

Summary

This PR extends the type information system to include detailed metadata about TypeVars, including their variance, upper bounds, and constraints. This allows clients to better understand the properties and restrictions of type variables in Python code.

Key Changes

  • TypeVar Variance Support: Added variance field to TypeVar descriptors, supporting covariant, contravariant, and invariant variance modes
  • Upper Bound Tracking: TypeVars with upper bounds now expose the bound type through an upperBound field
  • Constraints Support: TypeVars with constraints now expose the list of constraint types through a constraints field
  • Protocol Updates: Extended TypeDescriptor::TypeVar variant to include optional variance, upper_bound, and constraints fields
  • Registry Implementation: Updated type registry to extract and register TypeVar metadata from the semantic database

Implementation Details

  • Variance is always included in the response (never skipped during serialization)
  • Upper bounds and constraints are optional and only included when present
  • The constraints field is an empty vector when not applicable (skipped during serialization)
  • All new fields are properly integrated with the existing type registration system

Testing

Added comprehensive integration tests covering:

  • TypeVar with explicit covariance
  • PEP 695 generic syntax with inferred variance
  • TypeVar with upper bounds
  • TypeVar with constraints
  • Unbounded/unconstrained TypeVars (ensuring fields are absent)

TypeVar descriptors now include:
- variance: "covariant" | "contravariant" | "invariant" | "bivariant"
- upperBound: TypeId reference when bound= is specified
- constraints: array of TypeId references when constraints are specified

These are extracted from ty's BoundTypeVarInstance/TypeVarInstance APIs.
All fields use skip_serializing_if so they're omitted when absent.

https://claude.ai/code/session_01Ui3HhLK3b6S12WUNx5XfPp
Python has no bivariant type parameters — Bivariant is an internal
ty state during variance inference, not a valid output value.
@knutwannheden knutwannheden merged commit ff9cd03 into openrewrite:main Feb 24, 2026
7 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Feb 24, 2026
@knutwannheden knutwannheden deleted the claude/type-variable-bounds-variance-sbNQz branch February 24, 2026 09:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants