Skip to content

Enhancement: Infer TypeVar as its bound/constraint #34

@DanCardin

Description

@DanCardin

Summary

Given this line https://github.com/litestar-org/type-lens/blob/main/type_lens/type_view.py#L227 I think that could/should be rephrased as if isinstance(self.annotation, typ): return TypeView(Union[bytes, str]).is_subtype_of(typ). And that seems interesting.

Because it does feel like T = TypeVar(int, float); TypeVar(T).is_subtype_of(int) should return True, or that T = TypeVar(bound=Union[int, float]); TypeVar(T).is_union should return True.

Following from that, it feels like either TypeView should either transparently be doing something equivalent to the following:

        if isinstance(annotation, TypeVar):
            bound = self.annotation.__bound__
            if bound:
                return TypeView(bound)

            constraints = self.annotation.__constraints__
            return TypeView(Union[*constraints])

or it should have a method like resolve_type_var which does this recursively (because e.g. list[T]) or something...

I suppose the contravariant/covariant kwargs for constraints are a meaningful consideration potentially...but i dunno...

Basic Example

No response

Drawbacks and Impact

No response

Unresolved questions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions