smarie / python-vtypes

Validating types for python - use `isinstance()` to validate both type and value.

Home Page:https://smarie.github.io/python-vtypes/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Calling vtype at top level fails

piccolbo opened this issue · comments

Hi this is just an inconvenience I am reporting but it may discourage some people just checking out your package. When I first found about it i just typed this into an ipython session

from vtypes import vtype
PositiveInt = vtype("PositiveInt", int, {"should be positive": lambda x: x >= 0})

and got

In [7]: PositiveInt = vtype("PositiveInt", int, {"should be positive": lambda x: x >= 0})
   ...: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [7], in <cell line: 1>()
----> 1 PositiveInt = vtype("PositiveInt", int, {"should be positive": lambda x: x >= 0})

File /usr/local/lib/python3.10/site-packages/vtypes/core.py:400, in vtype(name, base, validators, help_msg, error_type, doc)
    386 """
    387 Creates a new Validating Type, a subclass of VType.
    388 
   (...)
    396 :return:
    397 """
    398 new_type = VTypeMeta(name, (VType,), dict(__type__=base, __validators__=validators,
    399                                           __help_msg__=help_msg, __error_type__=error_type))
--> 400 new_type.__module__ = get_caller_module().__name__
    401 if doc is not None:
    402     new_type.__doc__ = doc

AttributeError: 'NoneType' object has no attribute '__name__'

It works just fine running from a file with %run, because, I suppose, then the caller module is that file, but not with %load, because that's equivalent to typing it in, and apparently it was too hard for the Python folks to call that a stdin or console module or what have you.

So this is not a problem developing with your package, but when you try out things and the first thing you do you break them, it may be discouraging. Since I had already looked into your other work, that was not going to stop me!

Wow so sorry for this @piccolbo !

Unfortunately currently this project still has an old layout (that is using travis ci and does not include nox) so not sure how easy it would be for you to propose a PR if you would like to.

Alternately, I'll have to spend some time migrating the layout to the new format (same as for example https://github.com/smarie/python-makefun). Not sure when I'll be able to do this, probably during summer

Since I had already looked into your other work, that was not going to stop me!

thanks for the kind words ! I appreciate :)