-
Notifications
You must be signed in to change notification settings - Fork 20
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
Compliance with vtypes
#86
Comments
This looks like correct behavior to me:
Maybe you can achieve what you want if you use a subclasshook in
However, compliance with subclasshooks is not much tested in pytypes. Use with care! On the surface it seems to work, like in the example above. But in complex situations, I recommend careful testing. Please report back bugs with pytypes and subclasshook if they pop up. Also note that there are literal types now, which have some overlap with what vtypes wants to achieve. However literal types are not yet supported in pytypes and I cannot tell when they will be. |
The thing is, a PositiveInt = vtype('PositiveInt', int, {'should be positive': lambda x: x >= 0}) or class PositiveInt(VType):
__type__ = int
__validators__ = {'should be positive': lambda x: x >= 0} Both are equivalent to my initial example. So, the only way to get it right is to call You are right that there is one similarity with literal types, in the sense that the type validation is on the value, not on the type. We can see a literal type as a Thanks! |
Indeed, I overlooked that Unfortunately, the Python documentation and also the original PEP seem not to be specific about which check should be authoritive over the other: Maybe the best for all usecases would be to make it configurable. However, it is non-trivial work to let |
Okay, I had some thought and I think the best solution would be:
These actions should cause vtypes and pytypes to be compatible, right? How this sounds? |
I realize it is more complicated to get this right if the vtype is not toplevel, but somewhere nested in tuples, etc.. However, similar work would be required for LiteralType support. So it should be done at some point, but I cannot tell when. |
I am not sure there is a need to change the subclass behaviour. Current behaviour seems intuitive to me: >>> issubclass(PositiveInt, int) # all positive ints are ints
True
>>> issubclass(int, PositiveInt) # not all ints are positive ints
False Now I understand that more work is needed to integrate |
Indeed, that should be this way.
I have another idea, that would be rather easy to implement, but computationally not very efficient. It would require an opt-in, so it may be okay. This would let the nesting logic be done by |
This is one solution, another one even simpler could be to rely on Maybe you should just reject this proposal for now - a Also, maybe we should wait for |
The thing is, I wanted to have value types anyway. In the long run, to be able to work with numpy arrays. That's why this is not rejected. However, the priority is #40 and getting the basics right. The value types I have in mind are somewhat different from vtypes but this is reason enough to consider preparing the necessary architecture (also for Literal types).
A main motivation in pytypes is to have a proper PEP 484 aware What I might do is kind of (in pseudo code):
But that requires to provide the value throughout various methods that are originally designed for subtypechecking. This requires more architecture change. I will consider this after #40. Also, don't expect a solution too soon. You may have to suspend pytypes support for a while. |
not all registered classes. Only those that are also subclasses of the target class. They would be stored in a dict with the target class as the key, e.g. The bet here is of course that one would normally not have thousands of different validators for the same type, but only a handful. |
I see. Thanks for these thoughts ! I'll check for updates from time to time. I added a note in |
Hi, I found out that
pytypes
was not able to validate avtype
:I do not know if this can be fixed easily, but I thought you could be interested. Indeed
is_of_type
documentation statesSince
pytypes
is one of the two type validation library supported by default inpyfields
, I would like to make both behave the same way (typeguard
seems to handlevtype
ok)Thanks!
The text was updated successfully, but these errors were encountered: