You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Should we add read-only attributes (or prefix with underscore)? #634
Currently our python objects tend to have many attributes that we only want modified by the API itself, e.g. all of the reference to ids. However, by not prefixing those attributes or making them read-only, we don't signal clearly to the users that they should not be tampered with.
At the same time (partially because of this?), we introduce a lot of extra work to test behavior which should only occur if the user uses the API in bad faith, e.g. manually overwriting ids.
PEP8 proposes that non-public attributes be prefixed with _. There is also the option of creating read-only properties like such:
all old code with read and write operations breaks
need to access 'non-public' attributes for run_id (for the correct use of reading the value).
Disadvantages to prefixing with a property:
all old code with write operations breaks
verbose as our classes may have many of attributes which should be read-only for outside users
Disadvantages of leaving as is:
more prone to users accidentally writing fields that they should not do
maintaining unit tests to test behavior in case those mistakes are made
There is of course also the opportunity of leaving things as they are without proper unit tests.
This might give some weird local behavior when things are done that shouldn't be done, but server-side there shouldn't be any issues (after all, you should be able to throw anything at the REST API directly anyway, right?).
What are your thoughts?
I feel that we should move in a direction which avoids creating/maintaining unit tests for users deliberately playing with our internals.
I feel that we should move in a direction which avoids creating/maintaining unit tests for users deliberately playing with our internals.
I completely agree on this. I'd be fine underscoring them in the first place, and if they need to be accessed from the outside, add a property to make sure that they are read only. However, I think this should be done slowly and not in one go.
Can you elaborate? Do you mean restructuring attributes slowly as e.g. one attribute per PR (but potentially starting the next as soon as its merged into develop)? Or slowly as in take plan to have everything updated by next year?
Having a plan when to change this always requires someone devoting time to it. If you have that time it would be great, otherwise, it might be hard to find someone to do this all at once.
Therefore, I would suggest to do this when such attributes are changed/touched/introduced. As a first step we should add this information to the contribution guide, though.
Activity
mfeurer commentedon Mar 5, 2019
I completely agree on this. I'd be fine underscoring them in the first place, and if they need to be accessed from the outside, add a property to make sure that they are read only. However, I think this should be done slowly and not in one go.
PGijsbers commentedon Mar 5, 2019
Can you elaborate? Do you mean restructuring attributes slowly as e.g. one attribute per PR (but potentially starting the next as soon as its merged into develop)? Or slowly as in take plan to have everything updated by next year?
mfeurer commentedon Mar 5, 2019
Having a plan when to change this always requires someone devoting time to it. If you have that time it would be great, otherwise, it might be hard to find someone to do this all at once.
Therefore, I would suggest to do this when such attributes are changed/touched/introduced. As a first step we should add this information to the contribution guide, though.