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

Built-in function for chaining types to provide inside Bear.run() as annotation #1859

Closed
Makman2 opened this issue Mar 9, 2016 · 11 comments
Closed

Comments

@Makman2
Copy link
Member

Makman2 commented Mar 9, 2016

Originated from #1855, where multiple-types shall be supported. coala already has a way to define your own parsing function:

def bool_or_int(value):
    if value == "False":
        return False
    val = int(value)
    if val == 0:
        raise ValueError
    return val

def run(value: bool_or_int):
    pass

The task of this issue to implement a generic function that works like this:

def run(value: typechain(bool, int))
    pass

So it takes a sequence of types and tries to parse them in order. If nothing works, raise a ValueError.

@adtac
Copy link
Member

adtac commented Mar 13, 2016

Would it be enough to have a helper function that does this job inside the function instead of verifying at the function prototype definition?

Example:

def run(val_1, val_2): # unrestricted
    types.validate(val_1, [bool, int])
    types.validate(val_2, [string])

And types.validate raises ValueError if it doesn't match. I have no knowledge on how to validate types directly at the argument definition line.

@sils
Copy link
Member

sils commented Mar 13, 2016

the thing is if we have it as the annotation then coala will auto convert
it and the user doesn't have to care any further, this way he'd have to
manually error out if something's inconveertible and stuff

Sincerely,

Lasse Schuirmann

contact@viperdev.io
http://viperdev.io/

2016-03-13 13:28 GMT+01:00 Adhityaa Chandrasekar notifications@github.com:

Would it be enough to have a helper function that does this job inside
the function instead of verifying at the function prototype definition?

Example:

def run(val_1, val_2): # unrestricted
types.validate(val_1, [bool, int])
types.validate(val_2, [string])

And types.validate raises ValueError if it doesn't match. I have no
knowledge on how to validate types directly at the argument definition line.


Reply to this email directly or view it on GitHub
#1859 (comment)
.

@adtac
Copy link
Member

adtac commented Mar 13, 2016

No, we can let the function raise an error if the variable doesn't match the type. The user doesn't need to do anything IMO.

@Makman2
Copy link
Member Author

Makman2 commented Mar 13, 2016

Having this inside the signature is way better since coala is then able to provide more help for that bear. Inside the body coala can't influence/see anything. Also your validation function would do pretty much the same, so having the extra signature advantage is good keep^^

@adtac
Copy link
Member

adtac commented Mar 13, 2016

Is a function prototype like

@enforce(int, float)
def run(arg1, arg2):
    ...

make the enforced variable types available to bear help?

@sils
Copy link
Member

sils commented Mar 13, 2016

right now it's more:

def run(arg1: int, arg2: float):
    ...

and coala will always give the bear an int and a float, if conversion is not possible the bear won't be executed

@Uran198
Copy link
Contributor

Uran198 commented Mar 20, 2016

Where does this function have to live?

@sils
Copy link
Member

sils commented Mar 20, 2016

I think StringConverter would be the right place

@Makman2
Copy link
Member Author

Makman2 commented Mar 20, 2016

@sils1297 what about directly inside Bear? since it's general purpose type-chaining^^

@sils
Copy link
Member

sils commented Mar 20, 2016

it's general purpose, it's not at all tied to bears

@Uran198
Copy link
Contributor

Uran198 commented Mar 24, 2016

I want to get assigned :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

5 participants