Skip to content

Commit

Permalink
fix: merge errors
Browse files Browse the repository at this point in the history
  • Loading branch information
bonjourmauko committed Sep 13, 2024
1 parent 82eaaae commit e0b65f3
Show file tree
Hide file tree
Showing 23 changed files with 85 additions and 101 deletions.
3 changes: 2 additions & 1 deletion openfisca_core/commons/misc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from openfisca_core.types import Array
from typing import TypeVar

from openfisca_core.types import Array

T = TypeVar("T")


Expand Down
1 change: 0 additions & 1 deletion openfisca_core/commons/rates.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from openfisca_core.types import Array, ArrayLike
from typing import Optional

from openfisca_core.types import Array, ArrayLike
Expand Down
3 changes: 2 additions & 1 deletion openfisca_core/entities/_core_entity.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import annotations

from abc import abstractmethod
from openfisca_core.types import TaxBenefitSystem, Variable
from typing import Any

from openfisca_core.types import TaxBenefitSystem, Variable

import os

from .role import Role
Expand Down
9 changes: 4 additions & 5 deletions openfisca_core/errors/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@
from .situation_parsing_error import SituationParsingError
from .spiral_error import SpiralError
from .variable_name_config_error import VariableNameConflictError
from .variable_name_config_error import (
VariableNameConflictError as VariableNameConflict,
)
from .variable_not_found_error import VariableNotFoundError # noqa: F401
from .variable_not_found_error import VariableNotFoundError as VariableNotFound

from .parameter_not_found_error import ( # noqa: F401, isort: skip
ParameterNotFoundError as ParameterNotFound,
)

from .variable_name_config_error import (
VariableNameConflictError as VariableNameConflict,
)
from .variable_not_found_error import VariableNotFoundError
from .variable_not_found_error import VariableNotFoundError as VariableNotFound

__all__ = [
"CycleError",
Expand Down
6 changes: 0 additions & 6 deletions openfisca_core/errors/variable_not_found_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,15 @@ def __init__(self, variable_name: str, tax_benefit_system):
country_package_version = country_package_metadata["version"]
if country_package_version:
country_package_id = f"{country_package_name}@{country_package_version}"
country_package_name, country_package_version
)
else:
country_package_id = country_package_name
message = os.linesep.join(
[
f"You tried to calculate or to set a value for variable '{variable_name}', but it was not found in the loaded tax and benefit system ({country_package_id}).",
variable_name, country_package_id
),
f"Are you sure you spelled '{variable_name}' correctly?",
"If this code used to work and suddenly does not, this is most probably linked to an update of the tax and benefit system.",
"Look at its changelog to learn about renames and removals and update your code. If it is an official package,",
f"it is probably available on <https://github.com/openfisca/{country_package_name}/blob/master/CHANGELOG.md>.",
country_package_name
),
]
)
self.message = message
Expand Down
6 changes: 3 additions & 3 deletions openfisca_core/parameters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@
from .parameter_scale_bracket import ParameterScaleBracket
from .parameter_scale_bracket import ParameterScaleBracket as Bracket
from .values_history import ValuesHistory
from .vectorial_asof_date_parameter_node_at_instant import VectorialAsofDateParameterNodeAtInstant
from .vectorial_parameter_node_at_instant import VectorialParameterNodeAtInstant
VectorialParameterNodeAtInstant,
from .vectorial_asof_date_parameter_node_at_instant import (
VectorialAsofDateParameterNodeAtInstant,
)
from .vectorial_parameter_node_at_instant import VectorialParameterNodeAtInstant

__all__ = [
"ParameterNotFound",
Expand Down
2 changes: 0 additions & 2 deletions openfisca_core/parameters/parameter_at_instant.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ def validate(self, data):
if not isinstance(value, config.ALLOWED_PARAM_TYPES):
raise ParameterParsingError(
f"Value in {self.name} has type {type(value)}, which is not one of the allowed types ({config.ALLOWED_PARAM_TYPES}): {value}",
self.name, type(value), config.ALLOWED_PARAM_TYPES, value
),
self.file_path,
)

