-
-
Notifications
You must be signed in to change notification settings - Fork 108
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
Add introspection helper functions for third-party libraries #203
Conversation
We can also bikeshed over the names, etc. As I say, this is a proof-of-concept. Cc. @samuelcolvin / @adriangb / @ilevkivskyi -- curious if you'd find these useful? If you wouldn't, there's obviously no point :) |
For what it's worth, in pyanalyze (https://github.com/quora/pyanalyze/blob/master/pyanalyze/safe.py#L148) I found the following two functions useful:
I don't know when I would use your
|
Yeah, you're right -- that one was useless. I've removed it, and added an implementation of |
I think the idea (to help runtime users interact with typing-extensions better) is good. However, as a consumer of typing-extensions (in pyanalyze) I don't think I would use these functions. I need two things that this API doesn't give me:
I wouldn't want to merge this PR unless I hear from some other maintainers of runtime typing libraries who say they'll definitely use these functions. An alternative could be to put a recipe in the docs that suggests copying code like what you put in this PR and modifying it for your library's needs. |
FWIW my PR already has a cache on one of the two functions, and the other function is just a thin wrapper over the function that has the cache.
Fully agreed, and I agree we've seen no evidence of that yet. @ilevkivskyi / @adriangb / @agronholm, would any of you folks actually use these functions if we added these to |
Good point about the cache. Pyanalyze uses a somewhat different caching scheme but likely your lru_cache is enough. I do have another idiosyncratic need: my functions need to support strings (like |
Closing due to lack of interest among users |
Hey sorry I never gave any feedback here. We're getting into beta releases for Pydantic v2 so it's been pedal to the metal the last couple of weeks.
I agree that this would be a better first step than this PR. As part of the docs you can always change or even delete these examples without worrying about backward compatibility, and something like this is small enough that copy-pasting it into a codebase wouldn't be a huge concern. There's a good chance that if I put these into Pydantic's codebase I'd want to tweak something, I'm just not sure what yet. |
Sorry for late reply, here is some input from my side: Although in general I like the idea to of having some introspection helpers in
|
Thanks @ilevkivskyi, that makes sense! We've gone for adding recipes to the docs instead — see #225. |
This is a proof-of-concept for the idea that I mentioned in #185 (comment). The proposal is that we could provide two simple introspection helper functions for third-party libraries, and could recommend that they use these utilities as building blocks for their own introspection functions.
For example, pydantic has a function called
is_literal_type
, and typing_inspect does also. Both functions broke with the release of typing_extensions 4.6.0:With these introspection helpers, both libraries could rewrite their
is_literal_type
functions as:And you could use the utility functions in this PR as building blocks for all kinds of similar introspection capabilities, if you're a library that's doing that kind of thing. We could advertise and recommend these functions in the docs as the preferred way to do this kind of thing while avoiding breakage.
I'll add docs to this PR if there's interest in this idea -- didn't want to invest too much time into it if it's not something we want to pursue :)