GithubHelp home page GithubHelp logo

Some typing errors about enforce HOT 5 CLOSED

russbaz avatar russbaz commented on May 18, 2024
Some typing errors

from enforce.

Comments (5)

kevinboulain avatar kevinboulain commented on May 18, 2024

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.

RussBaz avatar RussBaz commented on May 18, 2024

Right . . . I think they are separate issues but I will address them here.

  1. 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.
  2. Yeah, I have neglected generators, coroutines and other async stuff so far. Generators should be the easiest to add. PR are surely welcome, though!
  3. Looks like a corner case of NamedTuple special handling (because you inherited from it). Will give it a look.
  4. 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.

kevinboulain avatar kevinboulain commented on May 18, 2024

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.

RussBaz avatar RussBaz commented on May 18, 2024

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.

kevinboulain avatar kevinboulain commented on May 18, 2024

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).

from enforce.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.