Expand Down
6 changes: 5 additions & 1 deletion openfisca_core/parameters/parameter_node_at_instant.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def __getitem__(self, key):
if isinstance(key, numpy.ndarray):
# If fancy indexing is used wit a datetime64, cast to a vectorial node supporting datetime64
if numpy.issubdtype(key.dtype, numpy.datetime64):
return parameters.VectorialAsofDateParameterNodeAtInstant.build_from_node(self)[key]
return (
parameters.VectorialAsofDateParameterNodeAtInstant.build_from_node(
self
)[key]
)

return parameters.VectorialParameterNodeAtInstant.build_from_node(self)[key]
return self._children[key]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import numpy

from openfisca_core.parameters.parameter_node_at_instant import ParameterNodeAtInstant
from openfisca_core.parameters.vectorial_parameter_node_at_instant import VectorialParameterNodeAtInstant
from openfisca_core.parameters.vectorial_parameter_node_at_instant import (
VectorialParameterNodeAtInstant,
)


class VectorialAsofDateParameterNodeAtInstant(VectorialParameterNodeAtInstant):
Expand All @@ -15,52 +17,58 @@ def build_from_node(node):
VectorialParameterNodeAtInstant.check_node_vectorisable(node)
subnodes_name = node._children.keys()
# Recursively vectorize the children of the node
vectorial_subnodes = tuple([
VectorialAsofDateParameterNodeAtInstant.build_from_node(node[subnode_name]).vector
if isinstance(node[subnode_name], ParameterNodeAtInstant)
else node[subnode_name]
for subnode_name in subnodes_name
])
vectorial_subnodes = tuple(
[
VectorialAsofDateParameterNodeAtInstant.build_from_node(
node[subnode_name]
).vector
if isinstance(node[subnode_name], ParameterNodeAtInstant)
else node[subnode_name]
for subnode_name in subnodes_name
]
)
# A vectorial node is a wrapper around a numpy recarray
# We first build the recarray
recarray = numpy.array(
[vectorial_subnodes],
dtype=[
(subnode_name, subnode.dtype if isinstance(subnode, numpy.recarray) else 'float')
(
subnode_name,
subnode.dtype if isinstance(subnode, numpy.recarray) else "float",
)
for (subnode_name, subnode) in zip(subnodes_name, vectorial_subnodes)
]
],
)
return VectorialAsofDateParameterNodeAtInstant(
node._name, recarray.view(numpy.recarray), node._instant_str
)
return VectorialAsofDateParameterNodeAtInstant(node._name, recarray.view(numpy.recarray), node._instant_str)

def __getitem__(self, key):
# If the key is a string, just get the subnode
if isinstance(key, str):
key = numpy.array([key], dtype='datetime64[D]')
key = numpy.array([key], dtype="datetime64[D]")
return self.__getattr__(key)
# If the key is a vector, e.g. ['1990-11-25', '1983-04-17', '1969-09-09']
elif isinstance(key, numpy.ndarray):
assert numpy.issubdtype(key.dtype, numpy.datetime64)
names = list(self.dtype.names) # Get all the names of the subnodes, e.g. ['before_X', 'after_X', 'after_Y']
names = list(
self.dtype.names
) # Get all the names of the subnodes, e.g. ['before_X', 'after_X', 'after_Y']
values = numpy.asarray([value for value in self.vector[0]])
names = [name for name in names if not name.startswith("before")]
names = [
name
numpy.datetime64("-".join(name[len("after_") :].split("_")))
for name in names
if not name.startswith("before")
]
names = [
numpy.datetime64(
"-".join(name[len("after_"):].split("_"))
)
for name in names
]
conditions = sum([
name <= key
for name in names
])
conditions = sum([name <= key for name in names])
result = values[conditions]

