Skip to content

Commit 080cbec

Browse files
committed
Make FieldSet.parse_wildcards() private
1 parent c4e8f6b commit 080cbec

File tree

2 files changed

+94
-18
lines changed

2 files changed

+94
-18
lines changed

parcels/fieldset.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,12 @@ def check_velocityfields(U, V, W):
343343
self._completed = True
344344

345345
@classmethod
346-
def parse_wildcards(cls, paths, filenames, var):
346+
@deprecated_made_private # TODO: Remove 6 months after v3.1.0
347+
def parse_wildcards(self, *args, **kwargs):
348+
return self._parse_wildcards(*args, **kwargs)
349+
350+
@classmethod
351+
def _parse_wildcards(cls, paths, filenames, var):
347352
if not isinstance(paths, list):
348353
paths = sorted(glob(str(paths)))
349354
if len(paths) == 0:
@@ -469,10 +474,10 @@ def from_netcdf(
469474
# Resolve all matching paths for the current variable
470475
paths = filenames[var] if type(filenames) is dict and var in filenames else filenames
471476
if type(paths) is not dict:
472-
paths = cls.parse_wildcards(paths, filenames, var)
477+
paths = cls._parse_wildcards(paths, filenames, var)
473478
else:
474479
for dim, p in paths.items():
475-
paths[dim] = cls.parse_wildcards(p, filenames, var)
480+
paths[dim] = cls._parse_wildcards(p, filenames, var)
476481

477482
# Use dimensions[var] and indices[var] if either of them is a dict of dicts
478483
dims = dimensions[var] if var in dimensions else dimensions

tests/test_deprecations.py

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import inspect
2+
13
import pytest
24

5+
from parcels import Field, FieldSet
36
from tests.utils import create_fieldset_unit_mesh
47

58
fieldset = create_fieldset_unit_mesh()
@@ -14,28 +17,96 @@
1417
"_chunk_set",
1518
]
1619

17-
private_fieldset_attrs = [
18-
"_completed",
19-
]
2020

21+
class FieldPrivate:
22+
attributes = [
23+
"_dataFiles",
24+
"_loaded_time_indices",
25+
"_creation_log",
26+
"_data_chunks",
27+
"_c_data_chunks",
28+
"_chunk_set",
29+
]
30+
methods = [
31+
"_get_dim_filenames",
32+
"_collect_timeslices",
33+
"_reshape",
34+
"_calc_cell_edge_sizes",
35+
"_search_indices_vertical_z",
36+
"_search_indices_vertical_s",
37+
"_reconnect_bnd_indices",
38+
"_search_indices_rectilinear",
39+
"_search_indices_curvilinear",
40+
"_search_indices",
41+
"_interpolator2D",
42+
"_interpolator3D",
43+
"_ccode_eval",
44+
"_ccode_convert",
45+
"_get_block_id",
46+
"_get_block",
47+
"_chunk_setup",
48+
"_chunk_data",
49+
"_rescale_and_set_minmax",
50+
"_data_concatenate",
51+
"_spatial_interpolation",
52+
"_time_index",
53+
]
2154

22-
@pytest.mark.parametrize("private_attribute", private_field_attrs)
23-
def test_private_attribute_field(private_attribute):
55+
56+
class FieldSetPrivate:
57+
attributes = [
58+
"_completed",
59+
]
60+
methods = [
61+
"_add_UVfield",
62+
"_parse_wildcards",
63+
"_check_complete",
64+
]
65+
66+
67+
def assert_private_public_attribute_equiv(obj, private_attribute: str):
2468
assert private_attribute.startswith("_")
2569
attribute = private_attribute.lstrip("_")
2670

2771
with pytest.raises(DeprecationWarning):
28-
assert hasattr(field, attribute)
29-
assert hasattr(field, private_attribute)
30-
assert getattr(field, attribute) is getattr(field, private_attribute)
72+
assert hasattr(obj, attribute)
73+
assert hasattr(obj, private_attribute)
74+
assert getattr(obj, attribute) is getattr(obj, private_attribute)
75+
76+
77+
def assert_public_method_calls_private(type_, private_method):
78+
"""Looks at the source code to ensure that `public_method` calls `private_method`.
79+
80+
Looks for the string `.{method_name}(` in the source code of `public_method`.
81+
"""
82+
assert private_method.startswith("_")
83+
public_method_str = private_method.lstrip("_")
84+
private_method_str = private_method
85+
86+
public_method = getattr(type_, public_method_str)
87+
private_method = getattr(type_, private_method_str)
88+
89+
assert callable(public_method)
90+
assert callable(private_method)
3191

92+
assert f".{private_method_str}(" in inspect.getsource(public_method)
3293

33-
@pytest.mark.parametrize("private_attribute", private_fieldset_attrs)
94+
95+
@pytest.mark.parametrize("private_attribute", FieldPrivate.attributes)
96+
def test_private_attribute_field(private_attribute):
97+
assert_private_public_attribute_equiv(field, private_attribute)
98+
99+
100+
@pytest.mark.parametrize("private_attribute", FieldSetPrivate.attributes)
34101
def test_private_attribute_fieldset(private_attribute):
35-
assert private_attribute.startswith("_")
36-
attribute = private_attribute.lstrip("_")
102+
assert_private_public_attribute_equiv(fieldset, private_attribute)
37103

38-
with pytest.raises(DeprecationWarning):
39-
assert hasattr(fieldset, attribute)
40-
assert hasattr(fieldset, private_attribute)
41-
assert getattr(fieldset, attribute) is getattr(fieldset, private_attribute)
104+
105+
@pytest.mark.parametrize("private_method", FieldPrivate.methods)
106+
def test_private_method_field(private_method):
107+
assert_public_method_calls_private(Field, private_method)
108+
109+
110+
@pytest.mark.parametrize("private_method", FieldSetPrivate.methods)
111+
def test_private_method_fieldset(private_method):
112+
assert_public_method_calls_private(FieldSet, private_method)

0 commit comments

Comments
 (0)