openfisca-core
openfisca-core copied to clipboard
Unify syntax, add style checks, remove obsolete Python 2 leftovers
Superset of openfisca/country-template#97
Follows discussion on #1033
Supersedes #977
Supersedes #960
Depends on #1160
Technical changes
- Document/honestify:
- Styles as enforced and skipped in
setup.cfg - Actual dependecy versions whith whom OpenFisca runs in py37-39 (note that this library does not run with py36, py310, numpy v1.17, etc., which is not product of any particular change introduced by this changeset)
- Styles as enforced and skipped in
- Make style checks stricter and clearer to help developers contribute:
- Early returns are prefered for readability and performance
- Before:
if 1 > n: var = something else: var = something_else return var - After:
if 1 > n: return something return something_elses
- Before:
- Normalise the use of:
- Quotes is normalised to double-quotes
@staticmethodwhenselfis unusedexcept fromfor better exception contextisinstance()as per the best practice- f-strings as per good practice
- Favour:
...as a placeholder andpassfor loops- Relative to absolute imports to avoid circularity
- Import sorting as described in the STYLEGUIDE
- Avoid:
- Catch-all exceptions in favour of more specific ones
- Import aliases (so
npbecomesnumpy)
- Remove:
- Code using
execbecause dangerous and hard to maintain
- Code using
- Extract:
- Common imports to
commons.imports
- Common imports to
- Early returns are prefered for readability and performance
- Most of these can be:
- Corrected automatically running
make format-style - Fixed by hand with the output of
make testandmake style-check
- Corrected automatically running
New features
- Rename and makes the following public (note this are not considered as
breking changes because methods were private, so renaming them to make
them public counts as actually adding a new publicly exposed method, while
removing a private method doesn't count as a breaking change):
Holder._to_arraytoHolder.to_arrayHolder._settoHolder.putHolder._memory_storagetoHolder.memory_storageparameters._compose_nametoparameters.compose_nameparameters._validate_parametertoparameters.validate_parameterParameterNodeAtInstant._nametoParameterNodeAtInstant.nameParameterNodeAtInstant._instant_strtoParameterNodeAtInstant.instant_strParameterNodeAtInstant._childrentoParameterNodeAtInstant.childrenPopulation._holderstoPopulation.holderssimulations._get_person_counttosimulations.get_person_countSimulation._check_for_cycletoSimulation.check_for_cycleSimulation._check_period_consistencytoSimulation.check_period_consistencyFlatTracer._enter_calculationtoFlatTracer.enter_calculationFlatTracer._exit_calculationtoFlatTracer.exit_calculationFlatTracer._start_timetoFlatTracer.start_timeFlatTracer._end_timetoFlatTracer.end_timePerformanceLog._jsontoPerformanceLog.json
- Add:
periods.year_startperiods.year_end- Extend:
test_runner.YamlItem.repr_failurewith a third argumentstyleas per the definition of the class it inherits from.
Breaking changes
- Expire deprecation of:
- The
Dummyclass, now provided bycommons formula_helpers, now provided bycommonsmemory_config, now provided byexperimentalrates, now provided bycommonsScale, now provided byParameterScaleBracket, now provided byParameterScaleBracketValuesHistory, now provided byParameterScaleParameterNotFound, now provided byParameterNotFoundErrorVariableNameConflict, now provided byVariableNameConflictErrorVariableNotFound, now provided byVariableNotFoundErrorsimulation_builder, now provided bysimulationsopenfisca-run-test, now provided byopenfisca testTaxBenefitSystem.new_scenario, now provided bySimulationBuilderAbstractRateTaxScale, now provided byRateTaxScaleLikeAbstractTaxScale, now provided byTaxScaleLike- In fine, All errors that were not but are not under
errors
- The
- Delete (no replacement):
scripts.measure_performancescripts.migrations.v16_2_to_v17
- Normalise the use of:
file_pathfor file context-managers (implies argument name rename)
Publising instructions
Because this library has a circular depedency with openfisca-country-template
and openfisca-extension-template, publish of this version requires:
- Publishing of one or many release candidates
- Publishing next major or
openfisca-country-template - Publishing next major or
openfisca-extension-template - Publishing next major of this library
- Publishing due diligence or unexpected outcome fixes as a release candidates
Hi @maukoquiroga : I am not very qualified to review all this work which BTW seems very substantial and valuable.
I am just wondering why you changed may methods _foo to foo or even foo_ ?
Hi @maukoquiroga : I am not very qualified to review all this work which BTW seems very substantial and valuable.
I'd like to have your opinion indeed, the idea would be to remove as many elements of style and keep those that are more tied to syntax and less controversial.
I am just wondering why you changed may methods
_footofooor evenfoo_?
That's a convention thing, he's the rationale, and why it may or not make sense for us:
Normally when you have _foo it means that a method is for internal use, and not to be called on externally. That's why usually documentation builders ignore them by default, and linters are going to warn you if you call them outside the scope.
So what I did was not to rename them, but to open them: because they're already being used as public methods, I made them public, while keeping the _ for backwards-compatibility.
Where it may not make sense for us is that we reorganised modules in a very modular way, and then we could for example argue that all methods marked as _foo inside the parameters module can be used inside, but not outside, etc.
Regarding foo_: that is just because _set, to be public, becomes set, but set is a builtin function, so it becomes set_.
That's an example of a pure style thing we could choose to ignore, as we do for variables for example, see https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles.
I think I went through all of your very helpful suggestions @MattiSG. This PR is in no way exhaustive in terms of style —for example we still. have quote use inconsistency— but it is a very good ground for further improvement.
This is too big for me to realistically review one more time, especially since the changeset is even bigger now. I have to trust that the initial review followed by pairing is enough 🙂
I believe that, in this case, the usual review approach is unfit for purpose. I see that you intend to release this as
36.0.0.rc1. Indeed, I suggest that we introduce for this case, both as a way to unblock and as an experiment for future similar cases, a prerelease system. Could we publish this as36.0.0-rc1(with a dash, not a dot, to abide by SemVer§9 and try it out / ask for it to be tried out on several country packages? 🙂 If no issues arise, then we'll publish as36.0.0! 😃 If there are issues, then we'll iterate and fix them.
The easier for that is then to improve the tooling with poetry or hatch to make it as easy as possible to hand publish.
I understand changing the dependency manager will make it easier in the future, but I would prefer we avoid putting this on the critical path of unblocking this PR, since this change will certainly yield additional decisions and issues.
@MattiSG #1160