# If the result is not a leaf, wrap the result in a vectorial node.
if numpy.issubdtype(result.dtype, numpy.record) or numpy.issubdtype(result.dtype, numpy.void):
return VectorialAsofDateParameterNodeAtInstant(self._name, result.view(numpy.recarray), self._instant_str)
if numpy.issubdtype(result.dtype, numpy.record) or numpy.issubdtype(
result.dtype, numpy.void
):
return VectorialAsofDateParameterNodeAtInstant(
self._name, result.view(numpy.recarray), self._instant_str
)

return result
2 changes: 0 additions & 2 deletions openfisca_core/reforms/reform.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def __init__(self, baseline):
self.decomposition_file_path = baseline.decomposition_file_path
self.key = self.__class__.__name__
if not hasattr(self, "apply"):
raise Exception(
raise Exception(f"Reform {self.key} must define an `apply` function")
)
self.apply()

def __getattr__(self, attribute):
Expand Down
2 changes: 0 additions & 2 deletions openfisca_core/scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def build_tax_benefit_system(country_package_name, extensions, reforms):
if not hasattr(country_package, "CountryTaxBenefitSystem"):
raise ImportError(
f"`{country_package_name}` does not seem to be a valid Openfisca country package."
country_package_name
)
)

country_package = importlib.import_module(country_package_name)
Expand Down
3 changes: 1 addition & 2 deletions openfisca_core/simulations/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def check_type(input, input_type, path=None):

if not isinstance(input, input_type):
raise errors.SituationParsingError(
path,
path, f"Invalid type: must be of type '{json_type_map[input_type]}'."
path, path, f"Invalid type: must be of type '{json_type_map[input_type]}'."
)


Expand Down
20 changes: 9 additions & 11 deletions openfisca_core/simulations/simulation.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from __future__ import annotations

from typing import NamedTuple
from typing import Dict, Mapping, NamedTuple, Optional, Set

from openfisca_core.types import Population, TaxBenefitSystem, Variable
from typing import Dict, Mapping, NamedTuple, Optional, Set

import tempfile
import warnings
Expand Down Expand Up @@ -178,8 +177,8 @@ def calculate_add(self, variable_name: str, period):

