#Type Lens
1 messages · Page 1 of 1 (latest)
huh, but that thing doesn't exist
april 2!? I was looking for this days ago!
do y'all have existing usage of type-lens generally? I looked up ParameterView in the org and didn't see anything
In particular right now, I can't tell if this being so strict https://github.com/litestar-org/type-lens/blob/main/type_lens/parameter_view.py#L53 is on purpose because of something I haven't thought of yet, or if it could just work properly in absence of a type.
No, here's the history.
In Litestar, we had a thing called ParsedType which was basically what TypeView is now, notwithstanding recent changes.
At some point, ParsedType was changed into into a thing called FieldDefinition and a bunch of other things were added to it, like implicit parsing of openapi metadata, a coupling to litestar's KwargDefinition and DependencyKwarg types, and a name field.. so where ParsedType was intended to be an interface over type introspection, it got overloaded with this other stuff.
type-lens's TypeView, was seeded with the implementation of ParsedType from the commit immediately before all of the above was added to it.
ParameterView was Litestar's representation of a signature parameter, which was removed from Litestar at the same time as these other changes were made to ParsedType.
The intention with type-lens is to first build an object that properly represents a type and allows useful introspection of that type, and then some abstractions over that to represent places where a type is used, such as a signature parameter or a model field.
Unwinding the litestar FieldDefinition implementation will take some time, but it needs to be done. Particularly the openapi metadata parsing stuff really doesn't belong there, but unfortunately it has been exposed as public api so ripping it out isn't an option.
Any more questions on this front, I'll be happy to answer as best as I can.
What would be passed to TypeView if the parameter had no annotation?
I haven't gotten that far, maybe the TypeView doesnt get constructed and it's a None or Empty instead.
In attempting to actually use ParameterView on an unknown function, it seems like you'd have to (roughly)
hints = get_type_hints()
signature = inspect.signature(fn)
params = [ParameterView.from_parameter(p, hints) for p in signature.parameters]
but then you hand it
def foo(bar):
return bar
and it immediately fails because of that check. (which you could try/exception, but it's except all the time, and in my mind, you'd ultimately still want a ParameterView instance handy for the common interface to decide what to do with the parameter
I'm trying to write something like a FunctionView that would actually perform the get_type_hints call and handle the various issues with raw get_type_hints().
this sounds like a logical next step!
Is it possible to add a "type lens ELI5" example in the docs? I will be honest, the readme does not tell me what I can do "at runtime"
yeah, we'll get there 🙂
the way we show a hello world route in litestar
maybe meta typing users can understand, but I dont 🙃
this type of library would mostly be used by other library authors, I'd expect. No so much targeting end-user applications
yes I assumed as much, at least having a hello world would tell me "this is not the library you are looking for"
def foo(bar):
return bar
I'd be pretty happy if we just treated this as
def foo(bar: Any) -> Any: ...
we could get there by looking at the difference b/w inspect.Signature and get_type_hints and assign Any to param/return type that exists in former, and not latter
I know I just renamed it to is_subtype_of but now I really want to call it is_consistent_with 🙈
A ModelView would also be a good abstraction, I think - it would almost be the same a a FunctionView except no return type
i had just assumed calling it on a type was using __init___ and the return type would be the thing itself
https://github.com/litestar-org/type-lens/pull/23 is what I have so far, although in retrospect it obviously makes more sense that lack of a hint should imply Any (which I haven't implemented yet)
Description
- Abstract over the calling of
get_type_hintsin order to have practical use forParameterStore
Based even on code inside type-view, i think it perhaps makes sense to for there to be a TypeView attribute that does this:
TypeView(int).<this> == int
TypeView(list[int]).<this> == list
TypeView(SomeArbitraryGeneric).<this> == SomeArbitraryGeneric
TypeView(SomeArbitraryGeneric[Foo]).<this> == SomeArbitraryGeneric
I.e. self.origin or self.annotation essentially.
seems like get_instantiable_origin and get_safe_generic_origin could disappear because they'd be so simple they could be inlined.
I also have at least a couple of places where I have this same pattern used to get at the bare annotated type rather than a specific generic variant
tl;dr interest in convenience property for this? and what would you call it, if so?
interested, sure
sorry I have been super busy for the last couple of weeks (and will be so for a little while yet)
the idea behind get_safe_generic_origin is to support rebuilding a type on 3.8 that was originally a generic collection e.g., List[<something>] -> List[<something else>] - where get_origin(List[<something>]) would be list and then list[<something_else>] would fail b/c of 3.8.
The instantiable origin one is to support getting a type like Sequence[int] and creating a list from it, we use it a bit in the DTO part of LS: https://github.com/litestar-org/litestar/blob/f5f7a4e6633748685ebaff0f4ad8410149906fac/litestar/dto/_backend.py#L581
I'd consider any part of this api up for change/debate though!
do you have a candidate name for my suggested property? to me "origin" would have been the name, and i feel like typing's get_origin is either poorly named or has bad semantics.
and in case it wasnt clear, i meant get_safe_generic_origin could disappear, because the impl to @property def safe_generic_origin would be return _SAFE_GENERIC_ORIGIN_MAP[self.<this property>]
- Interest in exposing
EmptyandEmptyTypetop level? I had my own equivalent sentinels, but i'd rather just not. - i'm not sure where to go with the whole discussion on
is_subtype_of. I'd be happy to just add a simpleris_subclass_ofas mentioned in the later comments, if it's worth having both (which i def think it is if we specifically dont want subtype_of to handle arbitrary input types ahead of time). I think my personal needs will be sufficed by that
submitted prs for all the things except #3 so far
Previously inlined into the native repr, but it's otherwise useful to have equivalent semantics in calling code
I dont suppose you're back yet? 😇
i just ran into another net-new feature that it'd be really convenient to be able to depend upon TypeView for in a released version of cappa 😛
he was active in GH yesterday, not sure if he is "active active"
Description
E.g.
from __future__ import annotations
class Foo:
bar: list[int] | None = None
This doesn't break anything in 3.8/3.9 due to the future import...until you use something like CallableView or TypeView to perform runtime type analysis, at which point it fails.
This PR makes use of eval-type-backport to make this function correctly in 3.8/3.9.
after this, i'll need to rebase my cappa branch, but i'm reasonably certain it's got everything I need for it to be depended on.
So if I can get passing tests there, i'd love to stage a release
@spice oar https://github.com/litestar-org/type-lens/pull/33. I believe this is the last of the bits and bobs I need personally, currently. and since peter hasn't been around...
I'd "just merge" most of the changes, except there was previously a reluctance for my change to the is_subtype_of method, that i hopefully justify in my comment.
No description provided.
Your explanation makes sense to me, seems good to merge
0.1.0 exists and was yanked, so that's why I'm skipping to 0.2.0, fwiw.
you dont need a fork anymore 🙂
mm also
https://github.com/litestar-org/type-lens/pull/36 and
https://github.com/litestar-org/type-lens/pull/37 and
https://github.com/litestar-org/type-lens/pull/38
also, i truly dont understand how the release docs/pypi publishing CI works. It seems like creating a release should have run both of them, but haven't. But I'm i guess just generally not familiar with the docs/publish setup of the stuff in the org as it is.
Also, aha: https://github.com/litestar-org/type-lens/actions/runs/11057458074. I guess someone with pypi permissions needs to set something up here
yes, member access lets you go as far as this (maintainers have to approve the final push) thats a different error
yea, from experience it needs someone that's a maintainer on the pypi package itself
which means @lilac raptor or @gentle vortex 😬 (thanks for making that info public pypi!)
I’ll check in just a bit
🦥
sorry in meetings first thing until 11
np no rush
taking a look now to see if i have this privs
@errant zinc permissions updated: https://github.com/litestar-org/type-lens/actions/runs/11057458074
published, lovely! thanks!
whatd u change?
It was still registered to be published under the jolt org
RIP
alright i think this https://github.com/litestar-org/type-lens/pull/39 is actually the last required PR.
Notably
- Literal is stupid in 3.8
- typing_extensions.Literal is a different object under 3.10
- Any (at least in 3.8) is a _SpecialForm that's not a type, so it ends up not working with the prior impl.
I wish all of typing wasn't built in. Its very annoying to deal with across versions
sigh, alright actually last one this time, definitely not wrong again 😬 https://github.com/litestar-org/type-lens/pull/40/files