Comments (5)
Forgot this one.
Example 3
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()
from enforce.
Right . . . I think they are separate issues but I will address them here.
- I think I know where the problem lies but it is a bit troublesome to pinpoint exactly. Decorating logic issue due to the wrapt library special handling of callables.
- Yeah, I have neglected generators, coroutines and other async stuff so far. Generators should be the easiest to add. PR are surely welcome, though!
- Looks like a corner case of NamedTuple special handling (because you inherited from it). Will give it a look.
- Forward Reference is not yet supported. I need to add some lazy evaluation to it. Tracked in #45
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.
from enforce.
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.
from enforce.
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.
from enforce.
Cheer up, I think this is the most advanced library out there for this kind of thing :)
Keep up the good work.
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).
from enforce.
Related Issues (20)
- License file
- enforce on static methods HOT 1
- runtime_validation yields a false positive when inheritance is involved HOT 2
- enforce is not transparent for Callable types - EnforceProxy HOT 2
- Typing error HOT 1
- It's impossible to install v 0.3.5 (`dev` branch) HOT 1
- Enable `runtime_validation` package/module-wide
- TypeError: 'NoneType' object is not iterable when using Optional[Tuple[str, str]]
- __init__ signature is not preserved for Generic classes HOT 1
- "User defined generic does not accept provided constraints" error when 'Type' is used in hint HOT 4
- Type check mypy TypedDict
- enforce ignore default values
- not compatible with python 3.7 HOT 9
- Forward references on classes
- @runtime_validation leaks refcounts and therefore causes memory leaks
- New enforce fork for Python 3.5.10, 3.6, 3.7, 3.8, 3.9 HOT 10
- `AttributeError: can't set attribute` with namedtuple HOT 1
- Python 3.8 AttributeError: module 'typing' has no attribute 'TupleMeta' HOT 1
- Data validation via `typing.Annotated`
- Support for `@overload`ed callables
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from enforce.