enthought / enthought/traits

Simplify Expression trait

Open
#1,114 2 comments 1 reaction 0 assignees View on GitHub
type: refactor
Dominant language
Python
Stars
462
Forks
90
PR merge metrics
No merged PRs in 30d

Description

At the moment `Expression` trait makes use of `setattr_original_value` and has a validation method that modifies given value. This has caused issues with default value setting (https://github.com/enthought/traits/issues/1087, https://github.com/enthought/traits/issues/1096).

The use case for `Expression` seems pretty simple - original trait (e.g. `expression`) holds the "source" which is used for inspection and the shadow/mapped trait (e.g. `expression_`) holds the compiled code object. 2 current uses of `Expression` are in TraitsUI `table_column.py` and `table_filter.py` which fall under this use case.

Given this simple use of `Expression` it could avoid issues with `setattr_original_value` by being simplified as follows:
```python
class Expression(TraitType):
default_value = "0"
info_text = "a valid Python expression"

is_mapped = True

def validate(self, object, name, value):
try:
compile(value, "", "eval")
return value
except:
self.error(object, name, value)

def post_setattr(self, object, name, value):
object.__dict__[name + "_"] = self.mapped_value(value)

def mapped_value(self, value):
return compile(value, "", "eval")
```

Note: removing the use of `setattr_original_value` flag from `Expression` means that tests added in https://github.com/enthought/traits/issues/1087 no longer cover the targeted code branch in `ctraits.c`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.