Skip to content

Commit 73c8b89

Browse files
committed
better way to have predefined cast in types file
1 parent 4dc2059 commit 73c8b89

28 files changed

Lines changed: 294 additions & 340 deletions

src/opengeodeweb_back/geode_functions.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@
44
# Third party imports
55
import werkzeug
66
import flask
7-
from typing import cast
87

98
# Local application imports
109
from .geode_objects import geode_objects
11-
from .geode_objects.types import (
12-
GeodeObjectType,
13-
geode_object_type,
14-
)
10+
from .geode_objects.types import GeodeObjectType, geode_object_type, cast_str
1511
from .geode_objects.geode_object import GeodeObject
1612
from opengeodeweb_microservice.database.data import Data
1713

@@ -49,7 +45,7 @@ def get_data_info(data_id: str) -> Data:
4945
def upload_file_path(filename: str) -> str:
5046
upload_folder = flask.current_app.config["UPLOAD_FOLDER"]
5147
secure_filename = werkzeug.utils.secure_filename(filename)
52-
return cast(str, os.path.abspath(os.path.join(upload_folder, secure_filename)))
48+
return cast_str(os.path.abspath(os.path.join(upload_folder, secure_filename)))
5349

5450

5551
def geode_object_output_extensions(

src/opengeodeweb_back/geode_objects/geode_brep.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Standard library imports
22
from __future__ import annotations
3-
from typing import cast, Any
3+
from typing import cast
44

55
# Third party imports
66
import opengeode as og
@@ -9,7 +9,7 @@
99
import geode_viewables as viewables
1010

1111
# Local application imports
12-
from .types import GeodeModelType
12+
from .types import GeodeModelType, cast_str, cast_list_str, cast_int, cast_bool
1313
from .geode_model import GeodeModel, ComponentRegistry
1414

1515

@@ -25,7 +25,7 @@ def geode_object_type(cls) -> GeodeModelType:
2525
return "BRep"
2626

2727
def native_extension(self) -> str:
28-
return cast(str, self.brep.native_extension())
28+
return cast_str(self.brep.native_extension())
2929

3030
@classmethod
3131
def is_3D(cls) -> bool:
@@ -52,35 +52,34 @@ def is_loadable(cls, filename: str) -> og.Percentage:
5252

5353
@classmethod
5454
def input_extensions(cls) -> list[str]:
55-
return cast(list[str], og.BRepInputFactory.list_creators())
55+
return cast_list_str(og.BRepInputFactory.list_creators())
5656

5757
@classmethod
5858
def output_extensions(cls) -> list[str]:
59-
return cast(list[str], og.BRepOutputFactory.list_creators())
59+
return cast_list_str(og.BRepOutputFactory.list_creators())
6060

6161
@classmethod
6262
def object_priority(cls, filename: str) -> int:
63-
return cast(int, og.brep_object_priority(filename))
63+
return cast_int(og.brep_object_priority(filename))
6464

6565
def is_saveable(self, filename: str) -> bool:
66-
return cast(bool, og.is_brep_saveable(self.brep, filename))
66+
return cast_bool(og.is_brep_saveable(self.brep, filename))
6767

6868
def save(self, filename: str) -> list[str]:
69-
return cast(list[str], og.save_brep(self.brep, filename))
69+
return cast_list_str(og.save_brep(self.brep, filename))
7070

7171
def save_viewable(self, filename_without_extension: str) -> str:
72-
return cast(
73-
str, viewables.save_viewable_brep(self.brep, filename_without_extension)
72+
return cast_str(
73+
viewables.save_viewable_brep(self.brep, filename_without_extension)
7474
)
7575

7676
def save_light_viewable(self, filename_without_extension: str) -> str:
77-
return cast(
78-
str,
79-
viewables.save_light_viewable_brep(self.brep, filename_without_extension),
77+
return cast_str(
78+
viewables.save_light_viewable_brep(self.brep, filename_without_extension)
8079
)
8180

8281
def mesh_components(self) -> ComponentRegistry:
83-
return cast(dict[Any, list[Any]], self.brep.mesh_components())
82+
return cast(ComponentRegistry, self.brep.mesh_components())
8483

8584
def inspect(self) -> og_inspector.BRepInspectionResult:
8685
return og_inspector.inspect_brep(self.brep)

src/opengeodeweb_back/geode_objects/geode_cross_section.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# Standard library imports
22
from __future__ import annotations
3-
from typing import cast
43

54
# Third party imports
65
import opengeode as og
76
import opengeode_geosciences as og_geosciences
87
import geode_viewables as viewables
98

109
# Local application imports
11-
from .types import GeodeModelType
10+
from .types import GeodeModelType, cast_str, cast_list_str, cast_int, cast_bool
1211
from .geode_section import GeodeSection
1312

1413

@@ -30,7 +29,7 @@ def geode_object_type(cls) -> GeodeModelType:
3029
return "CrossSection"
3130

3231
def native_extension(self) -> str:
33-
return cast(str, self.cross_section.native_extension())
32+
return cast_str(self.cross_section.native_extension())
3433

3534
def builder(self) -> og_geosciences.CrossSectionBuilder:
3635
return og_geosciences.CrossSectionBuilder(self.cross_section)
@@ -49,38 +48,36 @@ def is_loadable(cls, filename: str) -> og.Percentage:
4948

5049
@classmethod
5150
def input_extensions(cls) -> list[str]:
52-
return cast(list[str], og_geosciences.CrossSectionInputFactory.list_creators())
51+
return cast_list_str(og_geosciences.CrossSectionInputFactory.list_creators())
5352

5453
@classmethod
5554
def output_extensions(cls) -> list[str]:
56-
return cast(list[str], og_geosciences.CrossSectionOutputFactory.list_creators())
55+
return cast_list_str(og_geosciences.CrossSectionOutputFactory.list_creators())
5756

5857
@classmethod
5958
def object_priority(cls, filename: str) -> int:
60-
return cast(int, og_geosciences.cross_section_object_priority(filename))
59+
return cast_int(og_geosciences.cross_section_object_priority(filename))
6160

6261
def is_saveable(self, filename: str) -> bool:
63-
return cast(
64-
bool, og_geosciences.is_cross_section_saveable(self.cross_section, filename)
62+
return cast_bool(
63+
og_geosciences.is_cross_section_saveable(self.cross_section, filename)
6564
)
6665

6766
def save(self, filename: str) -> list[str]:
68-
return cast(
69-
list[str], og_geosciences.save_cross_section(self.cross_section, filename)
67+
return cast_list_str(
68+
og_geosciences.save_cross_section(self.cross_section, filename)
7069
)
7170

7271
def save_viewable(self, filename_without_extension: str) -> str:
73-
return cast(
74-
str,
72+
return cast_str(
7573
viewables.save_viewable_cross_section(
7674
self.cross_section, filename_without_extension
77-
),
75+
)
7876
)
7977

8078
def save_light_viewable(self, filename_without_extension: str) -> str:
81-
return cast(
82-
str,
79+
return cast_str(
8380
viewables.save_light_viewable_cross_section(
8481
self.cross_section, filename_without_extension
85-
),
82+
)
8683
)

src/opengeodeweb_back/geode_objects/geode_edged_curve2d.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Standard library imports
22
from __future__ import annotations
3-
from typing import cast
43

54
# Third party imports
65
import opengeode as og
@@ -9,7 +8,7 @@
98
import geode_viewables as viewables
109

1110
# Local application imports
12-
from .types import GeodeMeshType
11+
from .types import GeodeMeshType, cast_str, cast_list_str, cast_int, cast_bool
1312
from .geode_graph import GeodeGraph
1413

1514

@@ -27,7 +26,7 @@ def geode_object_type(cls) -> GeodeMeshType:
2726
return "EdgedCurve2D"
2827

2928
def native_extension(self) -> str:
30-
return cast(str, self.edged_curve.native_extension())
29+
return cast_str(self.edged_curve.native_extension())
3130

3231
@classmethod
3332
def is_3D(cls) -> bool:
@@ -54,33 +53,31 @@ def is_loadable(cls, filename: str) -> og.Percentage:
5453

5554
@classmethod
5655
def input_extensions(cls) -> list[str]:
57-
return cast(list[str], og.EdgedCurveInputFactory2D.list_creators())
56+
return cast_list_str(og.EdgedCurveInputFactory2D.list_creators())
5857

5958
@classmethod
6059
def output_extensions(cls) -> list[str]:
61-
return cast(list[str], og.EdgedCurveOutputFactory2D.list_creators())
60+
return cast_list_str(og.EdgedCurveOutputFactory2D.list_creators())
6261

6362
@classmethod
6463
def object_priority(cls, filename: str) -> int:
65-
return cast(int, og.edged_curve_object_priority2D(filename))
64+
return cast_int(og.edged_curve_object_priority2D(filename))
6665

6766
def is_saveable(self, filename: str) -> bool:
68-
return cast(bool, og.is_edged_curve_saveable2D(self.edged_curve, filename))
67+
return cast_bool(og.is_edged_curve_saveable2D(self.edged_curve, filename))
6968

7069
def save(self, filename: str) -> list[str]:
71-
return cast(list[str], og.save_edged_curve2D(self.edged_curve, filename))
70+
return cast_list_str(og.save_edged_curve2D(self.edged_curve, filename))
7271

7372
def save_viewable(self, filename_without_extension: str) -> str:
74-
return cast(
75-
str,
73+
return cast_str(
7674
viewables.save_viewable_edged_curve2D(
7775
self.edged_curve, filename_without_extension
7876
),
7977
)
8078

8179
def save_light_viewable(self, filename_without_extension: str) -> str:
82-
return cast(
83-
str,
80+
return cast_str(
8481
viewables.save_light_viewable_edged_curve2D(
8582
self.edged_curve, filename_without_extension
8683
),

src/opengeodeweb_back/geode_objects/geode_edged_curve3d.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# Standard library imports
22
from __future__ import annotations
3-
from typing import cast
43

54
# Third party imports
65
import opengeode as og
@@ -9,7 +8,7 @@
98
import geode_viewables as viewables
109

1110
# Local application imports
12-
from .types import GeodeMeshType
11+
from .types import GeodeMeshType, cast_str, cast_list_str, cast_int, cast_bool
1312
from .geode_graph import GeodeGraph
1413

1514

@@ -27,7 +26,7 @@ def geode_object_type(cls) -> GeodeMeshType:
2726
return "EdgedCurve3D"
2827

2928
def native_extension(self) -> str:
30-
return cast(str, self.edged_curve.native_extension())
29+
return cast_str(self.edged_curve.native_extension())
3130

3231
@classmethod
3332
def is_3D(cls) -> bool:
@@ -54,33 +53,31 @@ def is_loadable(cls, filename: str) -> og.Percentage:
5453

5554
@classmethod
5655
def input_extensions(cls) -> list[str]:
57-
return cast(list[str], og.EdgedCurveInputFactory3D.list_creators())
56+
return cast_list_str(og.EdgedCurveInputFactory3D.list_creators())
5857

5958
@classmethod
6059
def output_extensions(cls) -> list[str]:
61-
return cast(list[str], og.EdgedCurveOutputFactory3D.list_creators())
60+
return cast_list_str(og.EdgedCurveOutputFactory3D.list_creators())
6261

6362
@classmethod
6463
def object_priority(cls, filename: str) -> int:
65-
return cast(int, og.edged_curve_object_priority3D(filename))
64+
return cast_int(og.edged_curve_object_priority3D(filename))
6665

6766
def is_saveable(self, filename: str) -> bool:
68-
return cast(bool, og.is_edged_curve_saveable3D(self.edged_curve, filename))
67+
return cast_bool(og.is_edged_curve_saveable3D(self.edged_curve, filename))
6968

7069
def save(self, filename: str) -> list[str]:
71-
return cast(list[str], og.save_edged_curve3D(self.edged_curve, filename))
70+
return cast_list_str(og.save_edged_curve3D(self.edged_curve, filename))
7271

7372
def save_viewable(self, filename_without_extension: str) -> str:
74-
return cast(
75-
str,
73+
return cast_str(
7674
viewables.save_viewable_edged_curve3D(
7775
self.edged_curve, filename_without_extension
7876
),
7977
)
8078

8179
def save_light_viewable(self, filename_without_extension: str) -> str:
82-
return cast(
83-
str,
80+
return cast_str(
8481
viewables.save_light_viewable_edged_curve3D(
8582
self.edged_curve, filename_without_extension
8683
),

src/opengeodeweb_back/geode_objects/geode_graph.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# Standard library imports
22
from __future__ import annotations
3-
from typing import cast
43

54
# Third party imports
65
import opengeode as og
76

87
# Local application imports
9-
from .types import GeodeMeshType
8+
from .types import GeodeMeshType, cast_str, cast_list_str, cast_int, cast_bool
109
from .geode_vertex_set import GeodeVertexSet
1110

1211

@@ -22,7 +21,7 @@ def geode_object_type(cls) -> GeodeMeshType:
2221
return "Graph"
2322

2423
def native_extension(self) -> str:
25-
return cast(str, self.graph.native_extension())
24+
return cast_str(self.graph.native_extension())
2625

2726
@classmethod
2827
def is_3D(cls) -> bool:
@@ -49,21 +48,21 @@ def is_loadable(cls, filename: str) -> og.Percentage:
4948

5049
@classmethod
5150
def input_extensions(cls) -> list[str]:
52-
return cast(list[str], og.GraphInputFactory.list_creators())
51+
return cast_list_str(og.GraphInputFactory.list_creators())
5352

5453
@classmethod
5554
def output_extensions(cls) -> list[str]:
56-
return cast(list[str], og.GraphOutputFactory.list_creators())
55+
return cast_list_str(og.GraphOutputFactory.list_creators())
5756

5857
@classmethod
5958
def object_priority(cls, filename: str) -> int:
60-
return cast(int, og.graph_object_priority(filename))
59+
return cast_int(og.graph_object_priority(filename))
6160

6261
def is_saveable(self, filename: str) -> bool:
63-
return cast(bool, og.is_graph_saveable(self.graph, filename))
62+
return cast_bool(og.is_graph_saveable(self.graph, filename))
6463

6564
def save(self, filename: str) -> list[str]:
66-
return cast(list[str], og.save_graph(self.graph, filename))
65+
return cast_list_str(og.save_graph(self.graph, filename))
6766

6867
def save_viewable(self, filename_without_extension: str) -> str:
6968
return ""

0 commit comments

Comments
 (0)