# Rule out incompatible periods.
if (
variable.definition_period in (DateUnit.MONTH, DateUnit.DAY)
and period.unit == DateUnit.WEEK
variable.definition_period in (periods.DateUnit.MONTH, periods.DateUnit.DAY)
and period.unit == periods.DateUnit.WEEK
):
raise ValueError(
"Unable to compute variable '{0}' for period {1}, as {1} and {2} are incompatible periods. You can however change the requested period to 'period.this_year'.".format(
Expand All @@ -188,8 +187,9 @@ def calculate_add(self, variable_name: str, period):
)

if (
variable.definition_period in (DateUnit.WEEK, DateUnit.WEEKDAY)
and period.unit == DateUnit.MONTH
variable.definition_period
in (periods.DateUnit.WEEK, periods.DateUnit.WEEKDAY)
and period.unit == periods.DateUnit.MONTH
):
raise ValueError(
"Unable to compute variable '{0}' for period {1}, as {1} and {2} are incompatible periods. You can however change the requested period to 'period.this_year' or 'period.first_week'.".format(
Expand Down Expand Up @@ -248,9 +248,9 @@ def calculate_divide(self, variable_name: str, period):
)

if variable.definition_period not in (
periods.DateUnit.isoformat + periods.DateUnit.isocalendar
periods.DateUnit.YEAR,
periods.DateUnit.WEEK,
):
if variable.definition_period not in (DateUnit.YEAR, DateUnit.WEEK):
raise ValueError(
f"Unable to DIVIDE constant variable '{variable.name}' over "
f"the period {period}: eternal variables can't be divided "
Expand Down Expand Up @@ -421,15 +421,13 @@ def _check_for_cycle(self, variable: str, period):
if frame["name"] == variable
]
if period in previous_periods:
raise CycleError(
raise errors.CycleError(
f"Circular definition detected on formula {variable}@{period}"
)
spiral = len(previous_periods) >= self.max_spiral_loops
if spiral:
self.invalidate_spiral_variables(variable)
message = f"Quasicircular definition detected on formula {variable}@{period} involving {self.tracer.stack}"
variable, period, self.tracer.stack
)
raise errors.SpiralError(message, variable)

def invalidate_cache_entry(self, variable: str, period):
Expand Down
9 changes: 3 additions & 6 deletions openfisca_core/simulations/simulation_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import numpy

from openfisca_core import entities, errors, periods, populations, variables
from openfisca_core.populations import Population
from openfisca_core.simulations import Simulation, helpers
from openfisca_core.variables import Variable

from . import helpers
from ._build_default_simulation import _BuildDefaultSimulation
Expand All @@ -36,9 +39,6 @@
TaxBenefitSystem,
Variables,
)
from openfisca_core.populations import Population
from openfisca_core.simulations import Simulation, helpers
from openfisca_core.variables import Variable


class SimulationBuilder:
Expand Down Expand Up @@ -142,9 +142,6 @@ def build_from_dict(
return self.build_from_entities(tax_benefit_system, params)

if not are_entities_specified(params := input_dict, variables):
if any(
key in tax_benefit_system.entities_plural() for key in input_dict.keys()
):
return self.build_from_entities(tax_benefit_system, input_dict)
else:
return self.build_from_variables(tax_benefit_system, params)
Expand Down
4 changes: 1 addition & 3 deletions openfisca_core/taxbenefitsystems/tax_benefit_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ def load_variable(self, variable_class, update=False):
if baseline_variable and not update:
raise VariableNameConflictError(
f'Variable "{name}" is already defined. Use `update_variable` to replace it.'
name
)
)

variable = variable_class(baseline_variable=baseline_variable)
Expand Down Expand Up @@ -272,7 +270,7 @@ def add_variables_from_file(self, file_path):
self.add_variable(pot_variable)
except Exception:
log.error(
log.error(f'Unable to load OpenFisca variables from file "{file_path}"')
log.error(f'Unable to load OpenFisca variables from file "{file_path}"')
)
raise

Expand Down
4 changes: 2 additions & 2 deletions openfisca_core/types/_data.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from collections.abc import Sequence
from nptyping import NDArray as Array
from nptyping import types
from typing import TypeVar, Union

# from typing import Sequence, TypeVar, Union
# from nptyping import types, NDArray as Array
from numpy.typing import NDArray as Array # noqa: F401
from typing import Sequence, TypeVar
from typing import Sequence, TypeVar, Union

# import numpy

Expand Down
2 changes: 0 additions & 2 deletions openfisca_core/variables/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,9 @@ def check_set_value(self, value):
if self.value_type == datetime.date:
error_message = f"Can't deal with date: '{value}'."
else:
error_message = (
error_message = f"Can't deal with value: expected type {self.json_type}, received '{value}'."
raise ValueError(error_message)
except OverflowError:
error_message = (
error_message = f"Can't deal with value: '{value}', it's too large for type '{self.json_type}'."
raise ValueError(error_message)

Expand Down
12 changes: 4 additions & 8 deletions openfisca_web_api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,22 @@ def init_tracker(url, idsite, tracker_token):

tracker = piwik.PiwikTracker(url, idsite, tracker_token)

message = (
[
message = [
"You chose to activate the `tracker` module. ",
"Tracking data will be sent to: " + url,
"For more information, see <https://github.com/openfisca/openfisca-core#tracker-configuration>.",
]
)
]

log.info(os.linesep.join(message))

return tracker

except ImportError:
message = (
[
message = [
traceback.format_exc(),
"You chose to activate the `tracker` module, but it is not installed.",
"For more information, see <https://github.com/openfisca/openfisca-core#tracker-installation>.",
]
)
]

log.warn(os.linesep.join(message))

Expand Down
Loading

0 comments on commit e0b65f3

Please sign in to comment.