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

How to use Generics? #51

Open
justuswilhelm opened this issue Aug 22, 2017 · 2 comments
Open

How to use Generics? #51

justuswilhelm opened this issue Aug 22, 2017 · 2 comments

Comments

@justuswilhelm
Copy link

justuswilhelm commented Aug 22, 2017

In mypy you can use this

import typing


def returns_dict() -> typing.Mapping:
    return dict()


returns_dict()

And it validates just fine.

But with enforce I get a strange result

import enforce
import typing


@enforce.runtime_validation
def returns_dict() -> typing.Mapping:
    return dict


returns_dict()

Seemingly, the generic type handling is different in enforce.

Traceback (most recent call last):                
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/nodes.py", line 539, in preprocess_data
    enforcer = data.__enforcer__                  
AttributeError: type object 'dict' has no attribute '__enforcer__'                                   

During handling of the above exception, another exception occurred:                                  

Traceback (most recent call last):                
  File "test.py", line 10, in <module>            
    returns_dict()       
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/decorators.py", line 118, in universal
    return enforcer.validate_outputs(result)      
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/enforcers.py", line 96, in validate_outputs
    if not self.validator.validate(output_data, 'return'):                                           
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/validator.py", line 26, in validate
    validation_result = visit(validation_tree)    
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/utils.py", line 17, in visit
    stack.append(last.send(last_result))          
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/nodes.py", line 64, in validate
    clean_data = self.preprocess_data(validator, data)                                               
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/nodes.py", line 541, in preprocess_data
    return GenericProxy(data)                     
  File "/Users/XXX/enforce-test/env/lib/python3.6/site-packages/enforce/enforcers.py", line 130, in __init__
    raise TypeError('Only generics can be wrapped in GenericProxy')                                  
TypeError: Only generics can be wrapped in GenericProxy    

What's the best way to get a similar behavior to mypy?

@RussBaz
Copy link
Owner

RussBaz commented Aug 22, 2017

Well, you are not returning a dictionary in your second example. The return value is a function which returns a dictionary. Is it a typo or is it what you intended?

@justuswilhelm
Copy link
Author

Sorry, that was a typo in my post above. The method should return dict(). I tried return a dict type just now, and that does not change the error message. Which makes sense, since the decorator phase fails, not the execution phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants