-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify boilerplate #467
Simplify boilerplate #467
Conversation
4b4f56d
to
70714db
Compare
If you have better ideas for the name of the module, feel free. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If formula_toolbox
only exposes some very common functions as globals in order to avoid repetitive imports, then what about naming it shortcuts
? Or imports
?
If it constitutes all of what is used to create formulas, then what about naming it formula_api
?
openfisca_core/formula_toolbox.py
Outdated
) | ||
from .variables import DatedVariable, Variable # noqa analysis:ignore | ||
from .formula_helpers import apply_thresholds, switch # noqa analysis:ignore | ||
from .periods import MONTH, YEAR, ETERNITY # noqa analysis:ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are there these awful
# noqa
on each line? 😷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise the flake8
linter doesn't pass as we import stuff we do not use.
And no, there is no way to deactivate a specific error in a specific file. The only options I found are :
- Deactivating a specific error for the whole project
- Deactivating linting on a given line with
noqa
@cbenz if you know a better way, I'll happily take it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The better way would be to stick to the good practices, which are to import only what is needed by the code of the module.
As now, some Python modules are used as "import forwarders", letting other modules to import them in order to benefit from the imports they made... wow!
Adhering to these good practices will make this behavior forbidden.
But this is not in the scope of this PR.
To avoid warnings in some editors ...
|
It constitutes all what is needed to write variables and formulas. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I explained in my comments has some impact on many modules of France.
But I think the path that was taken is not the best, to solve the problem of the boilerplate code.
I think the problem is on France: do we want all the model.*
modules to import all the columns
, periods
, etc. or not ? My answer is yes, because this will lead to a clean surface API of OpenFisca, rather than hiding it under an import *
statement.
openfisca_core/formula_toolbox.py
Outdated
@@ -0,0 +1,37 @@ | |||
# -*- coding: utf-8 -*- |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not fond of the naming formula_toolbox
(like other variants like _helpers
or _functions
).
I would prefer to name the module after what's inside.
Second, this module contains only imports, which is weird. I would prefer to import directly these modules from the impacted countries (like here)
But this is transitive and does not stop there! I mean openfisca_france/model/base.py
should not exist neither lines like from openfisca_france.model.base import *
.
We've got to do a choice: either we keep these "forwarding-only modules", either we forbid them and make real imports from modules.
openfisca_core/formula_toolbox.py
Outdated
) | ||
from .variables import DatedVariable, Variable # noqa analysis:ignore | ||
from .formula_helpers import apply_thresholds, switch # noqa analysis:ignore | ||
from .periods import MONTH, YEAR, ETERNITY # noqa analysis:ignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The better way would be to stick to the good practices, which are to import only what is needed by the code of the module.
As now, some Python modules are used as "import forwarders", letting other modules to import them in order to benefit from the imports they made... wow!
Adhering to these good practices will make this behavior forbidden.
But this is not in the scope of this PR.
## 6.1.0 | ||
|
||
* Move `base.py` content (file usually located in country packages) to core module `formula_toolbox` so that it can be reused by all countries | ||
* Use `AbstractScenario` if no custom scenario is defined for a tax and benefit sytem |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because right now the AbstractScenario
is not really abstract, and can do the job without being overloaded.
Without it, the boilerplate country package needs to create its own Scenario
, which only contains:
from openfisca_core.scenarios import AbstractScenario
class Scenario(AbstractScenario):
pass
My answer would be no 😈. I think it's interesting to offer formula writers a bunch of tools to write formulas, without forcing them to think about where they come from. I've worked with developers from local collectivities, and they are usually not python experts. It's more trouble for them to dig into the
I'd argue the opposite. This module would actually be our clean surface api to write formulas. The promise is that this module contains all what you need to do so. And the bad practise would be to dig deeply into |
An import-forwarding module is not a clean surface API, it's hiding things under a carpet ;-)
A good surface API would allow users to do Think of The fact is that, given the current state of the code, my comment is not really concrete. In short, and given the constraints, at least I'd like to replace the indirection And rename I'm approving this PR, but I'd like one day to address those questions. |
I'll adapt the boiler plate country to |
Yep, and to do this, numpy actually does... import forwarding 😄 .
"Copy paste a block" is an expression that raises huge alerts for me. Given the big number of imports we use to write formulas, that'd be a big block. I understand that
I would go for
The only difficulty is that you need two things to write formulas :
The only role of |
To sum up:
To make the boilerplate country work on an official version of core, I'd like to merge this PR now, and let the door open to further improvements. |
bc80612
to
be7370b
Compare
base.py
content (file usually located in country packages) to core moduleformula_toolbox
so that it can be reused by all countriesAbstractScenario
if no custom scenario is defined for a tax and benefit sytemSee impact on openfisca/openfisca-france#697