Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Infer TypeVar as its bound/constraint #34

Open
DanCardin opened this issue Sep 25, 2024 · 1 comment
Open

Enhancement: Infer TypeVar as its bound/constraint #34

DanCardin opened this issue Sep 25, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@DanCardin
Copy link
Collaborator

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

@DanCardin DanCardin added the enhancement New feature or request label Sep 25, 2024
@DanCardin
Copy link
Collaborator Author

I think perhaps a .concretize() method makes the most sense as an API/method.

And then the clause in is_subtype_of could be

if self.is_type_var:
    return self.concretize().is_subtype_of(typ)

(which would still return False if it was just TypeVar('T') with no constraints/bounds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant