maint: convert to cached_property and other minor refactors #1870
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
This is a continuation of #1760.
I have a few motivations. One is to understand why some of the version-computing logic is so complicated, and to simplify that if possible. (See for example maresb/hatch-vcs-footgun-example#3.) Also, I have fantasies about being able to close #304.
In each case, I need to understand in detail what the code is doing so that I can make modifications without causing breakage. But then I get bogged down in complexity that to me seems inessential. So as I go through this process, I try to either eliminate inessential complexity or to document why the complexity is essential.
Currently, property values are cached by-hand in a variable of the same name prefixed with an underscore. As long as this variable is only used by that property, and only with the usual caching boilerplate, this can be directly converted to
@cached_propertyby adding the decorator and removing the cache variable and boilerplate. Sometimes the property is a mutable container likeProjectMetadata.dynamic(which is depopulated as dynamic values are computed), but this detail doesn't affect the caching logic. In other cases likeCoreMetadata._readme_content_type, the variable is modified not only by the.readme_content_typeproperty but also the.readmeproperty. Since this PR is already getting a bit heavy I have left these cases untouched.Along the way I've made a few other very minor refactors and added some comments.
Hopefully I made no mistakes and these changes are helpful. Thanks!