Skip to content

Commit eb154a0

Browse files
authored
Remove pre Python 3.9 syntax (#1185)
2 parents 00fe69a + d05b3aa commit eb154a0

File tree

182 files changed

+3303
-3193
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

182 files changed

+3303
-3193
lines changed

.github/get_pypi_info.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ def get_info(package_name: str = "") -> dict:
1818
::return:: A dict with last_version, url and sha256
1919
"""
2020
if package_name == "":
21-
raise ValueError("Package name not provided.")
21+
msg = "Package name not provided."
22+
raise ValueError(msg)
2223
url = f"https://pypi.org/pypi/{package_name}/json"
2324
print(f"Calling {url}") # noqa: T201
2425
resp = requests.get(url)
2526
if resp.status_code != 200:
26-
raise Exception(f"ERROR calling PyPI ({url}) : {resp}")
27+
msg = f"ERROR calling PyPI ({url}) : {resp}"
28+
raise Exception(msg)
2729
resp = resp.json()
2830
version = resp["info"]["version"]
2931

@@ -38,19 +40,19 @@ def get_info(package_name: str = "") -> dict:
3840
return {}
3941

4042

41-
def replace_in_file(filepath: str, info: dict):
43+
def replace_in_file(filepath: str, info: dict) -> None:
4244
"""Replace placeholder in meta.yaml by their values.
4345
4446
::filepath:: Path to meta.yaml, with filename.
4547
::info:: Dict with information to populate.
4648
"""
47-
with open(filepath, "rt", encoding="utf-8") as fin:
49+
with open(filepath, encoding="utf-8") as fin:
4850
meta = fin.read()
4951
# Replace with info from PyPi
5052
meta = meta.replace("PYPI_VERSION", info["last_version"])
5153
meta = meta.replace("PYPI_URL", info["url"])
5254
meta = meta.replace("PYPI_SHA256", info["sha256"])
53-
with open(filepath, "wt", encoding="utf-8") as fout:
55+
with open(filepath, "w", encoding="utf-8") as fout:
5456
fout.write(meta)
5557
print(f"File {filepath} has been updated with info from PyPi.") # noqa: T201
5658

@@ -75,6 +77,7 @@ def replace_in_file(filepath: str, info: dict):
7577
args = parser.parse_args()
7678
info = get_info(args.package)
7779
print( # noqa: T201
78-
"Information of the last published PyPi package :", info["last_version"]
80+
"Information of the last published PyPi package :",
81+
info["last_version"],
7982
)
8083
replace_in_file(args.filename, info)

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
### 41.5.6 [#1185](https://github.com/openfisca/openfisca-core/pull/1185)
4+
5+
#### Technical changes
6+
7+
- Remove pre Python 3.9 syntax.
8+
39
### 41.5.5 [#1220](https://github.com/openfisca/openfisca-core/pull/1220)
410

511
#### Technical changes

openfisca_core/commons/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,16 @@
5252

5353
# Official Public API
5454

55-
from .formulas import apply_thresholds, concat, switch # noqa: F401
56-
from .misc import empty_clone, stringify_array # noqa: F401
57-
from .rates import average_rate, marginal_rate # noqa: F401
55+
from .formulas import apply_thresholds, concat, switch
56+
from .misc import empty_clone, stringify_array
57+
from .rates import average_rate, marginal_rate
5858

5959
__all__ = ["apply_thresholds", "concat", "switch"]
6060
__all__ = ["empty_clone", "stringify_array", *__all__]
6161
__all__ = ["average_rate", "marginal_rate", *__all__]
6262

6363
# Deprecated
6464

65-
from .dummy import Dummy # noqa: F401
65+
from .dummy import Dummy
6666

6767
__all__ = ["Dummy", *__all__]

openfisca_core/commons/dummy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,3 @@ def __init__(self) -> None:
2020
"and will be removed in the future.",
2121
]
2222
warnings.warn(" ".join(message), DeprecationWarning, stacklevel=2)
23-
pass

openfisca_core/commons/formulas.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77

88

99
def apply_thresholds(
10-
input: t.Array[numpy.float_],
10+
input: t.Array[numpy.float64],
1111
thresholds: t.ArrayLike[float],
1212
choices: t.ArrayLike[float],
13-
) -> t.Array[numpy.float_]:
13+
) -> t.Array[numpy.float64]:
1414
"""Makes a choice based on an input and thresholds.
1515
1616
From a list of ``choices``, this function selects one of these values
@@ -38,7 +38,6 @@ def apply_thresholds(
3838
array([10, 10, 15, 15, 20])
3939
4040
"""
41-
4241
condlist: list[Union[t.Array[numpy.bool_], bool]]
4342
condlist = [input <= threshold for threshold in thresholds]
4443

@@ -47,12 +46,9 @@ def apply_thresholds(
4746
# must be true to return it.
4847
condlist += [True]
4948

50-
assert len(condlist) == len(choices), " ".join(
51-
[
52-
"'apply_thresholds' must be called with the same number of",
53-
"thresholds than choices, or one more choice.",
54-
]
55-
)
49+
assert len(condlist) == len(
50+
choices
51+
), "'apply_thresholds' must be called with the same number of thresholds than choices, or one more choice."
5652

5753
return numpy.select(condlist, choices)
5854

@@ -78,7 +74,6 @@ def concat(
7874
array(['this1.0', 'that2.5']...)
7975
8076
"""
81-
8277
if isinstance(this, numpy.ndarray) and not numpy.issubdtype(this.dtype, numpy.str_):
8378
this = this.astype("str")
8479

@@ -89,9 +84,9 @@ def concat(
8984

9085

9186
def switch(
92-
conditions: t.Array[numpy.float_],
87+
conditions: t.Array[numpy.float64],
9388
value_by_condition: Mapping[float, float],
94-
) -> t.Array[numpy.float_]:
89+
) -> t.Array[numpy.float64]:
9590
"""Mimicks a switch statement.
9691
9792
Given an array of conditions, returns an array of the same size,
@@ -115,11 +110,10 @@ def switch(
115110
array([80, 80, 80, 90])
116111
117112
"""
118-
119113
assert (
120114
len(value_by_condition) > 0
121115
), "'switch' must be called with at least one value."
122116

123-
condlist = [conditions == condition for condition in value_by_condition.keys()]
117+
condlist = [conditions == condition for condition in value_by_condition]
124118

125119
return numpy.select(condlist, tuple(value_by_condition.values()))

openfisca_core/commons/misc.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def empty_clone(original: T) -> T:
3030
True
3131
3232
"""
33-
3433
Dummy: object
3534
new: T
3635

@@ -60,7 +59,7 @@ def stringify_array(array: Optional[t.Array[numpy.generic]]) -> str:
6059
>>> stringify_array(None)
6160
'None'
6261
63-
>>> array = numpy.array([10, 20.])
62+
>>> array = numpy.array([10, 20.0])
6463
>>> stringify_array(array)
6564
'[10.0, 20.0]'
6665
@@ -73,7 +72,6 @@ def stringify_array(array: Optional[t.Array[numpy.generic]]) -> str:
7372
"[<class 'list'>, {}, <function stringify_array...]"
7473
7574
"""
76-
7775
if array is None:
7876
return "None"
7977

openfisca_core/commons/rates.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66

77

88
def average_rate(
9-
target: Array[numpy.float_],
9+
target: Array[numpy.float64],
1010
varying: ArrayLike[float],
1111
trim: Optional[ArrayLike[float]] = None,
12-
) -> Array[numpy.float_]:
12+
) -> Array[numpy.float64]:
1313
"""Computes the average rate of a target net income.
1414
1515
Given a ``target`` net income, and according to the ``varying`` gross
@@ -35,13 +35,12 @@ def average_rate(
3535
Examples:
3636
>>> target = numpy.array([1, 2, 3])
3737
>>> varying = [2, 2, 2]
38-
>>> trim = [-1, .25]
38+
>>> trim = [-1, 0.25]
3939
>>> average_rate(target, varying, trim)
4040
array([ nan, 0. , -0.5])
4141
4242
"""
43-
44-
average_rate: Array[numpy.float_]
43+
average_rate: Array[numpy.float64]
4544

4645
average_rate = 1 - target / varying
4746

@@ -62,10 +61,10 @@ def average_rate(
6261

6362

6463
def marginal_rate(
65-
target: Array[numpy.float_],
66-
varying: Array[numpy.float_],
64+
target: Array[numpy.float64],
65+
varying: Array[numpy.float64],
6766
trim: Optional[ArrayLike[float]] = None,
68-
) -> Array[numpy.float_]:
67+
) -> Array[numpy.float64]:
6968
"""Computes the marginal rate of a target net income.
7069
7170
Given a ``target`` net income, and according to the ``varying`` gross
@@ -91,13 +90,12 @@ def marginal_rate(
9190
Examples:
9291
>>> target = numpy.array([1, 2, 3])
9392
>>> varying = numpy.array([1, 2, 4])
94-
>>> trim = [.25, .75]
93+
>>> trim = [0.25, 0.75]
9594
>>> marginal_rate(target, varying, trim)
9695
array([nan, 0.5])
9796
9897
"""
99-
100-
marginal_rate: Array[numpy.float_]
98+
marginal_rate: Array[numpy.float64]
10199

102100
marginal_rate = +1 - (target[:-1] - target[1:]) / (varying[:-1] - varying[1:])
103101

openfisca_core/commons/tests/test_dummy.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
from openfisca_core.commons import Dummy
44

55

6-
def test_dummy_deprecation():
6+
def test_dummy_deprecation() -> None:
77
"""Dummy throws a deprecation warning when instantiated."""
8-
98
with pytest.warns(DeprecationWarning):
109
assert Dummy()

openfisca_core/commons/tests/test_formulas.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
from openfisca_core import commons
66

77

8-
def test_apply_thresholds_when_several_inputs():
8+
def test_apply_thresholds_when_several_inputs() -> None:
99
"""Makes a choice for any given input."""
10-
1110
input_ = numpy.array([4, 5, 6, 7, 8, 9, 10])
1211
thresholds = [5, 7, 9]
1312
choices = [10, 15, 20, 25]
@@ -17,9 +16,8 @@ def test_apply_thresholds_when_several_inputs():
1716
assert_array_equal(result, [10, 10, 15, 15, 20, 20, 25])
1817

1918

20-
def test_apply_thresholds_when_too_many_thresholds():
19+
def test_apply_thresholds_when_too_many_thresholds() -> None:
2120
"""Raises an AssertionError when thresholds > choices."""
22-
2321
input_ = numpy.array([6])
2422
thresholds = [5, 7, 9, 11]
2523
choices = [10, 15, 20]
@@ -28,9 +26,8 @@ def test_apply_thresholds_when_too_many_thresholds():
2826
assert commons.apply_thresholds(input_, thresholds, choices)
2927

3028

31-
def test_apply_thresholds_when_too_many_choices():
29+
def test_apply_thresholds_when_too_many_choices() -> None:
3230
"""Raises an AssertionError when thresholds < choices - 1."""
33-
3431
input_ = numpy.array([6])
3532
thresholds = [5, 7]
3633
choices = [10, 15, 20, 25]
@@ -39,9 +36,8 @@ def test_apply_thresholds_when_too_many_choices():
3936
assert commons.apply_thresholds(input_, thresholds, choices)
4037

4138

42-
def test_concat_when_this_is_array_not_str():
39+
def test_concat_when_this_is_array_not_str() -> None:
4340
"""Casts ``this`` to ``str`` when it is a NumPy array other than string."""
44-
4541
this = numpy.array([1, 2])
4642
that = numpy.array(["la", "o"])
4743

@@ -50,9 +46,8 @@ def test_concat_when_this_is_array_not_str():
5046
assert_array_equal(result, ["1la", "2o"])
5147

5248

53-
def test_concat_when_that_is_array_not_str():
49+
def test_concat_when_that_is_array_not_str() -> None:
5450
"""Casts ``that`` to ``str`` when it is a NumPy array other than string."""
55-
5651
this = numpy.array(["ho", "cha"])
5752
that = numpy.array([1, 2])
5853

@@ -61,19 +56,17 @@ def test_concat_when_that_is_array_not_str():
6156
assert_array_equal(result, ["ho1", "cha2"])
6257

6358

64-
def test_concat_when_args_not_str_array_like():
59+
def test_concat_when_args_not_str_array_like() -> None:
6560
"""Raises a TypeError when args are not a string array-like object."""
66-
6761
this = (1, 2)
6862
that = (3, 4)
6963

7064
with pytest.raises(TypeError):
7165
commons.concat(this, that)
7266

7367

74-
def test_switch_when_values_are_empty():
68+
def test_switch_when_values_are_empty() -> None:
7569
"""Raises an AssertionError when the values are empty."""
76-
7770
conditions = [1, 1, 1, 2]
7871
value_by_condition = {}
7972

openfisca_core/commons/tests/test_rates.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
from openfisca_core import commons
55

66

7-
def test_average_rate_when_varying_is_zero():
7+
def test_average_rate_when_varying_is_zero() -> None:
88
"""Yields infinity when the varying gross income crosses zero."""
9-
109
target = numpy.array([1, 2, 3])
1110
varying = [0, 0, 0]
1211

@@ -15,9 +14,8 @@ def test_average_rate_when_varying_is_zero():
1514
assert_array_equal(result, [-numpy.inf, -numpy.inf, -numpy.inf])
1615

1716

18-
def test_marginal_rate_when_varying_is_zero():
17+
def test_marginal_rate_when_varying_is_zero() -> None:
1918
"""Yields infinity when the varying gross income crosses zero."""
20-
2119
target = numpy.array([1, 2, 3])
2220
varying = numpy.array([0, 0, 0])
2321

0 commit comments

Comments
 (0)