Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Option to fail silently when resource optional but valid attr is not set #6
Currently, if trying to access resource.attr when the attr is not set on the resource will raise an exception.
This is useful when trying to access an invalid attribute for a given resource, like "issue.date_due" instead of "issue.due_date". But this exception is also thrown up when the given attr is not set on the resource, even though it is valid for the given resource (for example, if due date is not set, issue.due_date will raise an exception.
One could always wrap code in try catch statements, but this might be impractical, especially in contexts when this is not as easy to do (for example, in django template code)
Only two things come to my mind at the moment:
What do you think ?
I agree that a solution to such a problem is not available without some sort of compromise. I also agree that the first solution you listed would be a support nightmare.
But the second, if it's given as a constructor parameter for the main Redmine object or wherever, is a "risk" the user takes - just like setting https request verification to False - I find this less of a problem.
I hit this problem while listing issues inside a template, some had an optional field set, some didn't. For this case, a different kind of compromise can be imagined: each Redmine session could infer existing field names for the current connection for each resource type, and return None in getattr whenever we know this field existed in at least one resource of this type, not necessarily the current one.
This will at least give the connector knowledge of the fields that it received up to one point. These may very well not be all fields, but it might be a step forward (also, at least in some usecases, an user is less likely to request attrs that none of the resources he requested has, right?)
Okay, you convinced me ;-)
I like your idea about per session field names memorization, but I don't think it will work, because if a first object in a queue won't have a requested field an exception will be thrown anyway.
So I believe the option 2 is the best we can do. I added the raise_attr_exception kwarg to Redmine object which defaults to True to not break existing code. See 11d3f5e. You can set it to False to completely disable the ResourceAttrError or you can give it a list or tuple of resource class names and it will continue to raise an exception only for this types of resources. All other resources will return None and remain silent. Details can be found in the documentation.
Please reopen if anything isn't working like it should. Thanks.