opentelemetry-python icon indicating copy to clipboard operation
opentelemetry-python copied to clipboard

Use double underscore for private attributes

Open ocelotl opened this issue 2 years ago • 3 comments

Fixes #3097

ocelotl avatar Jan 02 '23 04:01 ocelotl

Some time ago I had a conversation where @aabmass pointed out we should make public class attributes that are intended to be used by other classes even if doing that would make them user-available as well, with the corresponding consequence of increasing our backwards-compatibility commitments. The intention here was to make encapsulation meaningful since now our classes freely access private attributes of other classes without any checking.

I understood the point of @aabmass but considered losing encapsulation the lesser of two evils when compared with increasing the increasing of the user-available API.

I now think we have a possible solution, to use double underscore to indicate actual "private" attributes and single underscore to indicate attributes that can be accessed by other classes but not by the end users.

This PR also includes some testing changes because we should always use [spec](https://docs.python.org/3/library/unittest.mock.html#autospeccing). A consecuence of using the approach defined above and spec is that private attributes are no longer defined in mocks of the refactored classes because they are not seen as part of the class API. That means they have to be added as private properties that return the double-underscored real private attribute.

ocelotl avatar Jan 02 '23 04:01 ocelotl

@ocelotl

Seems like we want to apply this pattern to the rest of the codebase. @ocelotl could you make a tracking issue to apply this change everywhere so it doesn't get lost?

lzchen avatar Jan 10 '23 20:01 lzchen

Converting to draft since these changes need to be applied to the entire codebase.

ocelotl avatar Feb 13 '24 00:02 ocelotl