Skip to content

feature: Support for Pre-PEP 695 generics #405

Description

@jonathan-laurent

Context and Problem

I saw there was a debate on a recently merged PR about whether or not to support pre-PEP 695 generics (i.e., which use Generic and TypeVar).

My understanding is that these are not currently supported. One particular consequence are spurious warnings. For example, for the following class definition:

T = TypeVar("T", covariant=True)
P = TypeVar("P", contravariant=True)

@dataclass(frozen=True)
class OpaqueSpace(Generic[P, T], dp.Space[T]):
    """
    Type Parameters:
        P: Type parameter for the ambient inner policy type.
        T: Type parameter for the element type.
    """

I get the spurious warning:

WARNING -  griffe: src/delphyne/stdlib/opaque.py:29: Type parameter 'P' does not appear in the class signature

The Case for Supporting Pre-PEP 695 Generics

One argument for supporting pre-PEP 695 generics is that the features they offer are not subsumed by PEP 695. In particular, PEP 695 does not allow specifying the variance of type variables. Type checkers can infer this information most of the time, but there are also cases where the use of TypeVar and Generic cannot be avoided (this is the case in one of my projects).

If (partial) support for Pre-PEP 695 Generics can be added without excessive effort, it would therefore be a worthwhile addition.

Activity

  1. pawamoy commented on Aug 28, 2025

    @pawamoy
    Member

    Thanks for the feature request @jonathan-laurent 🙂

    Now that Griffe models support type parameters, it shouldn't be too hard to parse Generic in class bases to extract the type parameters, associating them with declared attributes of the same name 👍

  2. dangotbanned commented on Jun 13, 2026

    @dangotbanned

    I'd be interested in this feature too.

    I just discovered this Type Parameters section, which seemed like a big improvement to my informal version in some protocols:

    Show diff

    diff --git a/src/narwhals/_plan/compliant/plugins.py b/src/narwhals/_plan/compliant/plugins.py
    index 93907fa1f..5b330bba1 100644
    --- a/src/narwhals/_plan/compliant/plugins.py
    +++ b/src/narwhals/_plan/compliant/plugins.py
    @@ -50,7 +50,11 @@ __all__ = ("Builtin", "Implementation", "Plugin")
     class Plugin(HasClasses[C], Protocol[C, DF, LF, S]):
         """An entrypoint for a backend that implements compliant-level operations.
     
    -    `[C, DF, LF, S]`.
    +    Type Parameters:
    +        C: _description_
    +        DF: _description_
    +        LF: _description_
    +        S: _description_
         """

    But that's triggering the same path:

    Show logs

    griffe: src\narwhals\_plan\compliant\plugins.py:54: Type parameter 'C' does not appear in the class signature
    griffe: src\narwhals\_plan\compliant\plugins.py:55: Type parameter 'DF' does not appear in the class signature
    griffe: src\narwhals\_plan\compliant\plugins.py:56: Type parameter 'LF' does not appear in the class signature
    griffe: src\narwhals\_plan\compliant\plugins.py:57: Type parameter 'S' does not appear in the class signature
    

    Anyways - just here to say you might need to handle Protocol as a special-case too 🙂:

    from typing import Generic, Protocol, TypeVar, is_protocol
    
    T = TypeVar("T")
    
    
    class This(Protocol[T]):
        member: T
    
    
    class AndThis(Generic[T], Protocol):  # noqa: PYI059
        member: T
    
    
    class ButAlso(Protocol, Generic[T]):
        member: T
    
    
    is_protocol(This), is_protocol(AndThis), is_protocol(ButAlso)
    (True, True, True)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

featureNew feature or request

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions