-
Notifications
You must be signed in to change notification settings - Fork 242
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
Support metapogramming-style magic (Metatypes?) #293
Comments
Looks like this is (yet another) a perfect use case for
python/mypy#1240 which exhorts us to design and
implement a plugin system for mypy.
I think you're on to something with your suggestion on how this could be
implemented. Would you care to think some more about the simplest
implementation of your idea?
|
I'll write something up! I have a good idea on how we could tackle this, but I'm going to look a little bit into mypy internals to decide on how certain details could be solved (the trickiest issue I can see is around circular dependencies). |
It would be good to have a more concrete proposal about how this could be used in a type checker. |
Proposal for metatypesBy settings a Whenever a tool like mypy parses a class definition, it will build up an object to store that type information, containing things like the class's methods and fields, as well as fields that appear on its instance. After loading the class, if a An example of what the type information could be shaped like:
As an example:
As a starting point, we would say that the type is run through the metatype once all forward references in types have been resolved. Though, because of circular references, we cannot be certain that all metatypes have been processed at that point, so it would be the responsibilities of the writers to understand that type information might not be completely initialized by that point. The big change that would be required here is actually loading the code during type checking. This is in order to provide the class to the metatype to provide useful information. For example, in the ORM case, to find the type of the field that is wanted. I don't know how feasible this is in some cases, but I believe it can be useful in a lot of "code generation" situations. Another small example would be a class loading its class definition from a file (useful in dealing with things like WSDL):
There we could imagine the metatype loading the file into memory and parsing it using the same tool that the |
Another thing that was mentioned in #399 is that some of such patterns may be described using descriptors (even if the actual runtime mechanism is more complex). This is for example what is done in |
I don't think that static type checking could ever support arbitrary code in a generic way. By now we have type checkers written in Python, JavaScript, C, and Java. Of course, individual type checkers could support extensions in various ways. I am closing this here though. |
I'm sorry, the following will be vague and could be totally out of scope:
In Django if you do
In this situation, in the final run, you have:
C.name
raisesAttributeError
C().name
bestring
This situation is a bit complicated, but really common for some of the more magical parts of python that use metaclasses.
My understanding is that the current philosophy on typing relies on a declarative view of things. But wouldn't it be neat if users could hook their own code into the type-building process?
For example, here:
I think building something like this could potentially be game changing for the future of typing in Python. Totally unsafe, but are the advantages worth the safety loss?
Pros:
contribute_to_type
mechanism. Things would no longer always need to go through "PEP -> mypy/PyCharm " to be tried out.Cons:
For example, if you have
then it's not sure what
contribute_to_type
will execute on. Though this problem is also present in checkers generally, so maybe this isn't a change of situation.There's a bit of trickiness regarding circular imports as well. If you execute
contribute_to_type
but the field type is not available yet, how do you resolve that?You could imagine something like:
but that won't fully solve circular dependencies.
based off of my understanding of how the common checkers work, this would be a change that is internal to the "determining type from class definition code"-part of the code: the type inference/checking code itself shouldn't need to be changed.
The text was updated successfully, but these errors were encountered: