Closed kevinboulain closed 7 years ago
Forgot this one.
Code:
import datetime
from typing import List, NamedTuple
import enforce
class Example(NamedTuple):
datetime: datetime.datetime
@enforce.runtime_validation
def example() -> List[Example]:
return [Example(datetime.datetime.now())]
example()
Traceback:
Traceback (most recent call last):
File "enforce_examples.py", line 9, in <module>
def example() -> List[Example]:
File ".../venv/lib/python3.6/site-packages/enforce/decorators.py", line 58, in runtime_validation
return generate_decorated()
File ".../venv/lib/python3.6/site-packages/enforce/decorators.py", line 158, in build_wrapper
return decorate(wrapped, configuration, None)
File ".../venv/lib/python3.6/site-packages/enforce/decorators.py", line 70, in decorate
data = apply_enforcer(data, parent_root=parent_root, settings=configuration)
File ".../venv/lib/python3.6/site-packages/enforce/enforcers.py", line 159, in apply_enforcer
func.__enforcer__ = generate_new_enforcer(func, generic, parent_root, instance_of, settings)
File ".../venv/lib/python3.6/site-packages/enforce/enforcers.py", line 227, in generate_new_enforcer
validator = init_validator(hints, parent_root)
File ".../venv/lib/python3.6/site-packages/enforce/validator.py", line 71, in init_validator
syntax_tree = visit(root_parser)
File ".../venv/lib/python3.6/site-packages/enforce/utils.py", line 17, in visit
stack.append(last.send(last_result))
File ".../venv/lib/python3.6/site-packages/enforce/parsers.py", line 54, in _parse_namedtuple
new_node = yield nodes.NamedTupleNode(hint)
File ".../venv/lib/python3.6/site-packages/enforce/nodes.py", line 390, in __init__
super().__init__(runtime_validation(data_type), is_sequence=True, is_container=True, **kwargs)
File ".../venv/lib/python3.6/site-packages/enforce/decorators.py", line 46, in runtime_validation
return get_typed_namedtuple(configuration, data, fields, field_types)
File "...venv/lib/python3.6/site-packages/enforce/decorators.py", line 180, in get_typed_namedtuple
exec(new_init_template, context)
File "<string>", line 1, in <module>
NameError: name 'datetime' is not defined
Can't remember the first time I got this one, sorry (it was with a custom type and not datetime
but the error was similar).
EDIT: just remembered where I found a similar issue: when using forward references as indicated in the PEP/documentation.
Code:
import enforce
class Example:
@classmethod
@enforce.runtime_validation
def create(cls, *args, **kwargs) -> 'Example':
return cls(*args, **kwargs)
Example.create()
Right . . . I think they are separate issues but I will address them here.
This is a pretty huge report. It will certainly take time to fix all of them as I am not very free at the moment. Anyway, I will see what I can do given the amount of time I have.
Thanks.
This issue could be split into smaller ones (I can open new ones and close this one), whatever is best for you, I just batched some errors that I encountered while using enforce.
The Callable
issue isn't that important: it's just less self-documenting and move the burden of checking the arguments to the callee.
For the generators/iterators/... that's what I thought when I saw the empty tests. :)
For NamedTuple
inheritance, it could be nice to handle it as it seems to be the recommended way to do it.
You are right. Sigh... More work for me. Hahaha. Alright, I will try fixing some of them on the weekend.
Anyway, I would prefer to have separate issues. If you can, please split them.
Thank you.
Cheer up, I think this is the most advanced library out there for this kind of thing :) Keep up the good work.
I opened #46 & #47.
By the way, I'm not sure if Python is really suited/designed for strict typing so maybe you shouldn't try to implement everything. But I do really like to have the ability to 'typecheck' some important parts of the program (for me, this is one of the most annoying thing with Python: even if the ease of development is great, there are too much ways to get bitten and I think enforce allow us to have a little more confidence on some critical parts).
Hi there, I got some typing errors and I'm not sure where the problems came from, could you give me your thoughts about the following examples? Feel free to close the issue if there is nothing wrong on enforce's side.
Version used,
pipe show enforce
:Example 1
In this case, there is no problem if the callback is not a bound method.
Code:
This code may raise two different tracebacks, the difference between the two being the type of the function:
The errors go away when the
callback
type is less strict:Is this the expected behavior?
Example 2
The traceback makes me think this may be an error on enforce's side.
Code:
Traceback:
There is a similar problem when a function returns a
Coroutine
.