-
Notifications
You must be signed in to change notification settings - Fork 35
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
is_member_of_type #2
Comments
@lucaswiman Before you start with this approach, please note that I already did exactly this work. Including support for type variables. Including issubtype. Already supporting older versions of typing module, at least from 3.5.2.2 onwards. Tested with Python 2.7, 3.5, 3.6, 3.6.1 and even Jython. Implementing this was much more involved than one might expect (e.g. getting things like |
No, that looks fantastic! I searched for something which implements that collection of functionality, and could only find runtime typecheckers. In the abstract, I think much of your |
Having read through the documentation in more detail, I guess only a small subset of it makes sense for the standard library, though several of the "Utilities" functions would be good candidates. |
After some thinking, it seems to me this should not be in Anyway, thanks for a nice write-up! |
That's disappointing; I do hope you'll reconsider after the From the perspective of an end user of
|
I would say that this is a perfect reason for you to contribute to and improve these projects, so that they will suit your needs. The goal they declare seems to be what you need, while the main goal of |
The most useful introspection methods I'd like to use from the
typing
module are basically the functions that were removed in python/typing#283: checking whether a concrete object is compatible with a given type. This would be useful for simple runtime type assertions, for exploring the implications of PEP484 in the REPL, and annotations-based dispatch.Please let me know what you think; I'd be very interested in helping to implement this. I can also submit a PR if that makes discussion easier.
Objections
Should this be in external tools?
@1st1 objected to this on the grounds that it should be left to external tools, but it seems like implementing a reference for cases which are unambiguous according to PEP484 would be quite useful, and even helpful to building external tools of that kind.
For cases where something other than recursive
isinstance
calls are required (e.g. type variables), a helpful error can be raised at runtime.Mutable values
There was an objection to
__instancecheck__
and__subclasscheck__
by BDFL-delegate @markshannon here:This is a bit tricky for mutable types, since
[]
is a member ofList[X]
for allX
, but the same list object might cease to be inList[X]
when elements are added to it. For immutable types, the answer is unambiguous, soTuple[*]
could always deliver an unambiguous answer.For
List[]
,Dict[]
and other mutable types, my preference would be to check the types of the elements right now, when the method is called, and not at some future time when it may have been mutated. This would be a distinction betweenmypy
and the runtime type checking, but this behavior seems more in keeping with the principle of least surprise, and would accord with the main intended use cases (REPL-based exploration of the type system, and simple runtime type assertions).Implementation
I think the implementation would be roughly like the following:
Include
issubtype
to matchissubclass
?issubtype
could make sense as an addition, but it would involve introducing much more of the sorts of logic a typechecker should implement, e.g. the following:For this reason, I think it would make more sense to leave
issubtype
out oftyping_inspect
.The text was updated successfully, but these errors were encountered: