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

Can use of exec() and locals() be avoided in processed_property's cast_type() function? #13

Open
brocksam opened this issue Aug 30, 2020 · 0 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@brocksam
Copy link
Owner

brocksam commented Aug 30, 2020

Code safety may be improved if used of exec() and locals() is avoided. See:

def cast_type(value):
"""Enforce type casting of property value to be set to specific type.
Parameters
----------
value : obj
Property object value for setting.
Returns
-------
obj
Supplied value cast to the specified type
Raises
------
ValueError
If the casting fails.
TypeError
If the casting fails.
"""
cast_str = f"processed_value = {expected_type.__name__}({value})"
try:
exec(cast_str)
except (ValueError, TypeError) as e:
name_str = generate_name_description_error_message()
msg = (f"{name_str} must be a {repr(expected_type)}, instead got "
f"a {repr(type(value))} which cannot be cast.")
raise e(msg)
return locals()['processed_value']

@brocksam brocksam added enhancement New feature or request question Further information is requested labels Aug 30, 2020
@brocksam brocksam modified the milestone: Pyproprop 0.2.0 release Sep 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant