Skip to content
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

Nonable fields don't work with type validations #51

Closed
devashishshankar opened this issue Nov 21, 2019 · 3 comments
Closed

Nonable fields don't work with type validations #51

devashishshankar opened this issue Nov 21, 2019 · 3 comments
Labels
bug Something isn't working

Comments

@devashishshankar
Copy link

If I have a Nonable field, setting it to None, or calling a constructor with the field unspecified should not raise a validation error. However, this happens if I have typegaurd installed

Example code to reproduce:

class A:
     f: str = field(check_type=True, default=None, nonable=True)
     @init_fields
     def __init__():
            pass

a = A()

This results in the following traceback:

raceback (most recent call last):
  File "/Users/devashishshankar/Work/ddp_v1_venv/lib/python3.7/site-packages/pyfields/typing_utils.py", line 58, in assert_is_of_type
    check_type(field.qualname, value, typ)
  File "/Users/devashishshankar/Work/ddp_v1_venv/lib/python3.7/site-packages/typeguard/__init__.py", line 544, in check_type
    format(argname, qualified_name(expected_type), qualified_name(value)))
TypeError: type of A.f must be str; got NoneType instead

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<makefun-gen-36>", line 2, in __init__
  File "/Users/devashishshankar/Work/ddp_v1_venv/lib/python3.7/site-packages/pyfields/init_makers.py", line 446, in __init__
    setattr(self, field_name, field_value)
  File "/Users/devashishshankar/Work/ddp_v1_venv/lib/python3.7/site-packages/pyfields/core.py", line 1066, in __set__
    assert_is_of_type(self, value, t)
  File "/Users/devashishshankar/Work/ddp_v1_venv/lib/python3.7/site-packages/pyfields/typing_utils.py", line 63, in assert_is_of_type
    raise new_e
pyfields.typing_utils.FieldTypeError: Invalid value type provided for 'A.f'. Value should be of type <class 'str'>. Instead, received a 'NoneType': None
@smarie smarie added the bug Something isn't working label Nov 22, 2019
@smarie
Copy link
Owner

smarie commented Nov 22, 2019

I am not able to reproduce the bug. Which version of pyfields do you use ? (you can see it using pyfields.__version__)

@smarie
Copy link
Owner

smarie commented Nov 22, 2019

I finally found it, sorry. On the way for a patch

@smarie smarie closed this as completed in b4df1d8 Nov 22, 2019
@smarie
Copy link
Owner

smarie commented Nov 22, 2019

By the way I now configured travis so that it tests the three configuration : without anything installed / with pytypes / with typeguard

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants